【Android】StrictMode

StrictMode is used to report violations of policies related to threads and related to the virtual machine.
Some of the violations include custom slow calls, disk reads, disk writes, and network accesses.

StrictMode Policies

StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder())
   .detectDiskReads()
   .detectDiskWrites()
   .detectNetwork()
   .penaltyLog()
   .build());


You could enable StrictMode at the beginning of your main activity's onCreate() method, which runs on the main thread, and it would then be enabled for everything that happens on that main thread.

You could also enable it in your application by extending the Application class and adding StrictMode setup to the application's onCreate() method.


StrictMode.setVmPolicy(new StrictMode.vmPolicy.Builder()
    .detectLeakedSqlLiteObjects()
    .penaltyLog()
    .penaltyDeath()
    .build());

VmPolicy can check for memory leaks if a SQLite object is finalized before it has been closed, or if any Closeable object is finalized before it has been closed.


Turning off StrictMode

ApplicationInfo appInfo = context.getApplicationInfo();
int appFlags = appInfo.flags;
if ((appFlags & ApplicationInfo.FLAG_DEBUGGABLE) != 0) {
   // Do strictMode setup here
}

There are also some permit methods as permitDiskReads().


你可能感兴趣的:(【Android】StrictMode)