重启App,目前有两种办法:
1.使用 PendingIntent,设置在未来某个时刻启动你的app,然后关掉app。
1 2 3 4 5 6 |
Intent mStartActivity = new Intent(context, MainActivity.class); int mPendingIntentId = 123456; PendingIntent mPendingIntent = PendingIntent.getActivity(context, mPendingIntentId, mStartActivity, PendingIntent.FLAG_CANCEL_CURRENT); AlarmManager mgr = (AlarmManager)MeConfigs.context.getSystemService(Context.ALARM_SERVICE); mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 100, mPendingIntent); System.exit(0); |
2.使用NEW_TASK的flag打开ActivityB,然后关掉当前进程,ActivityB马上再打开app。
重启代码
1 2 3 4 |
Intent i = new Intent(getApplicationContext(), DummyActivity.class); i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(i); Process.killProcess(Process.myPid()); |
ActivityB代码
1 2 3 4 5 6 7 8 9 |
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Intent i = new Intent(); i.setData(Uri.parse("xxx")); i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(i); finish(); } |