最近开发中要用重启程序网上收集了一些方法,Mark 一下
1.Intent
Intent intent = new Intent(this, Object.class);
startActivity(intent);
2. 通过ActivityManager来重新启动应用程序:
ActivityManager manager = (ActivityManager)this.getSystemService(Context.ACTIVITY_SERVICE);
manager.restartPackage("com.example.test");
3. 通过flag来实现:
private void restartApplication() {
final Intent intent = getPackageManager().getLaunchIntentForPackage(getPackageName());
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}
4. 利用PendingIntent
Intent mStartActivity = new Intent(context, StartActivity.class);
int mPendingIntentId = 123456;
PendingIntent mPendingIntent = PendingIntent.getActivity(context, mPendingIntentId, mStartActivity, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager mgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 100, mPendingIntent);
System.exit(0);