0

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?

Muhammed Refaat
  • 8,914
  • 14
  • 83
  • 118
  • 3
    what operations could cause a ANR? – A.S. Apr 01 '15 at 14:09
  • @A.S. It's not specific operation and it happens every very long while, but as per my app environment it's not acceptable at all to get a Not Responding message. It happened the last time when adding creating a big amount of data inside a popup window in a horizontal linear layouts. – Muhammed Refaat Apr 01 '15 at 14:12
  • 2
    *big amount of data inside a popup window in a horizontal linear layouts* what a terrible idea ... why didn't you use listview/recyclerview? – Selvin Apr 01 '15 at 14:13
  • @Selvin in fact, regarding to the design it will be a very complicated ListView, though I thought using LinearLayouts will be easier. – Muhammed Refaat Apr 01 '15 at 14:16
  • 1
    @MuhammedRefaat u can use a `RecyclerView` with horizontal orientation inside ur `PopUpWindow` instead of `LinearLayout` – Kaushik Apr 01 '15 at 15:22
  • @Kaushik in fact I didn't use `RecyclerView` before, so I'll have a look at it, thanks – Muhammed Refaat Apr 02 '15 at 11:56

1 Answers1

2

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.

Amir Uval
  • 14,425
  • 4
  • 50
  • 74