由于Android版本问题StrictMode模式下访问网络报错的解决办法 绝对经典

来源:互联网 发布:易企秀同类软件 编辑:程序博客网 时间:2024/05/16 01:25

设置严苛模式(StrictMode)的线程策略

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

Builder类使得设置变得很简单,Builder函数定义所有策略都返回Builder对象,从而这些函数能像列表2-9那样串连在一起。最后调用build()函数返回一个ThreadPolicy对象作为StrictMode对象的setThreadPolicy()函数的参数。注意到setThreadPolicy()是一个静态函数,因此不需要实例化StrictMode对象。在内部,setThreadPolicy()将对当前线程应用该策略。如果不指定检测函数,也可以用detectAll()来替代。penaltyLog()表示将警告输出到LogCat,你也可以使用其他或增加新的惩罚(penalty)函数,例如使用penaltyDeath()的话,一旦StrictMode消息被写到LogCat后应用就会崩溃。

仅在调试模式设置严苛模式(StrictMode)

ApplicationInfo appInfo = context.getApplicationInfo();  

int appFlags = appInfo.flags;  

  • if ((appFlags & ApplicationInfo.FLAG_DEBUGGABLE) != 0) {  
  •     // Do StrictMode setup here  
  •     StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()  .detectLeakedSqlLiteObjects()   .penaltyLog()   .penaltyDeath() .build());  
  • 利用反射技术(reflection)调用严苛模式(StrictMode)

    try {  

  •     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...");  
  • }
  • 在Anroid2.3之前版本建立严苛模式(StrictMode)封装类
  • 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 appFlags = context.getApplicationInfo().flags;  
  •         if ((appFlags & 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());  
  •         }  
  •     }  
  • }  
  • 在Anroid2.3之前版本调用严苛模式(StrictMode)封装类

    try {  

  •     StrictModeWrapper.init(this);  
  • }  
  • catch(Throwable throwable) {  
  •     Log.v("StrictMode""... is not available. Punting...");  
  • }  

    如果考虑到关于版本兼容问题,因为按照上面的写法在2.3以下系统是没有问题的,但是在2.3及以上的话,就会出错,所以应该采用以下方式来处理:

     

  •    @SuppressLint("NewApi")
     public static void init(Context context) {
      // check if android:debuggable is set to true
      int appFlags = context.getApplicationInfo().flags;
      if ((appFlags & ApplicationInfo.FLAG_DEBUGGABLE) != 0) {

       try {
        //Android 2.3及以上调用严苛模式
        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...");
       }

         }
     }

  • 这样的话就解决了版本问题带来的异常情况。
  •