Reputation: 23
Please help me to cycle through the recently opened application in android. for example, if I have 1, 2, 3, 4, 5 and now I'm in the application 3 and I want to go to 2 and 1 application, in the same way, I want to go to 4 and 5 application.
I have used the below code to navigate to the next app, but it did not work as expected.
final Intent intent = new Intent(Intent.ACTION_MAIN);
final ActivityManager am = (ActivityManager) mContext
.getSystemService(Context.ACTIVITY_SERVICE);
String defaultHomePackage = "com.android.launcher";
intent.addCategory(Intent.CATEGORY_HOME);
final ResolveInfo res = mContext.getPackageManager().resolveActivity(intent, 0);
if (res.activityInfo != null && !res.activityInfo.packageName.equals("android")) {
defaultHomePackage = res.activityInfo.packageName;
}
List <ActivityManager.RunningTaskInfo> tasks = am.getRunningTasks(5);
// lets get enough tasks to find something to switch to
// Note, we'll only get as many as the system currently has - up to 5
while ((lastAppId == 0) && (looper < tasks.size())) {
packageName = tasks.get(looper).topActivity.getPackageName();
if (!packageName.equals(defaultHomePackage) && !packageName.equals("com.android.systemui")) {
lastAppId = tasks.get(looper).id;
}
looper++;
}
if (lastAppId != 0) {
am.moveTaskToFront(lastAppId, ActivityManager.MOVE_TASK_NO_USER_ACTION);
} else {
Toast.makeText(mContext, mStrNoPrevApp, Toast.LENGTH_SHORT).show();
}
Upvotes: 1
Views: 203