关于安卓重启设备和重启应用进程

2024-01-02 17:33:26

android 重启应用进程


//多种方式重启应用进程


public class MainActivity
{
    //重启当前Application
    private void restartApplication()
    {
       final Intent intent = getPackageManager().getLaunchIntentForPackage(getPackageName());
       intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
       startActivity(intent);
    }

     // 使用 AlarmManager 来帮助重启
    public static void restartByAlarm(Context context, Class<?> cls)
    {
      Intent mStartActivity = new Intent(context, cls);
      int mPendingIntentId = 123456;
      PendingIntent pIntent = PendingIntent.getActivity(context, mPendingIntentId, mStartActivity, PendingIntent.FLAG_CANCEL_CURRENT);
      AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
      alarmManager.set(AlarmManager.RTC, System.currentTimeMillis() + 500, pIntent);
      System.exit(0);
    }

   
     //使用 killProcess
    public static void restartByKillProcess(Context context, Class<?> cls)
    {
        Intent intent = new Intent(context, cls);
        intent.addFlags(FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
        android.os.Process.killProcess(android.os.Process.myPid());
    }

     //通过清栈触发应用重启。但不会重启 application ,与应用相关的静态变量也会更重启前一样。

    public static void restartByClearTop(Context context)
    {
        Intent intent = context.getPackageManager().getLaunchIntentForPackage(context.getPackageName());
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        context.startActivity(intent);
    }

    //利用系统重启api触发应用重启

    public static void restartBySystemApi(Context context)
    {
        ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        manager.restartPackage(context.getPackageName());
    }

    //通过 Intent.makeRestartActivityTask 来触发应用重启,跟 restartByClearTop 类似。但不会重启 application ,与应用相关的静态变量也会更重启前一样。

    public static void restartByCompatApi(Context context, Class<?> cls)
    {
        Intent intent = new Intent(context, cls);
        Intent restartIntent = Intent.makeRestartActivityTask(intent.getComponent());
        context.startActivity(restartIntent);
        System.exit(0);
    }

    /**
     * 5.1 版本以后可以借助 JobScheduler 来重启应用
     *
     * @param context
     */
    public static void restartByJobScheduler(Context context, Class<?> cls)
    {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
        {
            int delayTimeMin = 1000;
            int delayTimeMax = 2000;

            MyJobSchedulerService.setMainIntent(new Intent(context, cls));

            JobInfo.Builder jobInfoBuild = new JobInfo.Builder(0, new ComponentName(context, MyJobSchedulerService.class));
            jobInfoBuild.setMinimumLatency(delayTimeMin);
            jobInfoBuild.setOverrideDeadline(delayTimeMax);
            JobScheduler jobScheduler = (JobScheduler) context.getSystemService(JOB_SCHEDULER_SERVICE);
            jobScheduler.schedule(jobInfoBuild.build());

            System.exit(0);
        }
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    static class MyJobSchedulerService extends JobService
    {
        private static Intent mIntent;

        public static void setMainIntent(Intent intent)
        {
            mIntent = intent;
        }

        @Override
        public boolean onStartJob(JobParameters params)
        {
            startActivity(mIntent);
            jobFinished(params, false);
            return false;
        }

        @Override
        public boolean onStopJob(JobParameters params)
        {
            return false;
        }
    }

}

重启运行App的当前设备

Process process = Runtime.getRuntime().exec("reboot");

process.waitfor();

文章来源:https://blog.csdn.net/loveryunz/article/details/135343530
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。