问题
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.
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.
What am I missing here ?
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.
回答1:
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.
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
.
来源:https://stackoverflow.com/questions/15921252/exception-on-android-4-0-android-os-strictmodeandroidblockguardpolicy-onnetwor