美文网首页
关于anr的知识复习

关于anr的知识复习

作者: Allenlll | 来源:发表于2020-05-06 16:30 被阅读0次

    一知半解

    从源码来看都哪种情况下会造成anr

    • ActivitiManagerService
      ActivityManagerService是系统核心服务,统一管理着四大组件。关于四大组件的管理过程,一直是一知半解,应该写篇文章来好好复习总结一下,这里就先总结一下anr的分发过程吧。
      static final int SHOW_NOT_RESPONDING_UI_MSG = 2;
      final class UiHandler extends Handler {
            public UiHandler() {
                super(com.android.server.UiThread.get().getLooper(), null, true);
            }
    
            @Override
            public void handleMessage(Message msg) {
                switch (msg.what) {
                case SHOW_NOT_RESPONDING_UI_MSG: {
                    mAppErrors.handleShowAnrUi(msg);
                    ensureBootCompleted();
                } break;
            }
    
    

    从中可以看出anr超时是通过handler来发送后更新ui的,handleSowAnrUi会弹出一个超时的dialog。接下来可以看看这个SHOW_NOT_RESPONDING_UI_MSG都是从哪里发出来的,就能知道哪种情况下会造成anr了。

    • ProcessRecord
      所有的anrMessage都是从这个方法中发送出去的
     void appNotResponding(String activityShortComponentName, ApplicationInfo aInfo,
                String parentShortComponentName, WindowProcessController parentProcess,
                boolean aboveSystem, String annotation) {
            synchronized (mService) {
                // mBatteryStatsService can be null if the AMS is constructed with injector only. This
                // will only happen in tests.
                // mUiHandler can be null if the AMS is constructed with injector only. This will only
                // happen in tests.
                if (mService.mUiHandler != null) {
                    // Bring up the infamous App Not Responding dialog
                    Message msg = Message.obtain();
                    msg.what = ActivityManagerService.SHOW_NOT_RESPONDING_UI_MSG;
                    msg.obj = new AppNotRespondingDialog.Data(this, aInfo, aboveSystem);
    
                    mService.mUiHandler.sendMessage(msg);
                }
            }
        }
    
    
    

    有以下几个调用的地方:

    1. ActivityManagerService
     public void appNotRespondingViaProvider(IBinder connection) {
      }
    contentProvider的超时
    
    1. ActivityManagerService
     /**
         * Handle input dispatching timeouts.
         * @return whether input dispatching should be aborted or not.
         */
        boolean inputDispatchingTimedOut(ProcessRecord proc, String activityShortComponentName,
                ApplicationInfo aInfo, String parentShortComponentName,
                WindowProcessController parentProcess, boolean aboveSystem, String reason) {
         
    
            if (proc != null) {
                synchronized (this) {
                    if (proc.isDebugging()) {//debug时不弹anr框
                        return false;
                    }
    
                    if (proc.getActiveInstrumentation() != null) {//子进程里面不弹anr弹
                        Bundle info = new Bundle();
                        info.putString("shortMsg", "keyDispatchingTimedOut");
                        info.putString("longMsg", annotation);
                        finishInstrumentationLocked(proc, Activity.RESULT_CANCELED, info);
                        return true;
                    }
                }
                proc.appNotResponding(activityShortComponentName, aInfo,
                        parentShortComponentName, parentProcess, aboveSystem, annotation);
            }
    
            return true;
        }
    输入的超时,两种情况下不弹anr框:debug时,子进程里面。
    
    1. ActiveServices
    void serviceTimeout(ProcessRecord proc) {
    }
    后台服务超时
    
    1. ActiveServices
      void serviceForegroundTimeout(ServiceRecord r) {
    }
    前台服务超时
    

    相关文章

      网友评论

          本文标题:关于anr的知识复习

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