SWT 重启案例分析(五)

作者: 程序员Android1 | 来源:发表于2019-05-08 09:51 被阅读1次

    极力推荐文章:欢迎收藏
    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());
                        }
                    }
                }
            }
        }
    
    长按识别二维码,领福利

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

    如有侵权,请联系小编,小编对此深感抱歉,届时小编会删除文章,立即停止侵权行为,请您多多包涵。

    相关文章

      网友评论

        本文标题:SWT 重启案例分析(五)

        本文链接:https://www.haomeiwen.com/subject/npvpoqtx.html