java程序崩溃怎么重启_android 异常崩溃后 重启app(进程守护方式实现)
【實(shí)例簡介】
【實(shí)例截圖】
【核心代碼】
package com.sunfusheng.daemon.sample;
import android.content.ComponentName;
import android.content.Intent;
import android.os.Looper;
import android.util.Log;
import com.blankj.utilcode.util.AppUtils;
import com.sunfusheng.daemon.AbsHeartBeatService;
/**
* @author sunfusheng on 2018/8/3.
*/
public class HeartBeatService extends AbsHeartBeatService {
private static final String TAG = "---> HeartBeatService";
private static final android.os.Handler mainThreadHandler = new android.os.Handler(Looper.getMainLooper());
@Override
public void onStartService() {
Log.d(TAG, "onStartService()");
}
@Override
public void onStopService() {
Log.e(TAG, "onStopService()");
}
@Override
public long getDelayExecutedMillis() {
return 0;
}
@Override
public long getHeartBeatMillis() {
return 30 * 1000;
}
@Override
public void onHeartBeat() {
String packetName=AppUtils.getAppPackageName();
Log.d(TAG, "onHeartBeat()" packetName);
if(!AppUtils.isAppRunning(packetName)){
//方案一
AppUtils.relaunchApp();
//方案二
// Intent sayHelloIntent=new Intent(this,MainActivity.class);
// sayHelloIntent.setAction(Intent.ACTION_VIEW);
// sayHelloIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
// sayHelloIntent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
// getApplicationContext().startActivity(sayHelloIntent);
//
//方案三
// Intent intent = new Intent("android.intent.action.MAIN");
// intent.setComponent(new ComponentName(getApplicationContext().getPackageName(), MainActivity.class.getName()));
// intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// getApplicationContext().startActivity(intent);
Log.d(TAG, packetName " launchApp Sucess!!!!!!");
}else{
Log.d(TAG, packetName " is running");
if(!AppUtils.isAppForeground()){
Intent sayHelloIntent=new Intent(this,MainActivity.class);
sayHelloIntent.setAction(Intent.ACTION_VIEW);
sayHelloIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
sayHelloIntent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
getApplicationContext().startActivity(sayHelloIntent);
Log.d(TAG, packetName " is FLAG_ACTIVITY_SINGLE_TOP!!!");
}
}
}
}
總結(jié)
以上是生活随笔為你收集整理的java程序崩溃怎么重启_android 异常崩溃后 重启app(进程守护方式实现)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java毛玻璃_模糊效果(毛玻璃效果)
- 下一篇: java 自省_自知 自省 自立 自信