SWT 重启案例分析(五)

极力推荐文章:欢迎收藏
Android 干货分享

阅读五分钟,每日十点,和您一起终身学习,这里是程序员Android

本篇文章主要介绍 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) {
        // Package removed. Remove all its transports from our list. These transports have already
        // 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,
                         * catch it && add log to find out witch package do it. */
                        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());
                    }
                }
            }
        }
    }

至此,本篇已结束,如有不对的地方,欢迎您的建议与指正。同时期待您的关注,感谢您的阅读,谢谢!

微信关注公众号:  程序员Android,领福利

posted @ 2019-08-09 14:25  程序员Android  阅读(211)  评论(0编辑  收藏  举报