Android 将后台应用切换到前台

  • Post author:
  • Post category:其他


Android 系统中如果想要切换系统中的任务,是需要获取系统权限的,在全局配置文件中添加:

<!–排序系统任务权限 重新排序系统Z轴运行中的任务–>

<uses-permission android:name=”android.permission.REORDER_TASKS” />

public class TgSystem {

    public static void startService(Context context, Intent intent) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            //android8.0以上通过startForegroundService启动service
            context.startForegroundService(intent);
        } else {
            context.startService(intent);
        }
    }

    /**
     * 判断本地是否已经安装好了指定的应用程序包
     *
     * @param packageNameTarget :待判断的 App 包名,如 微博 com.sina.weibo
     * @return 已安装时返回 true,不存在时返回 false
     */
    public static boolean appIsExist(Context context, String packageNameTarget) {
        if (packageNameTarget == null || packageNameTarget.isEmpty()) {
            return false;
        }

        PackageManager packageManager = context.getPackageManager();
        List<PackageInfo> packageInfoList = packageManager.getInstalledPackages(PackageManager.MATCH_UNINSTALLED_PACKAGES);
        for (PackageInfo packageInfo : packageInfoList) {
            if (packageInfo.packageName.equals(packageNameTarget)) {
                return true;
            }
        }

        return false;
    }

    //当本应用位于后台时,则将它切换到最前端
    public static void setTopApp(Context context) {
        if (isRunningForeground(context)) {
            return;
        }
        //获取ActivityManager
        ActivityManager activityManager = (ActivityManager) context.getSystemService(ACTIVITY_SERVICE);

        //获得当前运行的task(任务)
        List<ActivityManager.RunningTaskInfo> taskInfoList = activityManager.getRunningTasks(100);
        for (ActivityManager.RunningTaskInfo taskInfo : taskInfoList) {
            //找到本应用的 task,并将它切换到前台
            if (taskInfo.topActivity.getPackageName().equals(context.getPackageName())) {
                activityManager.moveTaskToFront(taskInfo.id, 0);
                break;
            }
        }
    }

    //判断本应用是否已经位于最前端:已经位于最前端时,返回 true;否则返回 false
    public static boolean isRunningForeground(Context context) {
        ActivityManager activityManager = (ActivityManager) context.getSystemService(ACTIVITY_SERVICE);
        List<ActivityManager.RunningAppProcessInfo> appProcessInfoList = activityManager.getRunningAppProcesses();

        for (ActivityManager.RunningAppProcessInfo appProcessInfo : appProcessInfoList) {
            if (appProcessInfo.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND
                    && appProcessInfo.processName.equals(context.getApplicationInfo().processName)) {
                return true;
            }
        }
        return false;
    }
}

/**最后将被挤压到后台的本应用重新置顶到最前端

* 当自己的应用在后台时,将它切换到前台来*/

TgSystem.setTopApp(MainActivity.this);

private void startLocalApp(String packageNameTarget) {

Log.i(“Wmx logs::”, “———————–开始启动第三方 APP=” + packageNameTarget);

if (SystemHelper.appIsExist(MainActivity.this, packageNameTarget)) {


PackageManager packageManager = getPackageManager();

Intent intent = packageManager.getLaunchIntentForPackage(packageNameTarget);

intent.addCategory(Intent.CATEGORY_LAUNCHER);

intent.setFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED | Intent.FLAG_ACTIVITY_NEW_TASK);

/**android.intent.action.MAIN:打开另一程序

*/

intent.setAction(“android.intent.action.MAIN”);

/**

* FLAG_ACTIVITY_SINGLE_TOP:

* 如果当前栈顶的activity就是要启动的activity,则不会再启动一个新的activity

*/

intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);

startActivity(intent);

} else {


Toast.makeText(getApplicationContext(), “被启动的 APP 未安装”, Toast.LENGTH_SHORT).show();

}

}

————————————————

版权声明:本文为CSDN博主「蚩尤后裔」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。

原文链接:https://blog.csdn.net/wangmx1993328/article/details/83007883