Android---杀死进程重启app

看了网上很多方法都不尽如意,有的软件退了但是并没有成功启动起来

这里是用getLaunchIntentForPackage进行重启,延迟了重启的操作。在重启之前先把程序给杀掉

getBaseContext(): 由构造函数指定或setBaseContext()设置的上下文
getApplicationContext():整个应用程序生命周期的应用程序上下文,当应用程序将销毁时,它也将销毁
getSystemService(Context.ALARM_SERVICE) :系统服务,定时后台重启应用
Intent intent = getBaseContext().getPackageManager()
                .getLaunchIntentForPackage(getBaseContext().getPackageName());
        PendingIntent restartIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, PendingIntent.FLAG_ONE_SHOT);
        AlarmManager mgr = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
        mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 1000, restartIntent); // 1秒钟后重启应用
        System.exit(0);

猜你喜欢

转载自blog.csdn.net/weixin_45265547/article/details/125163865
今日推荐