activity的crash监听主要分为两部分:
1、Thread.setDefaultUncaughtExceptionHandle监听所有线程的异常
比如:在activity中点击某个null控件造成的crash
2、ActivityThread.mH.mCallback监听主要是监听activity消息范围内的异常
比如:在activity中的onCreate中的null造成的crash
首先这两部分监听是包含关系,1包含2,如果在2中的异常被吃掉,则1中就拦截不到了否则1可以拦截到。
只需要在这两个地方提前拦截掉就不会造成crash(crash被吃掉)
如何吃掉?很简单在它们的回调中写入:
while (true) {// 把crash吃掉,防止异常闪退
try {
Looper.loop();
} catch (Throwable e) {
}
}
第一部分监听简单。第二部分比较复杂,要用hook技术
需要hook部分:
类:android.app.ActivityThread
方法:currentActivityThread
属性:mH
回调:mCallback
Class activityThreadClass = Class.forName("android.app.ActivityThread");
Object activityThread = activityThreadClass.getDeclaredMethod("currentActivityThread").invoke(null);
Field mhField = activityThreadClass.getDeclaredField("mH");
mhField.setAccessible(true);
final Handler mhHandler = (Handler) mhField.get(activityThread);
Field callbackField = Handler.class.getDeclaredField("mCallback");
callbackField.setAccessible(true);
然后就可以callbackField.set(mhHandler,myCall)
在myCall中可以处理activity中的消息了。
具体什么消息可以查看:
ActivityThread.H里的消息常量
我们用到的
final int LAUNCH_ACTIVITY = 100;// launch
final int PAUSE_ACTIVITY = 101;// pause
final int PAUSE_ACTIVITY_FINISHING = 102;//
final int STOP_ACTIVITY_HIDE = 104;
final int RESUME_ACTIVITY = 107;
final int DESTROY_ACTIVITY = 109;
callbackField.set(mhHandler, new Handler.Callback() {
@Override
public boolean handleMessage(Message msg) {
Log.d(TAG,"what:"+msg.what+" msg:"+msg.toString());
switch (msg.what) {
case LAUNCH_ACTIVITY:// startActivity--> activity.attach activity.onCreate r.activity!=null activity.onStart activity.onResume
try {
mhHandler.handleMessage(msg);//正常信息
} catch (Throwable throwable) {//异常信息:eg:启动失败
//在这里利用hook技术杀死当前的activity,决定是否吃掉异常等处理
}
return true;
}
return false;
}
});
Android中的黑屏问题,通过识别Throwable中的信息来判断是否当前黑屏了
if ("android.view.Choreographer".equals(element.getClassName()) &&
"Choreographer.java".equals(element.getFileName())
&& "doFrame".equals(element.getMethodName())) {// 根据多年经验,这里会造成黑屏
}
销毁当前crash的activity
通过hook技术解决不同api版本的兼容处理,抽取:
/**
* Created By George
* Description:
* 规范:api兼容处理具体实现下发,在各个对应的场景中实现杀死当前异常的activity
*/
public interface IActivityKiller {
void finishLaunchActivity(Message message);
void finishResumeActivity(Message message);
void finishPauseActivity(Message message);
void finishStopActivity(Message message);
}
例如:
/**
* Created By George
* Description:
* android 7.0 and 7.1.1
* 杀死当前异常的activity
*/
public class ActivityKillerV24_V25 implements IActivityKiller{
@Override
public void finishLaunchActivity(Message message) {
try {
Object activityClientRecord = message.obj;
Field tokenField = activityClientRecord.getClass().getDeclaredField("token");// hook出token属性
tokenField.setAccessible(true);
IBinder binder = (IBinder) tokenField.get(activityClientRecord);
finish(binder);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void finishResumeActivity(Message message) {
finishSomeArgs(message);
}
@Override
public void finishPauseActivity(Message message) {
finishSomeArgs(message);
}
@Override
public void finishStopActivity(Message message) {
finishSomeArgs(message);
}
private void finish(IBinder binder) throws Exception {
/*
ActivityManagerNative.getDefault()
.finishActivity(r.token, Activity.RESULT_CANCELED, null, Activity.DONT_FINISH_TASK_WITH_ACTIVITY);
*/
Class activityManagerNativeClass = Class.forName("android.app.ActivityManagerNative");
Method getDefaultMethod = activityManagerNativeClass.getDeclaredMethod("getDefault");
Object activityManager = getDefaultMethod.invoke(null);
Method finishActivityMethod = activityManager.getClass().getDeclaredMethod("finishActivity", IBinder.class, int.class, Intent.class, int.class);
int DONT_FINISH_TASK_WITH_ACTIVITY = 0;
finishActivityMethod.invoke(activityManager, binder, Activity.RESULT_CANCELED, null, DONT_FINISH_TASK_WITH_ACTIVITY);
}
private void finishSomeArgs(Message message) {
try {
Object someArgs = message.obj;
Field arg1Field = someArgs.getClass().getDeclaredField("arg1");
arg1Field.setAccessible(true);
IBinder binder = (IBinder) arg1Field.get(someArgs);
finish(binder);
} catch (Throwable throwable) {
throwable.printStackTrace();
}
}
}