Как мне позвонить AsyncTask
в onCreate
? Его никогда не называют. Тем не менее, он вызван на , поэтому для имитации я просто запустил еще одно действие и закончил его, но при таком подходе мое приложение не выйдет должным образом.Выполнение `AsyncTask` в android
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new ExternalIP().execute((Void) null);
open();
}
private void open() {
Intent intent = new Intent(this, Sec.class);
startActivity(intent);
}
private class ExternalIP extends AsyncTask<Void, Void, String> {
protected String doInBackground(Void... urls) {
String ip = "Empty";
try {
HttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet("http://wtfismyip.com/text");
//http://ip2country.sourceforge.net/ip2c.php?format=JSON
//http://wtfismyip.com/text
HttpResponse response;
response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
if (entity != null) {
long len = entity.getContentLength();
if (len != -1 && len < 1024) {
String str = EntityUtils.toString(entity);
ip = str.replace("\n", "");
final TextView tvHelp1 = (TextView) findViewById(R.id.ip);
tvHelp1.setText("Public IP: " + ip);
} else {
ip = "Response too long or error.";
}
} else {
ip = "Null:" + response.getStatusLine().toString();
}
} catch (Exception e) {
ip = "Error";
}
return ip;
}
protected void onPostExecute(String result) {
// External IP
Log.d("ExternalIP", result);
}
}
}
Я попытался
new ExternalIP().execute();
уже.
движения открытого(); от oncreateView до onPostExecute –