Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android借助Application重寫App的Crash(完整版)

Android借助Application重寫App的Crash(完整版)

編輯:關於Android編程

MainActivity如下:

[java]
package cn.testcrash; 
import android.app.Activity; 
import android.os.Bundle; 
/**
 * Demo描述:
 * 借助於Application自定義Crash
 * 
 * 備注說明:
 * 1 在獲取Crash信息後可依據業務上傳服務器或者保存至SDCard
 * 2 在uncaughtException()中的第三步——退出程序.此處只是做了
 *   最簡單的處理.完備的操作請參見:
 *   http://blog.csdn.net/lfdfhl/article/details/9261147
 *   http://blog.csdn.net/lfdfhl/article/details/9260605
 *   
 * 參考資料:
 * 1 http://blog.csdn.net/xiaanming/article/details/9344703
 * 2 http://blog.csdn.net/itachi85/article/details/9102021
 * Thank you very much
 */ 
public class MainActivity extends Activity { 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main); 
        init(); 
    } 
    //Crash  
    private void init(){ 
        System.out.println((9727/0)+""); 
    } 

package cn.testcrash;
import android.app.Activity;
import android.os.Bundle;
/**
 * Demo描述:
 * 借助於Application自定義Crash
 *
 * 備注說明:
 * 1 在獲取Crash信息後可依據業務上傳服務器或者保存至SDCard
 * 2 在uncaughtException()中的第三步——退出程序.此處只是做了
 *   最簡單的處理.完備的操作請參見:
 *   http://blog.csdn.net/lfdfhl/article/details/9261147
 *   http://blog.csdn.net/lfdfhl/article/details/9260605
 *  
 * 參考資料:
 * 1 http://blog.csdn.net/xiaanming/article/details/9344703
 * 2 http://blog.csdn.net/itachi85/article/details/9102021
 * Thank you very much
 */
public class MainActivity extends Activity {
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  init();
 }
 //Crash
 private void init(){
  System.out.println((9727/0)+"");
 }
}

CrashApplication如下:

[java]
package cn.testcrash; 
import android.app.Application; 
 
public class CrashApplication extends Application { 
    @Override 
    public void onCreate() { 
        super.onCreate(); 
        CrashHandler crashHandler=CrashHandler.getInstance(); 
        //指定Crash時的處理程序  
        crashHandler.setCrashHandler(getApplicationContext()); 
    } 

package cn.testcrash;
import android.app.Application;

public class CrashApplication extends Application {
 @Override
 public void onCreate() {
  super.onCreate();
  CrashHandler crashHandler=CrashHandler.getInstance();
  //指定Crash時的處理程序
  crashHandler.setCrashHandler(getApplicationContext());
 }
}

CrashHandler如下:

[java]
package cn.testcrash; 
import java.lang.Thread.UncaughtExceptionHandler; 
import android.content.Context; 
import android.os.Looper; 
import android.widget.Toast; 
public class CrashHandler implements UncaughtExceptionHandler { 
    private Context mContext; 
    private static CrashHandler mCrashHandler=new CrashHandler(); 
     
    public static CrashHandler getInstance(){ 
        return mCrashHandler; 
    } 
     
    /**
     * 設置當線程由於未捕獲到異常而突然終止的默認處理程序。
     */ 
    public void setCrashHandler(Context context){ 
        mContext=context; 
        Thread.setDefaultUncaughtExceptionHandler(this); 
    } 
     
    /**
     * 當發生Crash時調用該方法
     */ 
    @Override 
    public void uncaughtException(Thread thread, Throwable throwable) { 
         //1 保存錯誤日志到SD卡  
         Utils.saveCrashInfoToSDCard(mContext, throwable); 
         //2 提示Crash信息  
         showCrashTipToast(); 
         try { 
            Thread.sleep(3000); 
        } catch (Exception e) { 
        } 
         //3 退出應用  
         System.exit(0); 
    } 
     
    private void showCrashTipToast() { 
        new Thread(new Runnable() { 
            @Override 
            public void run() { 
                Looper.prepare(); 
                Toast.makeText(mContext, "I am very sorry", Toast.LENGTH_LONG).show(); 
                Looper.loop(); 
            } 
        }).start(); 
    } 
 

package cn.testcrash;
import java.lang.Thread.UncaughtExceptionHandler;
import android.content.Context;
import android.os.Looper;
import android.widget.Toast;
public class CrashHandler implements UncaughtExceptionHandler {
 private Context mContext;
 private static CrashHandler mCrashHandler=new CrashHandler();
 
