package com.rincliu.library.app;

import java.lang.Thread.UncaughtExceptionHandler;

import com.rincliu.library.R;
import com.rincliu.library.util.RLUiUtil;

import android.app.ActivityManager;
import android.content.Context;
import android.content.Intent;
import android.os.Looper;

public class RLCrashHandler implements UncaughtExceptionHandler {
	private Context mContext;
	private static RLCrashHandler instance;
	
	/**
	 * 
	 * @return
	 */
	public static RLCrashHandler getInstance() {
		if(instance==null){
			instance=new RLCrashHandler();
		}
		return instance;
	}
	
	/**
	 * 
	 * @param context
	 */
	public void init(Context context) {
		mContext = context;
		Thread.setDefaultUncaughtExceptionHandler(this);
	}

	@Override
	public void uncaughtException(Thread thread, final Throwable ex) {
		ex.printStackTrace();
		new Thread(){
			public void run(){
				Looper.prepare();
				RLUiUtil.toast(mContext,R.string.exception_occured);
		        Looper.loop();
			}
		}.start();
		try {
			Thread.sleep(3000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		Intent intent=new Intent(Intent.ACTION_MAIN);
		intent.addCategory(Intent.CATEGORY_HOME);
		mContext.startActivity(intent);
		android.os.Process.killProcess(android.os.Process.myPid());
		((ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE))
    		.killBackgroundProcesses(mContext.getPackageName());
	}
}
@@ -1077,16 +1077,18 @@
 ();%0A%09%09%7D%0A
+//
 %09%09Intent
@@ -1127,16 +1127,18 @@
 _MAIN);%0A
+//
 %09%09intent
@@ -1173,16 +1173,18 @@
 _HOME);%0A
+//
 %09%09mConte
@@ -1271,16 +1271,18 @@
 Pid());%0A
+//
 %09%09((Acti
@@ -1347,16 +1347,18 @@
 RVICE))%0A
+//
     %09%09.k

改进RLCrashHandler，只关闭崩溃页面，不影响之前页面。