• Stars
    star
    116
  • Rank 303,894 (Top 6 %)
  • Language
    Java
  • Created almost 7 years ago
  • Updated over 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

ANR collector which can collect ANR information(收集ANR相关信息的工具类)

AnrManager

AnrManager是一个android卡顿监测模块。当UI线程卡顿(得不到执行、无反应)达到预定阈值时,将把卡顿期间线程堆栈打印出来,以便开发人员分析和优化App的性能。 AnrManager由于本身有个收集线程堆栈的后台线程工作,所以会带来一定的性能消耗,这个大概在百分之2%-3%左右。设置收集堆栈时间越小,消耗越大。
触发卡顿时间范围是500ms~5000ms,默认为3000ms;
收集时间间隔范围为500ms~2000ms,默认为1000ms。
注意:自定义时,收集时间间隔设置不能大于触发卡顿时间间隔。

Usage

Add it to your build.gradle with:

allprojects {
    repositories {
        maven { url "https://jitpack.io" }
    }
}

默认情况下,你在Application的onCreate方法中这样写就可以。

    AnrManager.initialize(this);  //default

你也可以使用Builder来自定义

 // use builder build your custom way
 AnrManager.Builder builder = new AnrManager.Builder(this)
                 //默认监测模式为AnrManager.MonitorMode.LOOPER,这样指定AnrManager.MonitorMode.FRAME
                 .monitorMode(AnrManager.MonitorMode.FRAME)
                 .loggingEnabled(true)  // 是否打印log
                 .collectInterval(1000) //监测采集堆栈时间间隔
                 .thresholdTime(2000)   // 触发卡顿时间阈值
                 .callback(new AnrManager.AnrCallback() { //设置触发卡顿时回调
                     @Override
                     public void onBlockOccurs(String[] stackTraces, String anr, long... blockArgs) {
                         for(String temp : stackTraces){
                             Log.e(TAG,"stackTraces------------" + temp);
                         }
                         Log.e(TAG, "anr------------" + anr);
                         Log.e(TAG, "blockArgs------------" + blockArgs);

                         // stackTraces : 收集到的堆栈,以便分析卡顿原因。 anr : 如果应用发生ANR,这个就我ANR相关信息,没发生ANR,则为空。
                         //采用AnrManager.MonitorMode.FRAME模式监测时,blockArgs的size为1,blockArgs[0] 即是发生掉帧的数。
                         //采用AnrManager.MonitorMode.LOOPER模式监测时,blockArgs的size为2,blockArgs[0] 为UI线程卡顿时间值,blockArgs[1]为在此期间UI线程能执行到的时间。
                         //这里你可以卡顿信息上传到自己服务器
                     }
                 });
         AnrManager.initialize(builder);

1、监测模式

