Android中重启程序自己的几种方法

1.

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);

你可能感兴趣的:(移动开发,Android开发)