/**
* Author by ${XuMoumou}
* Email 845514573@123.com
* Date on 2017/12/28.
* PS:此工具類用來(lái)重啟APP抑钟,只是單純的重啟,不做任何處理烈掠。
*/
public class RestartAPPTool {
/**
? ? * 重啟整個(gè)APP
? ? * @param context
? ? * @param Delayed 延遲多少毫秒
? ? */
? ? public static void restartAPP(Context context, long Delayed){
/**開啟一個(gè)新的服務(wù)彤钟,用來(lái)重啟本APP*/
? ? ? ? Intent intent1=new Intent(context,killSelfService.class);
? ? ? ? intent1.putExtra("PackageName",context.getPackageName());
? ? ? ? intent1.putExtra("Delayed",Delayed);
? ? ? ? context.startService(intent1);
? ? ? ? /**殺死整個(gè)進(jìn)程**/
? ? ? ? android.os.Process.killProcess(android.os.Process.myPid());
? ? }
/***重啟整個(gè)APP*/
? ? public static void restartAPP(Context context){
restartAPP(context,1000);
? ? }
}
import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
/***
* 該服務(wù)只用來(lái)讓APP重啟来候,生命周期也僅僅是只是重啟APP。重啟完即自我殺死
*/
public class killSelfServiceextends Service {
/**
? ? * 關(guān)閉應(yīng)用后多久重新啟動(dòng)
? ? */
? ? private static long stopDelayed =1000;
? ? private Handlerhandler;
? ? private StringPackageName;
? ? public killSelfService() {
handler =new Handler();
? ? }
@Override
? ? public int onStartCommand(final Intent intent, int flags, int startId) {
stopDelayed = intent.getLongExtra("Delayed", 1000);
? ? ? ? PackageName = intent.getStringExtra("PackageName");
? ? ? ? handler.postDelayed(new Runnable() {
@Override
? ? ? ? ? ? public void run() {
Intent LaunchIntent = getPackageManager().getLaunchIntentForPackage(PackageName);
? ? ? ? ? ? ? ? startActivity(LaunchIntent);
? ? ? ? ? ? ? ? killSelfService.this.stopSelf();
? ? ? ? ? ? }
}, stopDelayed);
? ? ? ? return super.onStartCommand(intent, flags, startId);
? ? }
@Override
? ? public IBinderonBind(Intent intent) {
return null;
? ? }
}