Android借助Application重写App的Crash(简易版)
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(简易版)的更多相关文章
- android 在线升级借助开源中国App源码
android 在线升级借助开源中国App源码 http://www.cnblogs.com/luomingui/p/3949429.html android 在线升级借助开源中国App源码分析如下: ...
- Android开发之创建App Widget和更新Widget内容
App WidgetsApp Widgets are miniature application views that can be embedded in other applications (s ...
- 【转载】Android使用Application总结
Application 配置全局Context 第一步.写一个全局的单例模式的MyApplication继承自Application 覆盖onCreate ,在这个方法里面实例化Application ...
- Android学习之路——简易版微信为例(三)
最近好久没有更新博文,一则是因为公司最近比较忙,另外自己在Android学习过程和简易版微信的开发过程中碰到了一些绊脚石,所以最近一直在学习充电中.下面来列举一下自己所走过的弯路: (1)本来打算前端 ...
- Android学习之路——简易版微信为例(二)
1 概述 从这篇博文开始,正式进入简易版微信的开发.深入学习前,想谈谈个人对Android程序开发一些理解,不一定正确,只是自己的一点想法.Android程序开发不像我们在大学时候写C控制台程序那样, ...
- Android学习之路——简易版微信为例(一)
这是“Android学习之路”系列文章的开篇,可能会让大家有些失望——这篇文章中我们不介绍简易版微信的实现(不过不是标题党哦,我会在后续博文中一步步实现这个应用程序的).这里主要是和广大园友们聊聊一个 ...
- Android 使用Application总结
Application 配置全局Context 第一步.写一个全局的单例模式的MyApplication继承自Application 覆盖onCreate ,在这个方法里面实例化Application ...
- Android采用Application总结一下
什么是 Application Application和Activity,Service由于是android框架的一个系统组件,当android程序启动时系统会创建一个 application对象.用 ...
- 【安卓开发】为什么不能往Android的Application对象里存储数据
在一个App里面总有一些数据需要在多个地方用到.这些数据可能是一个 session token,一次费时计算的结果等.通常为了避免activity之间传递对象的开销 ,这些数据一般都会保存到持久化存储 ...
随机推荐
- u_boot启动过程中的具体分析(1)
闭上眼睛,细致的回顾一下从NAND FLASH 启动的整个流程,首先,当我们打开板子的时候,先执行的就是嵌入在芯片上的iROM,它的作用就是为了把.NAND Flash 中的bootloader的一部 ...
- HDU 4123 Bob’s Race 树的直径+单调队列
题意: 给定n个点的带边权树Q个询问. 以下n-1行给出树 以下Q行每行一个数字表示询问. 首先求出dp[N] :dp[i]表示i点距离树上最远点的距离 询问u, 表示求出 dp 数组中最长的连续序列 ...
- JavaScript的类型、值和变量的总结
前言:JavaScript的数据类型分为两类:原始类型和对象类型.5种原始类型:数字.字符串.布尔值.null(空).undefined(未定义).对象是属性的集合,每个属性都由“名/值对”(值可以是 ...
- iOS 8中CLLocationManager及MKMapView showUserLocation失败的解决的方法
用XCode 6编译的原来XCode 5.1.1写的程序时,发现原来写的CLLocationManager定位的代码以及MKmapView的showUserLocation失效.查了一下,XCode ...
- keyboard splitting bug on ipad with ios 5 and 6 (Cocos2d-x)
Had the same issue - the solution is to stop the opengl layer from rendering while this is happening ...
- windows平台下载android源代码
最近观看<android核心分析>,所以很多细节都没有详细看代码很难理解.请记住,印象不深.感觉是最好再一起去的源代码,返回下载android源代码,遇到了许多问题,最后开始下载.合并流程 ...
- [cocos2dx注意事项009]试用quick-cocos2dx-2.2.4
quick-cocos2d-x 在中国站http://quick.cocoachina.com/?page_id=40,用他们自己的话说:quick-cocos2d-x(后文简称 quick)与 co ...
- Codeforces 135A-Replacement(思维)
A. Replacement time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...
- 网络资源(4) - extJS视频
2014_08_24 http://v.youku.com/v_show/id_XMjk2ODc0MjA4.html?f=7183617 extJS视频教程04——ExtJS框架入门
- 解决Unity3d 4.3 动画系统带来的烦恼
近期有非常多同学问我关于unity3d 4.3更新之后动画系统和曾经不一样了,并且之前用的非常熟练的创建动画和修修改画非常多操作都不好用了,那么在这里和大家分享一下三杀的个人经验,方便大家使用unit ...