在app生产阶段使用StrictMode [英] Using StrictMode in app production phase

查看:335
本文介绍了在app生产阶段使用StrictMode的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道, StrictMode 主要设计为应用程序开发中使用阶段,但根​​据我的应用程序的需求,这是不能接受让ANR,而这是完全可以接受得到崩溃,而 StrictMode 提供了一种方法prevent ANR对话框:

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());

如果我用它的应用生产阶段里面呢?当应用程序获得的 StrictMode 使用的 ANR而会发生什么?将其冻结,死机,或者等到再次得到回应?

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?

推荐答案

StrictMode默认情况下禁用,并且用户需要其Android进入开发者模式来启用它。结果
这使得使用StrictMode不相干的溶液。



ANR的可能发生在极少数情况下完全在你的控制,由于条件,如极低的内存或其他应用窒息CPU。

但是,可以最大限度地减少ANR获得通过移动访问存储设备或网络的异步任务的每一个操作简单的的可能性。

在我的软件,我此行code的添加到所有危险的地方:

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:

断言Util.isMainThread()!!窝我是什么主线程做?

和有一定的Util类此方法:

and have this method in some Util class:

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

......而一个有用的技巧,以快速启用命令行断言:结果
亚行外壳setprop Debug.Assert的1 0 禁用。

这篇关于在app生产阶段使用StrictMode的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