zoukankan      html  css  js  c++  java
  • Android ANR总结

    1、ANR定义

    ANR的全称是application not responding,是指应用程序未响应,Android系统对于一些事件需要在一定的时间范围内完成,如果超过预定时间未能得到有效响应或者响应时间过长,都会造成ANR。一般地,这时往往会弹出一个提示框,告知用户“xxx没有响应”,用户可选择继续等待或者Force Close。

    首先ANR的发生是有条件限制的,分为以下三点:

    1、只有主线程才会产生ANR,主线程就是UI线程;
    2、必须发生某些输入事件或特定操作,比如按键或触屏等输入事件,在BroadcastReceiver或Service的各个生命周期调用函数;
    3、上述事件响应超时,不同的context规定的上限时间不同
      a.主线程对输入事件5秒内没有处理完毕

      b.主线程在执行BroadcastReceiver的onReceive()函数时10秒内没有处理完毕

      c.主线程在前台Service的各个生命周期函数时20秒内没有处理完毕(后台Service200s)。
    2、导致ANR的原因

    1、主线程执行了耗时操作,比如数据库操作或网络编程,I/O操作

    2、其他进程(就是其他程序)占用CPU导致本进程得不到CPU时间片,比如其他进程的频繁读写操作可能会导致这个问题。

    细分的话,导致ANR的原因有如下几点:

      1、耗时的网络访问

      2、大量的数据读写

      3、数据库操作

      4、硬件操作(比如camera)

      5、调用thread的join()方法、sleep()方法、wait()方法或者等待线程锁的时候

      6、service binder的数量达到上限

      7、system server中发生WatchDog ANR

      8、service忙导致超时无响应

      9、其他线程持有锁,导致主线程等待超时

      10、其它线程终止或崩溃导致主线程一直等待
    3、避免ANR、ANR的解决方法

      1、避免在主线程执行耗时操作,所有耗时操作应新开一个子线程完成,然后再在主线程更新UI。

      2、BroadcastReceiver要执行耗时操作时应启动一个service,将耗时操作交给service来完成。

      3、避免在Intent Receiver里启动一个Activity,因为它会创建一个新的画面,并从当前用户正在运行的程序上抢夺焦点。如果你的应用程序在响应Intent广 播时需要向用户展示什么,你应该使用Notification Manager来实现。
    4、各个组件导致ANR发生的时间 

      1、Service

        // How long we wait for a service to finish executing.
        static final int SERVICE_TIMEOUT = 20*1000;
    
        // How long we wait for a service to finish executing.
        static final int SERVICE_BACKGROUND_TIMEOUT = SERVICE_TIMEOUT * 10;
    
        // How long the startForegroundService() grace period is to get around to
        // calling startForeground() before we ANR + stop it.
        static final int SERVICE_START_FOREGROUND_TIMEOUT = 5*1000;

      前台Service:20s

      后台Service:200s

      2、Broadcast

        // How long we allow a receiver to run before giving up on it.
        static final int BROADCAST_FG_TIMEOUT = 10*1000;
        static final int BROADCAST_BG_TIMEOUT = 60*1000;

      前台Broadcast:10s

      后台Broadcast:60s

      3、InputDispatching

        // How long we wait until we timeout on key dispatching.
        static final int KEY_DISPATCHING_TIMEOUT = 5*1000;

      4、ContentProvider

        // How long we wait for an attached process to publish its content providers
        // before we decide it must be hung.
        static final int CONTENT_PROVIDER_PUBLISH_TIMEOUT = 10*1000;
  • 相关阅读:
    python简单的运算
    Typora的基本格式使用
    在Eclipse中在线安装Emmet和图文使用教程
    6月末的总结
    TF-IDF学习笔记
    idea调试SpringMvc, 出现:”Can't find catalina.jar"错误的解决方法
    idea调试SpringMvc, 出现:”javax.servlet.ServletException: java.lang.IllegalStateException: Cannot create a session after the response has been committed"错误的解决方法
    idea调试SpringMvc, 出现:”通配符的匹配很全面, 但无法找到元素 'mvc:annotation-driven' 的声明“错误的解决方法
    idea调试SpringMvc, 出现:java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener错误的解决办法
    Zabbix 漏洞分析
  • 原文地址:https://www.cnblogs.com/diyishijian/p/11558622.html
Copyright © 2011-2022 走看看