StrictModeViolation when calling AccountManager.getAccountsByType() from AsyncTask.doInBackground()

249 views Asked by At

I'm calling AccountManager.getAccountsByType() in an AsyncTask.doInBackground() method. But it's causing this StrictModeViolation at least sometimes:

android.os.StrictMode$StrictModeViolation: policy=95 violation=2
            at android.os.StrictMode.executeDeathPenalty(StrictMode.java:1379)
            at android.os.StrictMode.access$1300(StrictMode.java:118)
            at android.os.StrictMode$AndroidBlockGuardPolicy.handleViolation(StrictMode.java:1372)
            at android.os.StrictMode$AndroidBlockGuardPolicy$1.run(StrictMode.java:1254)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

But surely an AsyncTask.doInBackground() is exactly where it should be OK to call AccountManager methods?

This is how I'm enabling StrictMode:

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

By the way, I know that the AccountManager documentation says "It is safe to call this method from the main thread" on many methods but it's apparently common knowledge that that is wrong and I've already avoided several other StrictModeViolations by moving uses of AccountManager into AsyncTasks.

0

There are 0 answers