创建异常捕获handler类

import android.app.ActivityManager;import android.app.AlarmManager;import android.app.Application;import android.app.PendingIntent;import android.content.Context;import android.content.Intent;import android.os.Looper;import android.widget.Toast;import com.magus.youxiclient.R;import com.magus.youxiclient.util.Log;/** * Created by yangshuai in the 10:53 of 2016.01.05 . */public class MyUncaughtExceptionHandler implements Thread.UncaughtExceptionHandler {    private static final String TAG = "ExceptionHandler";    private Thread.UncaughtExceptionHandler mUncaughtExceptionHandler;    private Application mApplication;    public MyUncaughtExceptionHandler(Application context) {        mApplication = context;        mUncaughtExceptionHandler = Thread.getDefaultUncaughtExceptionHandler();    }    @Override    public void uncaughtException(Thread thread, Throwable ex) {        if (mApplication.getPackageName().equals(getProcessName(mApplication)))  {            if (Log.showLog) {                mUncaughtExceptionHandler.uncaughtException(thread, ex);            } else {                toastSorry();                restart();                android.os.Process.killProcess(android.os.Process.myPid());            }        } else {            android.os.Process.killProcess(android.os.Process.myPid());        }    }    private void toastSorry() {        new Thread(){            @Override            public void run() {                Looper.prepare();                Toast.makeText(                        mApplication.getApplicationContext(),                        "Program is abnormal, is about to restart the application.",                        Toast.LENGTH_SHORT).show();                Looper.loop();            }        }.start();    }    public static String getProcessName(Context appContext) {        String currentProcessName = "";        int pid = android.os.Process.myPid();        ActivityManager manager = (ActivityManager) appContext.getSystemService(Context.ACTIVITY_SERVICE);        for (ActivityManager.RunningAppProcessInfo processInfo : manager.getRunningAppProcesses()) {            if (processInfo.pid == pid) {                currentProcessName = processInfo.processName;                break;            }        }        return currentProcessName;    }    /* 重启应用 */    private void restart() {        try {            Thread.sleep(4000);        } catch (InterruptedException e) {            android.util.Log.e(TAG, "error : " + e);        }        Intent intent = new Intent(                mApplication.getApplicationContext(),                MainActivity.class);        PendingIntent restartIntent = PendingIntent.getActivity(                mApplication.getApplicationContext(), 0, intent,                Intent.FLAG_ACTIVITY_NEW_TASK);        //退出程序        AlarmManager mgr = (AlarmManager) mApplication.getSystemService(Context.ALARM_SERVICE);        mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 1000,                restartIntent); // 1秒钟后重启应用    }}

Application

import android.app.Application;import com.magus.trainingfirstapp.exception.CrashHandler;/** * Created by yangshuai on 2015/10/13 0013 11:01. */public class MApplication extends Application {    @Override    public void onCreate() {        super.onCreate();        /* 异常捕获(debug 时不捕获异常) */        Thread.setDefaultUncaughtExceptionHandler(new MyUncaughtExceptionHandler(this));    }}

更多相关文章

  1. Android(安卓)apps应用检查更新代码
  2. android正确使用killProcess完全退出应用
  3. android TabLayout的应用
  4. android手机端保存xml数据
  5. Android(安卓)Training - 使用IntentService执行任务(Lesson 1 -
  6. Android之OenGL ES 之 EGL
  7. Android中带分割线的九宫格
  8. android 应用程序性能跟踪
  9. Android(安卓)JNI编程

随机推荐

  1. android 类似QQ 换皮肤 实现思路 apk资源
  2. Android(安卓)build system 分析之 envse
  3. ProGuard Eclipse Android
  4. Android中必须学习的八大开源项目
  5. .Net 转战 Android(安卓)4.4 日常笔记(8)--
  6. Android(安卓)照片压缩
  7. android背景选择器selector用法汇总
  8. Android(安卓)cursor query方法详解
  9. 《Android》Lesson02-第1个Project,Log
  10. Android(安卓)Client-side OAUTH