本篇文章主要介绍 Android
开发中的部分知识点,通过阅读本篇文章,您将收获以下内容:
一、Service not registered 异常导致手机重启
二、Service not registered 解决方案
2.1 修改ContextImpl类
2.2 修改 TransportManager 类
一、Service not registered 异常导致手机重启
重启 的部分Log
如下:
Service not registered 重启异常
二、Service not registered 解决方案
从 Log
中分析发现异常信息,并尝试进行如下修改。
1.修改ContextImpl类
文件路径如下:
\frameworks\base\core\java\android\app\ContextImpl.java
文件
优化unbindService
方法实现如下:
@Override
public void unbindService(ServiceConnection conn) {
if (conn == null) {
throw new IllegalArgumentException("connection is null");
}
if (mPackageInfo != null) {
IServiceConnection sd = mPackageInfo.forgetServiceDispatcher(
getOuterContext(), conn);
try {
ActivityManager.getService().unbindService(sd);
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
// add for Service not registered unbindService() triger reboot exception
} catch (IllegalArgumentException e) {
//com.google.android.gms.ui Service not registered Crash
android.util.Log.e("wjwj","---ContextImpl GMS Crash---");
e.printStackTrace();
}
// add for Service not registered unbindService() triger reboot exception
} else {
throw new RuntimeException("Not supported in system context");
}
}
2.修改 TransportManager 类
文件路径如下:
frameworks\base\services\backup\java\com\android\server\backup\TransportManager.java
将异常捕获,防止重启。
优化unbindService
方法实现如下:
void onPackageRemoved(String packageName) {
// been removed from mBoundTransports because onServiceDisconnected would already been
// called on TransportConnection objects.
synchronized (mTransportLock) {
Iterator<Map.Entry<ComponentName, TransportConnection>> iter =
mValidTransports.entrySet().iterator();
while (iter.hasNext()) {
Map.Entry<ComponentName, TransportConnection> validTransport = iter.next();
ComponentName componentName = validTransport.getKey();
if (componentName.getPackageName().equals(packageName)) {
TransportConnection transportConnection = validTransport.getValue();
iter.remove();
if (transportConnection != null) {
/* 360OS begin */
/* unbindService() triger reboot exception,
try {
Slog.d(TAG, "onPackageRemoved trace, componentName:"
+ componentName.toString(), new Throwable());
mContext.unbindService(transportConnection);
} catch (IllegalArgumentException e) {
Slog.e(TAG, "unbindService fail.", e);
}
/* 360OS end */
log_verbose("Package removed, removing transport: "
+ componentName.flattenToShortString());
}
}
}
}
}