0

请帮助我循环浏览最近在 android 中打开的应用程序,例如如果我有 1、2、3、4、5,现在我在 3 个应用程序中,我想以我想去的相同方式去 2 和 1 应用程序4 和 5 应用。

我已经使用下面的代码导航到下一个应用程序,但它没有按预期工作。

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();
                }
            }
4

0 回答 0