本文介绍了在Android 4.0`$ android.os.StrictMode异常AndroidBlockGuardPolicy.onNetwork(StrictMode)`的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

当我在Android 2.3.6运行这块code的,它工作正常。在4.0我得到一个StrictMode例外。从SO一些问题,好像你不能做的UI线程上的网络操作。但我没有做UI线程上的任何UI操作。

When I run this piece of code on Android 2.3.6, it works fine. On 4.0 I get a StrictMode exception. From some questions on SO, seems like you cant do network operations on the UI thread. But I am not doing any UI operation on the UI thread.

我打电话doBackground而不是直接执行,因为我需要检查doBackground的返回值。我想下面应该有工作。

I am calling doBackground directly and not execute, since I need to check the return value of the doBackground. I guess the below should have worked.

我是什么在这里失踪?

handler.postDelayed(new Runnable() {
@Override
public void run() {
    God.identityHash = (String) new SwitchServer((IActionBar) splashScreen,
                God.mCruiseOnServer, nameText, phoneNumberText)
                .doInBackground(null);

    if (!mIsBackButtonPressed && God.identityHash != null) {
        FlurryAgent.logEvent(God.TCACCEPTED_AND_REGISTERED);
        Intent intent = new Intent(SplashScreen.this, HomeScreen.class);
        SplashScreen.this.startActivity(intent);
        finish();
    } else {
        Toast.makeText(splashScreen,
            "Unable to save your details. Please try again.",
            Toast.LENGTH_LONG).show();
        God.setTermsAndConditions(splashScreen, false);
    }
}
}, SPLASH_DURATION);

编辑:在手机上未启用严格模式

Edit : Strict mode is not enabled on the phone.

推荐答案

首先,是你明确地code开启时,它仅仅是一个辅助调试,而不应在生产code会留在。

Firstly, StrictMode is something you explicitly turned on from code, it is just a debugging aid, and should not be left on in production code.

其次,调用 .doInBackground 手动意味着在UI线程上运行它。如果您使用 的AsyncTask 如预期,你会得到它的返回值作为参数在 .onPostExecute

Secondly, calling .doInBackground manually means running it on the UI thread. If you use AsyncTask as intended, you get its return value as a parameter in .onPostExecute.

这篇关于在Android 4.0`$ android.os.StrictMode异常AndroidBlockGuardPolicy.onNetwork(StrictMode)`的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-08 19:53