MainActivity如下:

package cn.testcrash;
import android.app.Activity;
import android.os.Bundle;
/**
* Demo描述:
* 借助于Application自定义Crash
*
* 参考资料:
* 1 http://blog.csdn.net/xiaanming/article/details/9344703
* 2 http://blog.csdn.net/itachi85/article/details/9102021
*/
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如下:

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

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) {
//保存错误日志到SD卡
Utils.saveInfoToSDCard(mContext, throwable);
//提示Crash信息
showCrashTipToast();
try {
Thread.sleep(3000);
} catch (Exception e) {
}
//退出应用
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如下:

package cn.testcrash;
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 saveInfoToSDCard(Context context, Throwable throwable) {
HashMap<String, String> hashMap=getBaseInfo(context);
StringBuilder stringBuilder=new StringBuilder();
for(Map.Entry<String, String> entry:hashMap.entrySet()){
String key=entry.getKey();
String value=entry.getValue();
stringBuilder.append(key).append("=").append(value).append("\n");
}
System.out.println("stringBuilder.toString()如下:"+"\n"+stringBuilder.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;
} private String getCurrentTime(){
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);
System.out.println("currentTime="+currentTime);
return currentTime;
} }

main.xml如下:

<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>

AndroidManifest.xml如下:

<?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>

Android借助Application重写App的Crash(简易版)的更多相关文章

  1. android 在线升级借助开源中国App源码

    android 在线升级借助开源中国App源码 http://www.cnblogs.com/luomingui/p/3949429.html android 在线升级借助开源中国App源码分析如下: ...

  2. Android开发之创建App Widget和更新Widget内容

    App WidgetsApp Widgets are miniature application views that can be embedded in other applications (s ...

  3. 【转载】Android使用Application总结

    Application 配置全局Context 第一步.写一个全局的单例模式的MyApplication继承自Application 覆盖onCreate ,在这个方法里面实例化Application ...

  4. Android学习之路——简易版微信为例(三)

    最近好久没有更新博文,一则是因为公司最近比较忙,另外自己在Android学习过程和简易版微信的开发过程中碰到了一些绊脚石,所以最近一直在学习充电中.下面来列举一下自己所走过的弯路: (1)本来打算前端 ...

  5. Android学习之路——简易版微信为例(二)

    1 概述 从这篇博文开始,正式进入简易版微信的开发.深入学习前,想谈谈个人对Android程序开发一些理解,不一定正确,只是自己的一点想法.Android程序开发不像我们在大学时候写C控制台程序那样, ...

  6. Android学习之路——简易版微信为例(一)

    这是“Android学习之路”系列文章的开篇,可能会让大家有些失望——这篇文章中我们不介绍简易版微信的实现(不过不是标题党哦,我会在后续博文中一步步实现这个应用程序的).这里主要是和广大园友们聊聊一个 ...

  7. Android 使用Application总结

    Application 配置全局Context 第一步.写一个全局的单例模式的MyApplication继承自Application 覆盖onCreate ,在这个方法里面实例化Application ...

  8. Android采用Application总结一下

    什么是 Application Application和Activity,Service由于是android框架的一个系统组件,当android程序启动时系统会创建一个 application对象.用 ...

  9. 【安卓开发】为什么不能往Android的Application对象里存储数据

    在一个App里面总有一些数据需要在多个地方用到.这些数据可能是一个 session token,一次费时计算的结果等.通常为了避免activity之间传递对象的开销 ,这些数据一般都会保存到持久化存储 ...

随机推荐

  1. NSIS:在注册表中记录安装路径以便重装或升级时读取

    原文 NSIS:在注册表中记录安装路径以便重装或升级时读取 在NSIS中,这个功能是非常有用的,可以避免用户把程序安装到多个位置的尴尬. 第1步:在“安装目录选择页面”前面加入以下代码: 1 !def ...

  2. 前端插件@user

    分享一个 @user 前端插件   开源地址:https://github.com/yuezhongxin/Mention.js 插件效果:类似于微博或 github 中 @user 列表效果. 这是 ...

  3. 在CentOS 7上安装phpMyAdmin

    原文 在CentOS 7上安装phpMyAdmin phpMyAdmin是一款以PHP为基础,基于Web的MySQL/MariaDB数据库管理工具.虽然已经存在着一些诸如Adminer的轻量级数据库管 ...

  4. tmp1

    program1: Line # Mem usage Increment Line Contents================================================ 2 ...

  5. Linux centos 主机名颜色设置 和 别名设置

    方便和乐趣写今天.至于为什么主机名颜色设置 和 别名设置放在一起写.这是因为他们的设置是在一个文件中..bashrc. .bashrc放在cd /root 这个文件夹下! 这个文件主要保存个人的一些个 ...

  6. IOS ARC和非ARC文件混用

    ARC在SDK4.0的时候增加的,因为要和曾经的项目融合,就会有arc和非arc文件的混合. 当然,也就这两种情况: 1.自己的旧项目没有使用ARC,可是引入的第三方库却是使用了ARC的. 2.自己的 ...

  7. js中frame的操作问题

    这里以图为例,在这里把frame之间的互相操作简单列为:1变量2方法3页面之间元素的互相获取. A  首先从 父(frameABC)------->子(frameA,frameB,frameC) ...

  8. SQL Server编程系列(1):SMO介绍

    原文:SQL Server编程系列(1):SMO介绍 续篇:SQL Server编程系列(2):SMO常用对象的有关操作 最近在项目中用到了有关SQL Server管理任务方面的编程实现,有了一些自己 ...

  9. poj 2449 Remmarguts&#39; Date 【SPFA+Astar】【古典】

    称号:poj 2449 Remmarguts' Date 意甲冠军:给定一个图,乞讨k短路. 算法:SPFA求最短路 + AStar 以下引用大牛的分析: 首先,为了说话方便,列出一些术语: 在启示式 ...

  10. LinkedBlockingQueue多线程测试

    public class FillQueueThread extends Thread { private Queue queue; public FillQueueThread(Queue queu ...