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之间传递对象的开销 ,这些数据一般都会保存到持久化存储 ...
随机推荐
- 如何关闭CBox(2.4版本号)强制升级的形式
从今天开始2.4.0.9版本号CBox,提示检测到新的版本号,能够使用后必须更新为新版本号,提示表见下面的例子. 此次升级是强制升级.假如你选择不升级(单击窗体上的升级提示右下角"辍学but ...
- java Map 之 排序(key,value)
一:起因: (1)现实中须要Map容器进行排序的情况非常多非常多:由于Map<key,value>键值对的存储结构特别是HashMap的结构是非常优秀的,数据存储就难免对其进行排序: (2 ...
- 第12章 代理模式(Proxy Pattern)
原文 第12章 代理模式(Proxy Pattern) 代理模式 概述: 在软件系统中,有些对象有时候由于跨越网络或者其他的障碍,而不能够或者不想直接访问另一个对象,如果直接访问会给系统带来不必要 ...
- POJ 2352 && HDU 1541 Stars (树状数组)
一開始想,总感觉是DP,但是最后什么都没想到.还暴力的交了一发. 然后開始写线段树,结果超时.感觉自己线段树的写法有问题.改天再写.先把树状数组的写法贴出来吧. ~~~~~~~~~~~~~~~~~~~ ...
- A hard puzzle 1097
Problem Description lcy gives a hard puzzle to feng5166,lwg,JGShining and Ignatius: gave a and b,how ...
- Samza/KafkaAnalysizing
Apache Samza is a distributed stream processing framework. It uses Apache Kafka for messaging, and A ...
- RPC和RMI的区别(Difference Between RPC and RMI)
RPC和RMI的区别(Difference Between RPC and RMI) RPC vs RMI RPC (Remote Procedure Call) and RMI (Remote Me ...
- CSS3+HTML5特效4 - 横向无缝滚动
先看例子 This is a test 1. This is a test 2. This is a test 3. This is a test 4. This is a test 5. This ...
- php设计模式(一):简介及创建型模式
我们分三篇文章来总结一下设计模式在PHP中的应用,这是第一篇创建型模式. 一.设计模式简介 首先我们来认识一下什么是设计模式: 设计模式是一套被反复使用.容易被他人理解的.可靠的代码设计经验的总结. ...
- HDU 3683 模拟&搜索
给出五子棋残局,推断三步内能否分出胜负,玩家为当前该走旗子的颜色,下一步为白棋或黑棋不定. 依照顺序推断就可以: 1:推断棋盘是否合法,并确定玩家颜色 2:推断当前玩家颜色是否有一个必胜点,有玩家则在 ...