日志崩溃重新启动

来源:互联网 发布:office办公软件免费版 编辑:程序博客网 时间:2024/05/29 17:59
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent" >  
  5.       
  6.     <TextView   
  7.         android:id="@+id/show"  
  8.         android:layout_width="wrap_content"  
  9.         android:layout_height="wrap_content"  
  10.         android:layout_centerInParent="true"  
  11.         android:text="我要崩溃咯"  
  12.         android:textSize="20sp"/>  
  13.   
  14.     <Button  
  15.         android:id="@+id/btn"  
  16.         android:layout_width="wrap_content"  
  17.         android:layout_height="wrap_content"  
  18.         android:layout_below="@+id/show"  
  19.         android:layout_marginTop="20dp"  
  20.         android:layout_centerHorizontal="true"  
  21.         android:padding="10dp"  
  22.         android:text="点我崩溃" />  
  23.   
  24. </RelativeLayout>  

MainActivity:

[java] view plaincopy
  1. package com.lzy.gesture;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.view.View;  
  6. import android.view.View.OnClickListener;  
  7. import android.widget.Button;  
  8. import android.widget.TextView;  
  9.   
  10. public class MainActivity extends Activity {  
  11.   
  12.     private Button mButton;  
  13.     private TextView mTextView;  
  14.     private MyApplication myApplication;  
  15.   
  16.     @Override  
  17.     protected void onCreate(Bundle savedInstanceState) {  
  18.         super.onCreate(savedInstanceState);  
  19.         setContentView(R.layout.activity_main);  
  20.   
  21.         myApplication = (MyApplication) getApplication();  
  22.         myApplication.init();  
  23.         myApplication.addActivity(this);  
  24.           
  25.         mButton = (Button) findViewById(R.id.btn);  
  26.         mTextView = (TextView) findViewById(R.id.show);  
  27.   
  28.         mButton.setOnClickListener(new OnClickListener() {  
  29.   
  30.             @Override  
  31.             public void onClick(View v) {  
  32.                 pressed();  
  33.             }  
  34.   
  35.         });  
  36.     }  
  37. //人工制造异常崩溃  
  38.     private void pressed() {  
  39.         new Thread(new Runnable() {  
  40.               
  41.             @Override  
  42.             public void run() {  
  43.                 mTextView.setText("beng...");  
  44.             }  
  45.         }).start();  
  46.   
  47.     }  
  48. }  

MyApplication:在启动APP的时候一直开启application用于监听异常

[java] view plaincopy
  1. package com.lzy.gesture;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.List;  
  5.   
  6. import android.app.Activity;  
  7. import android.app.Application;  
  8.   
  9. public class MyApplication extends Application {  
  10.       
  11.     List<Activity> list = new ArrayList<Activity>();  
  12.       
  13.     public void init(){    
  14.         //设置该CrashHandler为程序的默认处理器      
  15.         MyUncaughtExceptionHandler catchException = new MyUncaughtExceptionHandler(this);    
  16.         Thread.setDefaultUncaughtExceptionHandler(catchException);     
  17.     }    
  18.         
  19.     /**  
  20.      * Activity关闭时,删除Activity列表中的Activity对象*/    
  21.     public void removeActivity(Activity a){    
  22.         list.remove(a);    
  23.     }    
  24.         
  25.     /**  
  26.      * 向Activity列表中添加Activity对象*/    
  27.     public void addActivity(Activity a){    
  28.         list.add(a);    
  29.     }    
  30.         
  31.     /**  
  32.      * 关闭Activity列表中的所有Activity*/    
  33.     public void finishActivity(){    
  34.         for (Activity activity : list) {      
  35.             if (null != activity) {      
  36.                 activity.finish();      
  37.             }      
  38.         }    
  39.         //杀死该应用进程    
  40.        android.os.Process.killProcess(android.os.Process.myPid());      
  41.     }    
  42. }  
  43. package com.lzy.gesture;  
  44.   
  45. import java.util.ArrayList;  
  46. import java.util.List;  
  47.   
  48. import android.app.Activity;  
  49. import android.app.Application;  
  50.   
  51. public class MyApplication extends Application {  
  52.       
  53.     List<Activity> list = new ArrayList<Activity>();  
  54.       
  55.     public void init(){    
  56.         //设置该CrashHandler为程序的默认处理器      
  57.         MyUncaughtExceptionHandler catchException = new MyUncaughtExceptionHandler(this);    
  58.         Thread.setDefaultUncaughtExceptionHandler(catchException);     
  59.     }    
  60.         
  61.     /**  
  62.      * Activity关闭时,删除Activity列表中的Activity对象*/    
  63.     public void removeActivity(Activity a){    
  64.         list.remove(a);    
  65.     }    
  66.         
  67.     /**  
  68.      * 向Activity列表中添加Activity对象*/    
  69.     public void addActivity(Activity a){    
  70.         list.add(a);    
  71.     }    
  72.         
  73.     /**  
  74.      * 关闭Activity列表中的所有Activity*/    
  75.     public void finishActivity(){    
  76.         for (Activity activity : list) {      
  77.             if (null != activity) {      
  78.                 activity.finish();      
  79.             }      
  80.         }    
  81.         //杀死该应用进程    
  82.        android.os.Process.killProcess(android.os.Process.myPid());      
  83.     }    
  84. }  

