Using StrictMode in app production phase

回眸只為那壹抹淺笑 提交于 2019-12-11 20:33:02

问题


I know that StrictMode designed mainly to be used in application development phase, but according to my app needs, it's not acceptable to get ANR while it's quite acceptable to get crash, and StrictMode provides a way to prevent ANR dialogs:

StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll()
            .penaltyLog().penaltyDeath().build());

what if I used it inside the app production phase? and what will happen when the app getting ANR while StrictMode used? will it freeze, crash, or wait until getting responded again?


回答1:


StrictMode is disabled by default, and users needs to enter "Developer's mode" in their Android to enable it.
That makes the solution of using StrictMode irrelevant.

ANR's could occur in very rare occasions completely out of your control, due to conditions such as very low memory or other app choking the CPU.
However, you can minimize the likelihood of getting ANR's simply by moving every single operation that access storage or network to an asynchronous task.
In my software I add this line of code to all dangerous places:

assert !Util.isMainThread():"woh! what am I doing on the main thread??"

and have this method in some Util class:

public static boolean isMainThread() {
    return Looper.myLooper().equals(Looper.getMainLooper());
}

... And a useful tip to quickly enable the assertion from command line:
adb shell setprop debug.assert 1 or 0 to disable.



来源:https://stackoverflow.com/questions/29392969/using-strictmode-in-app-production-phase

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!