Skip to content

Instantly share code, notes, and snippets.

@rmkrishna
Last active March 6, 2024 08:01
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save rmkrishna/7e133bcf1a918a02f2c882c1d7011b18 to your computer and use it in GitHub Desktop.
Save rmkrishna/7e133bcf1a918a02f2c882c1d7011b18 to your computer and use it in GitHub Desktop.
To get the Recent Apps in Android
// We should have this 2 permissions to get the recent app
<uses-permission android:name="android.permission.GET_TASKS" />
<uses-permission android:name="android.permission.PACKAGE_USAGE_STATS" />
// we should register our service in manifest file
public class AndroidUtils {
private static final String RECENT_ACTIVITY;
static {
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP) {
RECENT_ACTIVITY = "com.android.systemui.recents.RecentsActivity";
} else if (Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN_MR1) {
RECENT_ACTIVITY = "com.android.systemui.recent.RecentsActivity";
} else {
RECENT_ACTIVITY = "com.android.internal.policy.impl.RecentApplicationDialog";
}
}
public static boolean isRecentActivity(String className) {
if (RECENT_ACTIVITY.equalsIgnoreCase(className)) {
return true;
}
return false;
}
}
public class AppLockService extends Service {
// Write code here to run the service contiuously, and call every 50 to 300 ms getRecentApps(Context context) method to get the current open application
public String getRecentApps(Context context) {
String topPackageName = "";
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
UsageStatsManager mUsageStatsManager = (UsageStatsManager) context.getSystemService(Context.USAGE_STATS_SERVICE);
long time = System.currentTimeMillis();
UsageEvents usageEvents = mUsageStatsManager.queryEvents(time - 1000 * 30, System.currentTimeMillis() + (10 * 1000));
UsageEvents.Event event = new UsageEvents.Event();
while (usageEvents.hasNextEvent()) {
usageEvents.getNextEvent(event);
}
if (event != null && !TextUtils.isEmpty(event.getPackageName()) && event.getEventType() == UsageEvents.Event.MOVE_TO_FOREGROUND) {
if (AndroidUtils.isRecentActivity(event.getClassName())) {
return event.getClassName();
}
return event.getPackageName();
} else {
topPackageName = "";
}
} else {
ActivityManager am = (ActivityManager) context.getSystemService(context.ACTIVITY_SERVICE);
List<ActivityManager.RunningTaskInfo> taskInfo = am.getRunningTasks(1);
ComponentName componentInfo = taskInfo.get(0).topActivity;
// If the current running activity it will return not the package name it will return the activity refernce.
if (AndroidUtils.isRecentActivity(componentInfo.getClassName())) {
return componentInfo.getClassName();
}
topPackageName = componentInfo.getPackageName();
}
return topPackageName;
}
}
public class MainViewActivity extends AppCompatActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Do you stuff
startServiceToGetRecentApps();
}
public void startServiceToGetRecentApps() {
// And you should check first the user API level is 21 or more if yes you should get access for usage access settings
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
if (UStats.getUsageStatsList(this).isEmpty()) {
Intent intent = new Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS);
startActivity(intent);
retrun;
}
}
// Start your service here
}
}
import android.app.usage.UsageStats;
import android.app.usage.UsageStatsManager;
import android.content.Context;
import android.util.Log;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;
/**
* Created by User on 3/2/15.
*/
public class UStats {
public static final String TAG = UStats.class.getSimpleName();
public static List<UsageStats> getUsageStatsList(Context context){
UsageStatsManager usm = (UsageStatsManager) context.getSystemService("usagestats");
Calendar calendar = Calendar.getInstance();
long endTime = calendar.getTimeInMillis();
calendar.add(Calendar.DATE, -1);
long startTime = calendar.getTimeInMillis();
List<UsageStats> usageStatsList = usm.queryUsageStats(UsageStatsManager.INTERVAL_DAILY,startTime,endTime);
return usageStatsList;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment