android 重启应用进程
public class MainActivity
{
private void restartApplication()
{
final Intent intent = getPackageManager().getLaunchIntentForPackage(getPackageName());
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}
public static void restartByAlarm(Context context, Class<?> cls)
{
Intent mStartActivity = new Intent(context, cls);
int mPendingIntentId = 123456;
PendingIntent pIntent = PendingIntent.getActivity(context, mPendingIntentId, mStartActivity, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC, System.currentTimeMillis() + 500, pIntent);
System.exit(0);
}
public static void restartByKillProcess(Context context, Class<?> cls)
{
Intent intent = new Intent(context, cls);
intent.addFlags(FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
android.os.Process.killProcess(android.os.Process.myPid());
}
public static void restartByClearTop(Context context)
{
Intent intent = context.getPackageManager().getLaunchIntentForPackage(context.getPackageName());
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
context.startActivity(intent);
}
public static void restartBySystemApi(Context context)
{
ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
manager.restartPackage(context.getPackageName());
}
public static void restartByCompatApi(Context context, Class<?> cls)
{
Intent intent = new Intent(context, cls);
Intent restartIntent = Intent.makeRestartActivityTask(intent.getComponent());
context.startActivity(restartIntent);
System.exit(0);
}
public static void restartByJobScheduler(Context context, Class<?> cls)
{
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
{
int delayTimeMin = 1000;
int delayTimeMax = 2000;
MyJobSchedulerService.setMainIntent(new Intent(context, cls));
JobInfo.Builder jobInfoBuild = new JobInfo.Builder(0, new ComponentName(context, MyJobSchedulerService.class));
jobInfoBuild.setMinimumLatency(delayTimeMin);
jobInfoBuild.setOverrideDeadline(delayTimeMax);
JobScheduler jobScheduler = (JobScheduler) context.getSystemService(JOB_SCHEDULER_SERVICE);
jobScheduler.schedule(jobInfoBuild.build());
System.exit(0);
}
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
static class MyJobSchedulerService extends JobService
{
private static Intent mIntent;
public static void setMainIntent(Intent intent)
{
mIntent = intent;
}
@Override
public boolean onStartJob(JobParameters params)
{
startActivity(mIntent);
jobFinished(params, false);
return false;
}
@Override
public boolean onStopJob(JobParameters params)
{
return false;
}
}
}
重启运行App的当前设备
Process process = Runtime.getRuntime().exec("reboot");
process.waitfor();