您的位置:首页 > 移动开发 > Android开发

Android学习记录(二十一)-crash保护处理

2016-06-07 19:14 495 查看
本文基本都是基于两篇blog:
http://blog.csdn.net/cym_lmy/article/details/24704089 http://blog.csdn.net/xiaanming/article/details/9344703/
为什么要列两篇呢,原来的设计是crash后,2s后重启应用。最近说能不能改成直接关闭app.

然后就开始各种方案。
http://stackoverflow.com/questions/3105673/how-to-kill-an-application-with-all-its-activities
这里遇到两个问题:

1.android.os.Process.killProcess(android.os.Process.myPid());

如果二级的activity crash,只会杀当前的activity,并不会关闭整个程序。

2.当crash发生时,下面的方法无法使用。intent无法跳转到目标初始的activity.如果先杀进程,后写intent的代码,intent的代码又不会执行。。。。

11down
vote
When
the user wishes to exit all open activities, they should press a button which loads the first Activity that runs when your app starts, in my case "LoginActivity".
Intent intent = new Intent(getApplicationContext(), LoginActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("EXIT", true);
startActivity(intent);
The
above code clears all the activities except for LoginActivity. LoginActivity is the first activity that is brought up when the user runs the program. Then put this code inside the LoginActivity's onCreate, to signal when it should self destruct when the 'Exit'
message is passed.
if (getIntent().getBooleanExtra("EXIT", false)) {
finish();
}
最后总算在:

http://blog.csdn.net/xiaanming/article/details/9344703/

找到了解决方案。理论上是完全可行的。相当于纪录activity的调用链,一个一个finish。

问题是这样代码里所有的activity都要修改,就为了把重启改成直接关闭,目前项目的意见是暂时不改。。。

以上就是这几天填的坑。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: