我认为android应该提供更多binder远程异常信息
所以我修改 Parcel.java 来包装更多的活页夹远程异常信息
public final void writeException(Exception e) {
int code = 0;
if (e instanceof SecurityException) {
code = EX_SECURITY;
} else if (e instanceof BadParcelableException) {
code = EX_BAD_PARCELABLE;
} else if (e instanceof IllegalArgumentException) {
code = EX_ILLEGAL_ARGUMENT;
} else if (e instanceof NullPointerException) {
code = EX_NULL_POINTER;
} else if (e instanceof IllegalStateException) {
code = EX_ILLEGAL_STATE;
}
writeInt(code);
StrictMode.clearGatheredViolations();
if (code == 0) {
if (e instanceof RuntimeException) {
throw (RuntimeException) e;
}
throw new RuntimeException(e);
}
// I replace writeString(e.getMessage()) with writeString(remoteExceptionToString(e))
writeString(remoteExceptionToString(e));
}
static String remoteExceptionToString(Exception e) {
final StringWriter sw = new StringWriter(1024);
final PrintWriter pw = new PrintWriter(sw, true);
pw.println();
e.printStackTrace(pw);
return sw.toString().replace("\n", String.format("\n(%5d %5d): ", Process.myPid(), Process.myTid()));
}
SerializeExceptionSecondService 定义:
public class SerializeExceptionSecondService extends Service {
private static final String TAG = "SerializeExceptionSecondService";
public SerializeExceptionSecondService() {
}
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
return mServiceBinder;
}
private final ISerializeExceptionSecondService.Stub mServiceBinder = new ISerializeExceptionSecondService.Stub() {
@Override
public void throwException() throws RemoteException {
// TODO Auto-generated method stub
Log.e(TAG, "throwException");
throw new IllegalStateException("Cause1", new IllegalStateException("Cause2", new IllegalStateException("Cause3")));
}
@Override
public void noThrowException() throws RemoteException {
// TODO Auto-generated method stub
}
};
}
AndroidManifest.xml 片段:
<service
android:name=".SerializeExceptionSecondService"
android:enabled="true"
android:exported="true"
android:process=":second_remote" >
</service>
这样,binder 异常 logcat 将如下所示: