Pregunta

I have target sdk set as 3.2 and min sdk as 2.2, how can I use strictmode in my application, as I understand it is introduced but cannot really understand how to start using it.

¿Fue útil?

Solución

My suggestion is two-fold:

First, add some baseline StrictMode code to your Application's onCreate(). This lets you apply StrictMode to your entire app in an easy fashion (though you could put this code anywhere for more specific testing). There's good sample code in the StrictMode docs.

Second, detect the version of the SDK before trying to use StrictMode. This way, you only use StrictMode in API versions 9 or above - if you don't do this check, you'll crash your app on older versions of Android. You can easily detect the SDK version by looking at Build.VERSION.SDK_INT.

Optionally, you may want to only enable StrictMode when you're testing. How you do this is up to you, though I've written up one solution for this in the past.

Otros consejos

StrictMode.ThreadPolicy was introduced since API Level 9 and the default thread policy had been changed since API Level 11, which in short, does not allow network operation

(eg: HttpClient and HttpUrlConnection) get executed on UI thread. If you do this, you get NetworkOnMainThreadException.

You can easily solve this error By two ways:-

  1. The recommended way of solving this is by Using anAsyncTask so that the network request does not block the UI thread.

  2. Alternatively, you can override this thread policy by adding the below code into your main activity’s onCreate() method.

if (android.os.Build.VERSION.SDK_INT > 9) {
    StrictMode.ThreadPolicy policy = 
    new StrictMode.ThreadPolicy.Builder().permitAll().build();      
        StrictMode.setThreadPolicy(policy);
 }

Hope this is helpful to you..

Set the Android Manifest to something like this.

<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="16" android:maxSdkVersion="16"/>

Use the below code in the onCreate Method.

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); 

}

Note: Disable the warning as you are already checking which version of the Android is going to use this code.

This Code will be activated if the Android version is higher than Android 2.2

This is really helpful for How to use StrictMode in App? Check this.. http://mobile.tutsplus.com/tutorials/android/android-sdk_strictmode/

Refer to the following link on how to use it. Set it up on the onCreate of the relevant component or the Application

Strict Mode

It helped in resolving the issue by checking the api level and then executing strict mode.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top