gpt4 book ai didi

android - 如何在 Android 中正确使用 AsyncTask

转载 作者:IT王子 更新时间:2023-10-28 23:58:11 25 4
gpt4 key购买 nike

我不想将任何参数传递给 AsyncTask 的 doInBackground 方法。

那么代码应该是什么样的?

最佳答案

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;

public class AsyncExample extends Activity{


private String url="http://www.google.co.in";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}


@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();

new AsyncCaller().execute();

}

private class AsyncCaller extends AsyncTask<Void, Void, Void>
{
ProgressDialog pdLoading = new ProgressDialog(AsyncExample.this);

@Override
protected void onPreExecute() {
super.onPreExecute();

//this method will be running on UI thread
pdLoading.setMessage("\tLoading...");
pdLoading.show();
}
@Override
protected Void doInBackground(Void... params) {

//this method will be running on background thread so don't update UI frome here
//do your long running http tasks here,you dont want to pass argument and u can access the parent class' variable url over here


return null;
}

@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);

//this method will be running on UI thread

pdLoading.dismiss();
}

}
}

关于android - 如何在 Android 中正确使用 AsyncTask,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14250989/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com