 public static CrashHandler getInstance(){
  return mCrashHandler;
 }
 
 /**
  * 設置當線程由於未捕獲到異常而突然終止的默認處理程序。
  */
 public void setCrashHandler(Context context){
  mContext=context;
  Thread.setDefaultUncaughtExceptionHandler(this);
 }
 
 /**
  * 當發生Crash時調用該方法
  */
 @Override
 public void uncaughtException(Thread thread, Throwable throwable) {
   //1 保存錯誤日志到SD卡
         Utils.saveCrashInfoToSDCard(mContext, throwable);
         //2 提示Crash信息
         showCrashTipToast();
         try {
   Thread.sleep(3000);
  } catch (Exception e) {
  }
         //3 退出應用
         System.exit(0);
 }
 
 private void showCrashTipToast() {
  new Thread(new Runnable() {
   @Override
   public void run() {
    Looper.prepare();
    Toast.makeText(mContext, "I am very sorry", Toast.LENGTH_LONG).show();
    Looper.loop();
   }
  }).start();
 }

}

Utils如下:

[java]
package cn.testcrash; 
import java.io.PrintWriter; 
import java.io.StringWriter; 
import java.text.SimpleDateFormat; 
import java.util.Date; 
import java.util.HashMap; 
import java.util.Map; 
import java.util.TimeZone; 
import android.content.Context; 
import android.content.pm.PackageInfo; 
import android.content.pm.PackageManager; 
import android.content.pm.PackageManager.NameNotFoundException; 
import android.os.Build; 
 
public class Utils { 
 
    public static void saveCrashInfoToSDCard(Context context, Throwable throwable) { 
        StringBuilder crashInfoStringBuilder=new StringBuilder(); 
         
        //獲取Crash時間  
        String crashTime=getCrashTime(); 
        crashInfoStringBuilder.append("------------------"+"\n"); 
        crashInfoStringBuilder.append(crashTime+"\n"); 
        crashInfoStringBuilder.append("------------------"+"\n"); 
         
        //獲取Crash時設備及該App的基本信息  
        HashMap<String, String> hashMap=getBaseInfo(context); 
        for(Map.Entry<String, String> entry:hashMap.entrySet()){ 
            String key=entry.getKey(); 
            String value=entry.getValue(); 
            crashInfoStringBuilder.append(key).append("=").append(value).append("\n"); 
        } 
        crashInfoStringBuilder.append("------------------"+"\n"); 
         
        //獲取導致Crash的時間  
        String uncaughtException=getUncaughtException(throwable); 
        crashInfoStringBuilder.append(uncaughtException+"\n"); 
        crashInfoStringBuilder.append("------------------"+"\n"); 
         
        System.out.println("crashInfo如下:"+"\n"+crashInfoStringBuilder.toString()); 
         
    } 
    /**
     * 獲取設備及該App的基本信息
     */ 
    public static HashMap<String, String> getBaseInfo(Context context){ 
        HashMap<String, String> hashMap = new HashMap<String, String>(); 
        PackageManager packageManager = context.getPackageManager(); 
        PackageInfo packageInfo = null; 
        try { 
            packageInfo = packageManager.getPackageInfo(context.getPackageName(), PackageManager.GET_ACTIVITIES); 
        } catch (NameNotFoundException e) { 
            e.printStackTrace(); 
        } 
         
        hashMap.put("versionName", packageInfo.versionName); 
        hashMap.put("versionCode", packageInfo.versionCode+""); 
         
        hashMap.put("MODEL",  Build.MODEL+""); 
        hashMap.put("SDK_INT",Build.VERSION.SDK_INT+""); 
        hashMap.put("RELEASE",Build.VERSION.RELEASE+""); 
        hashMap.put("PRODUCT",Build.PRODUCT+""); 
         
         
        return hashMap; 
    } 
    /**
     * 獲取造成Crash的異常的具體信息
     */ 
    public static String getUncaughtException(Throwable throwable){ 
        StringWriter stringWriter = new StringWriter(); 
        PrintWriter printWriter = new PrintWriter(stringWriter); 
        throwable.printStackTrace(printWriter); 
        printWriter.close(); 
        String uncaughtException=stringWriter.toString(); 
        return uncaughtException; 
 
    } 
     