MyUncaughtExceptionHandler:处理崩溃异常

[java] view plaincopy
  1. package com.lzy.gesture;  
  2.   
  3. import java.lang.Thread.UncaughtExceptionHandler;  
  4.   
  5. import android.app.AlarmManager;  
  6. import android.app.PendingIntent;  
  7. import android.content.Context;  
  8. import android.content.Intent;  
  9. import android.os.Looper;  
  10. import android.widget.Toast;  
  11.   
  12. public class MyUncaughtExceptionHandler implements UncaughtExceptionHandler {  
  13.   
  14.     private MyApplication myApplication;  
  15.     private Thread.UncaughtExceptionHandler mUncaughtExceptionHandler;  
  16.       
  17.     public MyUncaughtExceptionHandler(MyApplication myApplication) {  
  18.         this.myApplication = myApplication;  
  19.         mUncaughtExceptionHandler = Thread.getDefaultUncaughtExceptionHandler();// 获取系统默认的异常处理器  
  20.     }  
  21.       
  22.     @Override  
  23.     public void uncaughtException(Thread thread, Throwable ex) {  
  24.         if (!handleException(ex) && mUncaughtExceptionHandler != null) {  
  25.             //如果用户没有处理则让系统默认的异常处理器来处理     
  26.             mUncaughtExceptionHandler.uncaughtException(thread, ex);  
  27.         }else {  
  28.             try {  
  29.                 Thread.sleep(2000);  
  30.             } catch (InterruptedException e) {  
  31.                 e.printStackTrace();  
  32.             }  
  33.             Intent intent = new Intent(myApplication.getApplicationContext(), MainActivity.class);  
  34.             //重启应用,得使用PendingIntent  
  35.             PendingIntent restartIntent = PendingIntent.getActivity(      
  36.                     myApplication.getApplicationContext(), 0, intent,      
  37.                     Intent.FLAG_ACTIVITY_NEW_TASK);                                                   
  38.             //退出程序                                            
  39.             AlarmManager mAlarmManager = (AlarmManager) myApplication.getSystemService(Context.ALARM_SERVICE);      
  40.             mAlarmManager.set(AlarmManager.RTC, System.currentTimeMillis() + 1000,      
  41.                     restartIntent); // 1秒钟后重启应用     
  42.             myApplication.finishActivity();   
  43.         }  
  44.     }  
  45.   
  46.     /**   
  47.      * 自定义错误处理,收集错误信息 发送错误报告等操作均在此完成.   
  48.      *    
  49.      * @param ex   
  50.      * @return true:如果处理了该异常信息;否则返回false.   
  51.      */      
  52.     private boolean handleException(Throwable ex) {      
  53.         if (ex == null) {      
  54.             return false;      
  55.         }      
  56.         //使用Toast来显示异常信息      
  57.         new Thread(){      
  58.             @Override      
  59.             public void run() {      
  60.                 Looper.prepare();      
  61.                 Toast.makeText(myApplication.getApplicationContext(), "很抱歉,程序出现异常,一秒钟后重启.",     
  62.                         Toast.LENGTH_SHORT).show();      
  63.                 Looper.loop();      
  64.             }     
  65.         }.start();      
  66.         return true;      
  67.     }      
  68. }  

刚开始碰到了

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了。如下:

[html] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     package="com.lzy.gesture"  
  4.     android:versionCode="1"  
  5.     android:versionName="1.0" >  
  6.   
  7.     <uses-sdk  
  8.         android:minSdkVersion="8"  
  9.         android:targetSdkVersion="21" />  
  10.   
  11.     <application  
  12.         android:allowBackup="true"  
  13.         android:icon="@drawable/ic_launcher"  
  14.         android:label="@string/app_name"  
  15.         android:theme="@style/AppTheme"   
  16.         android:name="com.lzy.gesture.MyApplication">  
  17.         <activity  
  18.             android:name=".MainActivity"  
  19.             android:label="@string/app_name" >  
  20.             <intent-filter>  
  21.                 <action android:name="android.intent.action.MAIN" />  
  22.   
  23.                 <category android:name="android.intent.category.LAUNCHER" />  
  24.             </intent-filter>  
  25.         </activity>  
  26.     </application>  
  27.   
  28. </manifest>  
0 0
原创粉丝点击