在有的时候我们需要重启应用程序,但是有的程序用一般的重启方法重启不了
普通应用的重启
/**
* 重新启动应用
*/
public void reStart(){
Intent launch=getBaseContext().getPackageManager()
.getLaunchIntentForPackage(getBaseContext()
.getPackageName());
launch.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(launch);
}
/**
* 重新启动应用
*/
public void restart(Context context)
{
try {
Intent intent = context.getApplicationContext().getPackageManager().getLaunchIntentForPackage(context.getPackageName());
if (Build.VERSION.SDK_INT >= 11) {
intent.addFlags(32768);
} else {
intent.addFlags(67108864);
}
PendingIntent pendingIntent = PendingIntent.getActivity(context, 223344, intent, 268435456);
((AlarmManager)context.getSystemService("alarm")).set(1, System.currentTimeMillis() + 100L, pendingIntent);
System.exit(0);
return;
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}