- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent" >
- <TextView
- android:id="@+id/show"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_centerInParent="true"
- android:text="我要崩溃咯"
- android:textSize="20sp"/>
- <Button
- android:id="@+id/btn"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_below="@+id/show"
- android:layout_marginTop="20dp"
- android:layout_centerHorizontal="true"
- android:padding="10dp"
- android:text="点我崩溃" />
- </RelativeLayout>
MainActivity:
- package com.lzy.gesture;
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.TextView;
- public class MainActivity extends Activity {
- private Button mButton;
- private TextView mTextView;
- private MyApplication myApplication;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- myApplication = (MyApplication) getApplication();
- myApplication.init();
- myApplication.addActivity(this);
- mButton = (Button) findViewById(R.id.btn);
- mTextView = (TextView) findViewById(R.id.show);
- mButton.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- pressed();
- }
- });
- }
- //人工制造异常崩溃
- private void pressed() {
- new Thread(new Runnable() {
- @Override
- public void run() {
- mTextView.setText("beng...");
- }
- }).start();
- }
- }
MyApplication:在启动APP的时候一直开启application用于监听异常
- package com.lzy.gesture;
- import java.util.ArrayList;
- import java.util.List;
- import android.app.Activity;
- import android.app.Application;
- public class MyApplication extends Application {
- List<Activity> list = new ArrayList<Activity>();
- public void init(){
- //设置该CrashHandler为程序的默认处理器
- MyUncaughtExceptionHandler catchException = new MyUncaughtExceptionHandler(this);
- Thread.setDefaultUncaughtExceptionHandler(catchException);
- }
- /**
- * Activity关闭时,删除Activity列表中的Activity对象*/
- public void removeActivity(Activity a){
- list.remove(a);
- }
- /**
- * 向Activity列表中添加Activity对象*/
- public void addActivity(Activity a){
- list.add(a);
- }
- /**
- * 关闭Activity列表中的所有Activity*/
- public void finishActivity(){
- for (Activity activity : list) {
- if (null != activity) {
- activity.finish();
- }
- }
- //杀死该应用进程
- android.os.Process.killProcess(android.os.Process.myPid());
- }
- }
- package com.lzy.gesture;
- import java.util.ArrayList;
- import java.util.List;
- import android.app.Activity;
- import android.app.Application;
- public class MyApplication extends Application {
- List<Activity> list = new ArrayList<Activity>();
- public void init(){
- //设置该CrashHandler为程序的默认处理器
- MyUncaughtExceptionHandler catchException = new MyUncaughtExceptionHandler(this);
- Thread.setDefaultUncaughtExceptionHandler(catchException);
- }
- /**
- * Activity关闭时,删除Activity列表中的Activity对象*/
- public void removeActivity(Activity a){
- list.remove(a);
- }
- /**
- * 向Activity列表中添加Activity对象*/
- public void addActivity(Activity a){
- list.add(a);
- }
- /**
- * 关闭Activity列表中的所有Activity*/
- public void finishActivity(){
- for (Activity activity : list) {
- if (null != activity) {
- activity.finish();
- }
- }
- //杀死该应用进程
- android.os.Process.killProcess(android.os.Process.myPid());
- }
- }
MyUncaughtExceptionHandler:处理崩溃异常
- package com.lzy.gesture;
- import java.lang.Thread.UncaughtExceptionHandler;
- import android.app.AlarmManager;
- import android.app.PendingIntent;
- import android.content.Context;
- import android.content.Intent;
- import android.os.Looper;
- import android.widget.Toast;
- public class MyUncaughtExceptionHandler implements UncaughtExceptionHandler {
- private MyApplication myApplication;
- private Thread.UncaughtExceptionHandler mUncaughtExceptionHandler;
- public MyUncaughtExceptionHandler(MyApplication myApplication) {
- this.myApplication = myApplication;
- mUncaughtExceptionHandler = Thread.getDefaultUncaughtExceptionHandler();// 获取系统默认的异常处理器
- }
- @Override
- public void uncaughtException(Thread thread, Throwable ex) {
- if (!handleException(ex) && mUncaughtExceptionHandler != null) {
- //如果用户没有处理则让系统默认的异常处理器来处理
- mUncaughtExceptionHandler.uncaughtException(thread, ex);
- }else {
- try {
- Thread.sleep(2000);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- Intent intent = new Intent(myApplication.getApplicationContext(), MainActivity.class);
- //重启应用,得使用PendingIntent
- PendingIntent restartIntent = PendingIntent.getActivity(
- myApplication.getApplicationContext(), 0, intent,
- Intent.FLAG_ACTIVITY_NEW_TASK);
- //退出程序
- AlarmManager mAlarmManager = (AlarmManager) myApplication.getSystemService(Context.ALARM_SERVICE);
- mAlarmManager.set(AlarmManager.RTC, System.currentTimeMillis() + 1000,
- restartIntent); // 1秒钟后重启应用
- myApplication.finishActivity();
- }
- }
- /**
- * 自定义错误处理,收集错误信息 发送错误报告等操作均在此完成.
- *
- * @param ex
- * @return true:如果处理了该异常信息;否则返回false.
- */
- private boolean handleException(Throwable ex) {
- if (ex == null) {
- return false;
- }
- //使用Toast来显示异常信息
- new Thread(){
- @Override
- public void run() {
- Looper.prepare();
- Toast.makeText(myApplication.getApplicationContext(), "很抱歉,程序出现异常,一秒钟后重启.",
- Toast.LENGTH_SHORT).show();
- Looper.loop();
- }
- }.start();
- return true;
- }
- }
刚开始碰到了
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.lzy.gesture/com.lzy.gesture.MainActivity}: java.lang.ClassCastException: android.app.Application cannot be cast to com.lzy.gesture.MyApplication
的报错,原来是忘了在manifast中application节点添加MyApplication了。如下:
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.lzy.gesture"
- android:versionCode="1"
- android:versionName="1.0" >
- <uses-sdk
- android:minSdkVersion="8"
- android:targetSdkVersion="21" />
- <application
- android:allowBackup="true"
- android:icon="@drawable/ic_launcher"
- android:label="@string/app_name"
- android:theme="@style/AppTheme"
- android:name="com.lzy.gesture.MyApplication">
- <activity
- android:name=".MainActivity"
- android:label="@string/app_name" >
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
- </application>
- </manifest>