StrictMode对较低的平台版本
-
25-09-2019 - |
题
我已经开始使用StrictMode并发现,这将是巨大的,它总是在运行期间发展并不仅仅是在一个特殊分支我创造了在饭桶。原因我没有这是我的应用程序需要运行有1.6。
我读了在开发的博客,你可以把它设置,使激活它通过反射。我只是想知道如何实际上会看起来像如果有可能向具有此记录的在这里(或其他地方),而不必每个人都想利用它作出自己。
其他提示
我读过曼弗雷德的博客文章,但如果你将目标平台的版本降低它不工作比2.3 因为StrictMode.enableDefaults();
方法是不可用的。
下面是我的解决方案,它充分依赖于反射,并且不产生编译错误:
try {
Class<?> strictModeClass = Class.forName("android.os.StrictMode", true, Thread.currentThread()
.getContextClassLoader());
Class<?> threadPolicyClass = Class.forName("android.os.StrictMode$ThreadPolicy", true, Thread
.currentThread().getContextClassLoader());
Class<?> threadPolicyBuilderClass = Class.forName("android.os.StrictMode$ThreadPolicy$Builder", true,
Thread.currentThread().getContextClassLoader());
Method setThreadPolicyMethod = strictModeClass.getMethod("setThreadPolicy", threadPolicyClass);
Method detectAllMethod = threadPolicyBuilderClass.getMethod("detectAll");
Method penaltyMethod = threadPolicyBuilderClass.getMethod("penaltyLog");
Method buildMethod = threadPolicyBuilderClass.getMethod("build");
Constructor<?> threadPolicyBuilderConstructor = threadPolicyBuilderClass.getConstructor();
Object threadPolicyBuilderObject = threadPolicyBuilderConstructor.newInstance();
Object obj = detectAllMethod.invoke(threadPolicyBuilderObject);
obj = penaltyMethod.invoke(obj);
Object threadPolicyObject = buildMethod.invoke(obj);
setThreadPolicyMethod.invoke(strictModeClass, threadPolicyObject);
} catch (Exception ex) {
Log.w(TAG, ex);
}
我看到你的博客文章。由于您只想设置StrictMode最多按Java文件一次,它将使任何意义,简化代码呼吁建立如下?
下面是一个备用StrictModeWrapper:
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.os.StrictMode;
public class StrictModeWrapper {
public static void init(Context context) {
// check if android:debuggable is set to true
int applicationFlags = context.getApplicationInfo().flags;
if ((applicationFlags & ApplicationInfo.FLAG_DEBUGGABLE) != 0) {
StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
.detectDiskReads()
.detectDiskWrites()
.detectNetwork()
.penaltyLog()
.build());
StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
.detectLeakedSqlLiteObjects()
.penaltyLog()
.penaltyDeath()
.build());
}
}
}
这是你的代码,你只需要做到以下几点:
try {
StrictModeWrapper.init(this);
}
catch(Throwable throwable) {
Log.v("StrictMode", "... is not available. Punting...");
}
其中的此强>是本地上下文,诸如您的活动或应用程序或任何。这似乎是工作前的Android 2.3,同时也为您提供了使用生成器类来配置StrictMode的其他方法完全按照你想要的力量。
我扔在一起的另一个变化主题上,我概述了在博客 在这里,.主要区别在我的方法是,它还提供了包装磁盘和虚拟机政策的对象,这样就可以很容易地支架StrictMode犯罪码的临时政策的变化。反馈意见是值得欢迎的。
要像素代码我还添加这个(基于StrictMode Android的API示例):
// VM policy
Class<?> VmPolicyClass = Class.forName("android.os.StrictMode$VmPolicy", true, Thread.currentThread().getContextClassLoader());
Class<?> VmPolicyBuilderClass = Class.forName("android.os.StrictMode$VmPolicy$Builder", true, Thread.currentThread().getContextClassLoader());
Method setVmPolicyMethod = strictModeClass.getMethod("setVmPolicy", VmPolicyClass);
Method detectLeakedSqlLiteObjectsMethod = VmPolicyBuilderClass.getMethod("detectLeakedSqlLiteObjects");
Method detectLeakedClosableObjectsMethod = null;
try
{
detectLeakedClosableObjectsMethod = VmPolicyBuilderClass.getMethod("detectLeakedClosableObjects");
}
catch (Exception e) {}
Method penaltyLogMethod = VmPolicyBuilderClass.getMethod("penaltyLog");
Method penaltyDeathMethod = VmPolicyBuilderClass.getMethod("penaltyDeath");
Method VmbuildMethod = VmPolicyBuilderClass.getMethod("build");
Constructor<?> VmPolicyBuilderConstructor = VmPolicyBuilderClass.getConstructor();
Object VmPolicyBuilderObject = VmPolicyBuilderConstructor.newInstance();
Object Vmobj = detectLeakedSqlLiteObjectsMethod.invoke(VmPolicyBuilderObject);
if (detectLeakedClosableObjectsMethod != null) Vmobj = detectLeakedClosableObjectsMethod.invoke(Vmobj);
Vmobj = penaltyLogMethod.invoke(Vmobj);
Vmobj = penaltyDeathMethod.invoke(Vmobj);
Object VmPolicyObject = VmbuildMethod.invoke(Vmobj);
setVmPolicyMethod.invoke(strictModeClass, VmPolicyObject);
设置Android清单到这样的事情。
< uses-sdk android:minSdkVersion="8" android:targetSdkVersion="16" android:maxSdkVersion="16"/ >
使用下面的代码在onCreate方法。
int SDK_INT = android.os.Build.VERSION.SDK_INT;
} if (SDK_INT>8){
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
请注意:禁止因为你已经检查其Android版本将会使用此代码的警告
这个代码将被如果Android版本是比Android 2.2
更高激活不隶属于 StackOverflow