Android崩溃后重启App心得
2016-01-27 09:57
447 查看
昨天接到需求要求,App崩溃后要进行统计重启,搜索了下, 主要就是application 继承Thread.UncaughtExceptionHandler
实现 uncaughtException(final Thread thread, final Throwable ex) 方法 在里面对ex进行判断,不为空的情况就可以处理了.
按照百度里面一些例子尝试,发现单纯的Intent是没有在崩溃后唤起App的,后来尝试了PendingIntent 是可行的.
但是执行了这个代码后,会发现崩溃的app处于黑屏,卡死状态,知道ANR,后来查阅了一下,我们如果想重启我们的自己的app 需要杀掉原进程,不然android不允许同名称的两个进程存在.
所以在上面代码之后必须加载一句话..
这样就ok了 .
为什么Intent不ok, 我们来一起看看PendingIntent 的解释
http://my.oschina.net/u/242041/blog/206710
附加一个开源中国的详细链接,这里就不多做解释了
Demo 下载地址:
http://download.csdn.net/detail/doudou_1117/9419261
实现 uncaughtException(final Thread thread, final Throwable ex) 方法 在里面对ex进行判断,不为空的情况就可以处理了.
按照百度里面一些例子尝试,发现单纯的Intent是没有在崩溃后唤起App的,后来尝试了PendingIntent 是可行的.
Intent intent = new Intent(application.getApplicationContext(), MainActivity.class); PendingIntent restartIntent = PendingIntent.getActivity( application.getApplicationContext(), 0, intent, Intent.FLAG_ACTIVITY_NEW_TASK); //退出程序 AlarmManager mgr = (AlarmManager)application.getSystemService(Context.ALARM_SERVICE); mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 1000, restartIntent); // 1秒钟后重启应用
但是执行了这个代码后,会发现崩溃的app处于黑屏,卡死状态,知道ANR,后来查阅了一下,我们如果想重启我们的自己的app 需要杀掉原进程,不然android不允许同名称的两个进程存在.
所以在上面代码之后必须加载一句话..
// 杀死该应用进程 android.os.Process.killProcess(android.os.Process.myPid());
这样就ok了 .
为什么Intent不ok, 我们来一起看看PendingIntent 的解释
http://my.oschina.net/u/242041/blog/206710
附加一个开源中国的详细链接,这里就不多做解释了
Demo 下载地址:
http://download.csdn.net/detail/doudou_1117/9419261
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- Android IPC进程间通讯机制
- Android Manifest 用法
- [转载]Activity中ConfigChanges属性的用法
- Android之获取手机上的图片和视频缩略图thumbnails
- Android之使用Http协议实现文件上传功能
- Android学习笔记(二九):嵌入浏览器
- android string.xml文件中的整型和string型代替
- i-jetty环境搭配与编译
- android之定时器AlarmManager
- android wifi 无线调试
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- android 代码实现控件之间的间距
- android FragmentPagerAdapter的“标准”配置
- Android"解决"onTouch和onClick的冲突问题
- android:installLocation简析
- android searchView的关闭事件
- SourceProvider.getJniDirectories