Strict mode in android 2.2

后端 未结 6 577
庸人自扰
庸人自扰 2020-12-06 03:56

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

6条回答
  •  南方客
    南方客 (楼主)
    2020-12-06 04:16

    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..

提交回复
热议问题