例外を集約するクラス import android.app.ActivityManager; import android.content.Context; import android.util.Log; /** * Created by androidyue on 11/23/15. */ public class TestUncaughtExceptionHandler implements Thread.UncaughtExceptionHandler{ private static final String LOGTAG = "TestUncaughtExceptionHandler"; private Thread.UncaughtExceptionHandler mDefaultExceptionHandler; private Context mAppContext; public TestUncaughtExceptionHandler(Context context) { mAppContext = context.getApplicationContext(); mDefaultExceptionHandler = Thread.getDefaultUncaughtExceptionHandler(); } 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; } @Override public void uncaughtException(Thread thread, Throwable ex) { String processName = getProcessName(mAppContext); if (mAppContext.getPackageName().equals(processName)) { Log.i(LOGTAG, "uncaughtException main process"); mDefaultExceptionHandler.uncaughtException(thread, ex); } else { Log.i(LOGTAG, "uncaughtException process name=" + processName); android.os.Process.killProcess(android.os.Process.myPid()); } } } 例外集約するクラスをアプリケーションに追加する public class XXXApplication extends Application { private static final String LOGTAG = "XXXApplication"; @Override public void onCreate() { super.onCreate(); Log.i(LOGTAG, "onCreate"); Thread.setDefaultUncaughtExceptionHandler(new TestUncaughtExceptionHandler(this)); } } ★月額105円~/容量最大30GB/機能満載! ロリポップ!レンタルサーバー ★ コメント: |