监测模式有两种:

  • AnrManager.MonitorMode.FRAME
  • 这种模式是通过监测绘制帧间隔时间来判断是否卡顿。也就是给Choreographer设置FrameCallback的方式。这种方式只能在API 16上才能使用,否则默认使用LOOPER模式。调用FPSFrameCallBack
  • AnrManager.MonitorMode.LOOPER
  • 这种模式是通过监测主线程消息处理时间来判断。也就是给主线程Looper设置Printer,来计算消息处理开始前和处理后的时间间隔判断。

    2、结构原理图

    Alt text

    3、测试。

    我们人为在MainActivity中制造卡顿:

     public void pause(View view){
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

    logcat将打印出如下图:

    Alt text

    More Repositories

    1

    ServiceKeep

    Android Service keep-alive module.(Android Service保活模块:5.0以下使用c来fork进程,5.0以上使用JobScheduler来保活进程)
    Java
    413
    star
    2

    ProcessKeep

    Two Services are keeping each other alive. You can use this project. (使用双进程守护来实现2个Service的保活,2个进程相互监听。通过AIDL实现。)
    Java
    282
    star
    3

    apikeylib

    Generating sign when access the Internet. (访问网络时签名的生成)
    C++
    200
    star
    4

    year_month_wheel_dialog

    Year Month Wheel Dialog is a view to choose year and month by double roller dialog.(能够选择年月的双滚轮dialog)
    Java
    191
    star
    5

    AndroidNet

    Java & Android Network Module(JAVA网络、Android网络操作:包括网络请求、WebView相关、常用第三方框架OkHttp&Volley)
    Java
    174
    star
    6

    drag_click_view

    Two GridViews which support clicking and dragging to select or unselect item.(支持点击、拖拽来进行选择、未选择item切换的双GridView)
    Java
    162
    star
    7

    CoverFlow

    Infinite loop photo wall, which support sliding around.(无限循环照片墙,支持左右滑动)
    Java
    153
    star
    8

    ImageLoader

    Custom interface picture loading module, which supports Glide and so on.(自定义接口的图片加载工具module,支持 Glide等)
    Java
    153
    star
    9

    verticaltextview

    Vertical TextView, text from down to up .(竖版的TextView,文本从下往上展示)
    Java
    149
    star
    10

    select_picture_module

    Choose custom chart component, which supports multiple specified folder pictures and supports full screen display and to delete the selected images. (自定义选图组件,支持多选指定文件夹的图片,支持全屏展示和删除已选图片)
    Java
    146
    star
    11

    edittext_with_delete_button

    Custom EditText with delete button which can be clicked to clear the letters.The letters are already inputted in the EditText. (带有自动显示删除按钮的EditText,点击后可清空已经输入的内容)
    Java
    145
    star
    12

    custom_seekbar

    SeekBar with the custom user interface. (自定义UI样式的可选进度条)
    Java
    144
    star
    13

    CustomClock

    The clock is painted by canvas, with a hour,minute and second hand.(手画的时钟,带有时、分、秒针)
    Java
    139
    star
    14

    IOSToggleButton

    toggle button like in IOS original system.(仿IOS原生的toggle button)
    Java
    138
    star
    15

    MultiListView

    Custom ListView is designed to support secondary and tertiary linkage , and can be retrieved according to the first letter.(支持二、三级联动ListView,可以按首字母检索)
    Java
    137
    star
    16

    Character_Line_Feed_TextView

    Custom TextView, which will not automatically wrap If the end of a line is English words.(自定义TextView,一行末尾若是英文单词,不会自动换行。)
    Java
    137
    star
    17

    ArcImageView

    Custom ImageView is designed to support loading network and local image, which has rounded corners, or is rounded. (支持加载网络、本地的图片的四角圆角或圆形的ImageView)
    Java
    135
    star
    18

    email_auto_popupwindow

    Input email text when @ is written email address will be auto completed. (自动下拉关联邮箱列表)
    Java
    131
    star
    19

    fade_in_out_dialog

    Android Dialog which has fade-in and fade-out animation. (带有淡入淡出动画的Android Dialog)
    Java
    131
    star
    20

    datepickerdialog

    Custom Date Picker Dialog.(带有全日历图表and滚轮选择日期的dialog)
    Java
    129
    star
    21

    CustomImageView

    Custom round or rounded ImageView image. (自定义圆形or圆角图片ImageView)
    Java
    129
    star
    22

    trackballs

    tracking ball which has dynamic effect. (展示带有动态效果的轨迹球)
    Java
    126
    star
    23

    name_indexbar

    Alphabetic list on the right side to select first letter to locate ListView. (右侧26个英文字母列表的可根据首字母定位的ListView)
    Java
    126
    star
    24

    CustomToast

    Custom Toast extends android widget Toast. (自定义Toast,继承自android.widget.Toast)
    Java
    124
    star
    25

    GuideView

    Guidance of pages for application that is installed at the first time, which is encapsulated into a View. (应用第一次安装后显示的引导页,封装到一个控件中)
    Java
    123
    star
    26

    ruler

    Hand-painted ruler ,inch and cm for the unit , is identified by the middle of the two red line to measure the width of the articles. (手绘直尺,以inch和cm为单位的,可以通过中间的两条红线来确认要测量物品的宽度)
    Java
    117
    star
    27

    horizontal_listview

    Custom Horizontal ListView.(自定义的支持水平滑动的ListView)
    Java
    116
    star
    28

    AndroidMultiChannelBuild

    Kit support generating multi-channel apk (Android多渠道打包工具)
    Java
    109
    star
    29

    hexagon

    Hand-painted hexagon view or hexagon hexagon layout is composed of n button. (手绘正六边形视图or由n个按钮组成的正六边形布局)
    Java
    106
    star
    30

    CustomHandler

    Custom Handler mechanism to achieve communication between threads.(自定义Handler机制,实现线程间的通信)
    Java
    105
    star
    31

    RequestPermission

    Module used for requesting permission after 6.0(Android6.0后用来动态申请权限的模块)
    Java
    95
    star
    32

    ndk_pic

    Using NDK&JNI to process images.(使用NDK和JNI调整图片的亮度和对比度,使用cmake编写脚本)
    CMake
    65
    star
    33

    ndk_pic_mk

    Using NDK&JNI to process images, with mk.(使用NDK和JNI调整图片的亮度和对比度,使用mk编写脚本)
    Java
    57
    star