Android 초학 --------프로그램 이상 리셋 데모

<pre name="code" class="java">public class CrashHandler  implements UncaughtExceptionHandler  {
	
	private  static CrashHandler mCrashHandler =new CrashHandler();
	
	private Thread.UncaughtExceptionHandler mDefaultHandler;  
	
	private Context mContext;
	
	private PendingIntent intent;

	@Override
	public void uncaughtException(Thread thread, Throwable ex) {
		// TODO Auto-generated method stub
		Log.i("TTT","      。。。。。。。。。。。。。。。。。。");
		sendHandler();
	}
	
	private void sendHandler() {
		// TODO Auto-generated method stub
		intent = PendingIntent.getActivity(mContext, 0,  new Intent(mContext, MainActivity.class),Intent.FLAG_ACTIVITY_NEW_TASK);
		AlarmManager mgr = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);
		mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 2000, intent);
		System.exit(2);
	}

	private CrashHandler() {
		// TODO Auto-generated constructor stub
	}
	
	public static CrashHandler getInstance(){
		return  mCrashHandler;
	}
	
	
	public void init(Context context){
		mContext=context;
		mDefaultHandler=Thread.getDefaultUncaughtExceptionHandler();
		Thread.setDefaultUncaughtExceptionHandler(this);
	}

}
 
 

异常处理类  -------------------------- ↑

注册异常处理类  ↓

public class MyApplication extends Application {
	
	@Override
	public void onCreate() {
		// TODO Auto-generated method stub
		super.onCreate();
		CrashHandler crashHandler=CrashHandler.getInstance();
		crashHandler.init(getApplicationContext());
	}

}

인위적으로 이상을 만들다
protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		Log.i("TTT","onCreatele  。。。。。。。。。。。。。。。。。。");
		findViewById(R.id.btn).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				System.out.println(a.equals("123"));
			}
		});
	}

log 출력
01-02 02:11:55.980: I/TTT(16359):       。。。。。。。。。。。。。。。。。。
01-02 02:11:58.200: I/TTT(16682): onCreatele  。。。。。。。。。。。。。。。。。。
01-02 02:12:44.110: I/TTT(16682):       。。。。。。。。。。。。。。。。。。
01-02 02:12:46.280: I/TTT(17586): onCreatele  。。。。。。。。。。。。。。。。。。

이 demo는 간이 버전으로 자신의 논리를 실현하여 uncaughtException 방법에서 처리해야 합니다...오류 로그 저장 같은 거,,

좋은 웹페이지 즐겨찾기