What is the Android StrictMode life cycle?

I am trying to minimize the number of places in my code where I need to install StrictMode. But I'm not sure whether I am right or not about the following.

The Android StrictMode documentation says you can use it for apps, activities, and other components. I read that it is not advisable to extend the Application class, and I would prefer not to extend the application to enable StrictMode. But I donโ€™t think I should.

There are two policies you can use: ThreadPolicy (for thread) and VmPolicy (for all threads). Thus, it would seem that if I install StrictMode on a thread once, it doesnโ€™t matter where I do it from, and after that violations will be reported in this thread regardless of other calls or not on StrictMode. I just need to call him from somewhere before the disturbances that I want to detect can occur. And it must be configured for any new threads that are created in my application that I also want to check.

What I think I want to avoid calls the build () methods more than I need. Putting StrictMode at the beginning of onCreate() in all my actions means that the build () function will be called more than once in this thread. If I have one Launcher activity in my application, setting StrictMode in this onCreate() action should be sufficient for the rest of the application. It's true?

Secondly, if my main action restarts, although the application has not died, is it technically necessary to call StrictMode again? Or is my thread still configured to report abuse? I thought there might be some value in creating a wrapper type class around StrictMode, for example:

 public class MyStrictModeSettings { static private List<Long> setThreads = new ArrayList<Long>(); // Prevent instantiation of this class private MyStrictModeSettings() {} static public synchronized void init() { try { Long tid = Thread.currentThread().getId(); if(!setThreads.contains(tid)) { setThreads.add(tid); Class sMode = Class.forName("android.os.StrictMode"); Method enableDefaults = sMode.getMethod("enableDefaults"); enableDefaults.invoke(null); } } catch(Exception e) { // StrictMode not supported on this device, punt Log.v("StrictMode", "... not supported. Skipping..."); } } } 

Thus, in my main onCreate () action, I can just call MyStrictModeSettings.init () and do with it. And it should work with Android versions up to 2.3. But it may not be worth it. Brad, are you there? Thanks.

Edit: Since VmPolicy is designed for all threads, technically I only need to install it once for the application, right? So enableDefaults () wastes efforts on repeating VmPolicy when it is called second, third, etc. Time? Again, maybe this is more of a problem than trying to avoid extra calls.

+4
source share
2 answers

Yes, VmPolicy is designed for the whole process, so doing this incorrectly. More time is cheap, however, so there is nothing to worry about.

And yes, you only need to do this in your main / running activity onCreate () --- the same โ€œmainโ€ thread as all your other components.

+7
source

By looking at the source code, you can see that it is called statically:

 public static void setVmPolicy(final VmPolicy policy) { synchronized (StrictMode.class) { sVmPolicy = policy; sVmPolicyMask = policy.mask; setCloseGuardEnabled(vmClosableObjectLeaksEnabled()); Looper looper = Looper.getMainLooper(); if (looper != null) { MessageQueue mq = looper.mQueue; if (policy.classInstanceLimit.size() == 0 || (sVmPolicyMask & VM_PENALTY_MASK) == 0) { mq.removeIdleHandler(sProcessIdleHandler); sIsIdlerRegistered = false; } else if (!sIsIdlerRegistered) { mq.addIdleHandler(sProcessIdleHandler); sIsIdlerRegistered = true; } } } } 

And the policy itself is also stored statically - there are no non-static member variables in the class.

  private static volatile VmPolicy sVmPolicy = VmPolicy.LAX; 

This means that you only need to do this once for each application, for example, in a run / write action for your application.

+2
source

Source: https://habr.com/ru/post/1335761/


All Articles