昨天接到需求要求,App崩溃后要进行统计重启,搜索了下, 主要就是application 继承Thread.UncaughtExceptionHandler
实现 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秒钟后重启应用
所以在上面代码之后必须加载一句话..
// 杀死该应用进程
android.os.Process.killProcess(android.os.Process.myPid());
为什么Intent不ok, 我们来一起看看PendingIntent 的解释
http://my.oschina.net/u/242041/blog/206710
附加一个开源中国的详细链接,这里就不多做解释了
Demo 下载地址:
http://download.csdn.net/detail/doudou_1117/9419261
本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。