    /**
     * 獲取Crash的時間
     */ 
    public static String getCrashTime(){ 
        String currentTime=""; 
        long currentTimeMillis=System.currentTimeMillis(); 
        System.setProperty("user.timezone", "Asia/Shanghai");   
        TimeZone timeZone = TimeZone.getTimeZone("Asia/Shanghai");   
        TimeZone.setDefault(timeZone);   
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss"); 
        Date currentDate=new Date(currentTimeMillis); 
        currentTime = simpleDateFormat.format(currentDate);   
        return currentTime; 
    } 

package cn.testcrash;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.TimeZone;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Build;

public class Utils {

 public static void saveCrashInfoToSDCard(Context context, Throwable throwable) {
  StringBuilder crashInfoStringBuilder=new StringBuilder();
  
  //獲取Crash時間
  String crashTime=getCrashTime();
  crashInfoStringBuilder.append("------------------"+"\n");
  crashInfoStringBuilder.append(crashTime+"\n");
  crashInfoStringBuilder.append("------------------"+"\n");
  
  //獲取Crash時設備及該App的基本信息
  HashMap<String, String> hashMap=getBaseInfo(context);
  for(Map.Entry<String, String> entry:hashMap.entrySet()){
   String key=entry.getKey();
   String value=entry.getValue();
   crashInfoStringBuilder.append(key).append("=").append(value).append("\n");
  }
  crashInfoStringBuilder.append("------------------"+"\n");
  
  //獲取導致Crash的時間
  String uncaughtException=getUncaughtException(throwable);
  crashInfoStringBuilder.append(uncaughtException+"\n");
  crashInfoStringBuilder.append("------------------"+"\n");
  
        System.out.println("crashInfo如下:"+"\n"+crashInfoStringBuilder.toString());
       
 }
 /**
  * 獲取設備及該App的基本信息
  */
 public static HashMap<String, String> getBaseInfo(Context context){
  HashMap<String, String> hashMap = new HashMap<String, String>();
  PackageManager packageManager = context.getPackageManager();
  PackageInfo packageInfo = null;
  try {
   packageInfo = packageManager.getPackageInfo(context.getPackageName(), PackageManager.GET_ACTIVITIES);
  } catch (NameNotFoundException e) {
   e.printStackTrace();
  }
  
  hashMap.put("versionName", packageInfo.versionName);
  hashMap.put("versionCode", packageInfo.versionCode+"");
  
  hashMap.put("MODEL",  Build.MODEL+"");
  hashMap.put("SDK_INT",Build.VERSION.SDK_INT+"");
  hashMap.put("RELEASE",Build.VERSION.RELEASE+"");
  hashMap.put("PRODUCT",Build.PRODUCT+"");
  
  
  return hashMap;
 }
 /**
  * 獲取造成Crash的異常的具體信息
  */
 public static String getUncaughtException(Throwable throwable){
  StringWriter stringWriter = new StringWriter();
  PrintWriter printWriter = new PrintWriter(stringWriter);
  throwable.printStackTrace(printWriter);
  printWriter.close();
  String uncaughtException=stringWriter.toString();
  return uncaughtException;

 }
 
 /**
  * 獲取Crash的時間
  */
 public static String getCrashTime(){
  String currentTime="";
  long currentTimeMillis=System.currentTimeMillis();
  System.setProperty("user.timezone", "Asia/Shanghai"); 
  TimeZone timeZone = TimeZone.getTimeZone("Asia/Shanghai"); 
  TimeZone.setDefault(timeZone); 
  SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
  Date currentDate=new Date(currentTimeMillis);
  currentTime = simpleDateFormat.format(currentDate); 
        return currentTime;
 }
}
 

AndroidManifest.xml如下:

[html]
<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="cn.testcrash" 
    android:versionCode="1" 
    android:versionName="1.0" > 
 
    <uses-sdk 
        android:minSdkVersion="8" 
        android:targetSdkVersion="8" /> 
 
    <application 
        android:name="cn.testcrash.CrashApplication" 
        android:allowBackup="true" 
        android:icon="@drawable/ic_launcher" 
        android:label="@string/app_name" 
        android:theme="@style/AppTheme" > 
        <activity 
            android:name="cn.testcrash.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> 

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="cn.testcrash"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="8" />

    <application
        android:name="cn.testcrash.CrashApplication"
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="cn.testcrash.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>
main.xml如下:

[html]
<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/textView" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="自定義Crash"  
        android:layout_centerInParent="true" 
        android:textSize="28sp" 
    /> 
 
</RelativeLayout> 

<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/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="自定義Crash"
        android:layout_centerInParent="true"
        android:textSize="28sp"
    />

</RelativeLayout>
 

 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved