Android之app实现重启功能

有些特殊情况可能需要使用到这个功能,为此记录下。
方式一:使用AlarmManger

Intent intent = getBaseContext().getPackageManager().getLaunchIntentForPackage(getBaseContext().getPackageName());
 //与正常页面跳转一样可传递序列化数据,在Launch页面内获得
intent.putExtra("REBOOT","reboot");
 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);
 android.os.Process.killProcess(android.os.Process.myPid());

方式二:通过设置FLAG_ACTIVITY_CLEAR_TOP

Intent intent = getBaseContext().getPackageManager().getLaunchIntentForPackage(getBaseContext().getPackageName());
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
//与正常页面跳转一样可传递序列化数据,在Launch页面内获得
intent.putExtra("REBOOT","reboot");
startActivity(intent);
19

这篇文章还没有评论

发表评论