[英]AsyncTask progressdialog not showing, doInBackground blocks UI
我一直在嘗試在檢索數據時使progressdialog工作。
我在加載數據時使用了此鏈接實現android啟動畫面,但我似乎無法正常工作。
到目前為止,它沒有顯示任何進度條,因為我認為funcion fillMyTickets會阻止UI線程,因此不會顯示該對話框。 我不明白如何解決此問題。 通過閱讀Stackoverflow,我找到了解決方案,例如在AsyncTask中使用onProgressUpdate函數,但這不是我想要的。 我需要的是一個等待對話框,其中顯示HTTP通話結束后下載並消失。
exampleFragment.java中的 Asynctask函數
private ProgressDialog dialog;
private class PrefetchData extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
dialog = new ProgressDialog(getActivity());
dialog.setMessage("Downloading..");
dialog.setIndeterminate(true);
dialog.setCancelable(true);
dialog.show();
}
@Override
protected Void doInBackground(Void... arg0) {
fillMyTickets();
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
dialog.dismiss();
}
exampleFragment.java中的 fillMyTickets函數
private void fillMyTickets() {
HttpReader httpReader = new HttpReader();
httpReader
.setOnResultReadyListener(new HttpReader.OnResultReadyListener() {
@Override
public void resultReady(String result) {
JsonHelper jsonHelper = new JsonHelper();
tickets = jsonHelper.getTickets(result);
}
});
httpReader
.execute(url);
}
執行AsyncTask的onAttach(活動活動)函數
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
new PrefetchData().execute();
}
同樣,在AsyncTask的onPostExecute中注釋dialog.dismiss()時,該對話框確實顯示,但隨后永遠不會關閉。
您正在調用注冊偵聽器並下載一些數據的方法,但是在下載數據之前,您是從doInBackground
返回並關閉該對話框,因此您的對話框被顯示並隱藏。 嘗試修改代碼,並在完成下載后關閉對話框:
private ProgressDialog dialog;
private class PrefetchData extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
dialog = new ProgressDialog(getActivity());
dialog.setMessage("Downloading..");
dialog.setIndeterminate(true);
dialog.setCancelable(true);
dialog.show();
}
@Override
protected Void doInBackground(Void... arg0) {
HttpReader httpReader = new HttpReader();
httpReader.setOnResultReadyListener(new HttpReader.OnResultReadyListener() {
@Override
public void resultReady(String result) {
JsonHelper jsonHelper = new JsonHelper();
tickets = jsonHelper.getTickets(result);
//finished the download and we dismiss the dialog
dialog.dismiss();
}
});
httpReader.execute(url);
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
}
希望能幫助到你!
雖然我建議您使用Volley庫執行異步網絡請求(具有重試策略,引入線程池執行程序的東西,以及一些可以擴展和修改的其他好東西),但在過去的項目中,我使用了異步任務與進度對話框,並且之前已經遇到了這個確切的問題。 我處理它的方法是在您的類中編寫一個回調接口,該接口擴展了將在postExecute()上調用的AsyncTask。
public class MyDataGrabber extends AsyncTask<Void,Void,Void> {
public interface Callback {
public void onComplete(String data);
}
public Callback callBack;
public MyDataGrabber(Callback callback) { // Initialize the callback
this.callBack = callback;
}
// on preExecute
// on doInBackground
public void onPostExecute(String data){
this.callBack.onComplete(data);
}
}
// So when you call your async task it will look like this
Dialog dialog;
//.... set up and show dialog etc.
new MyDataGrabber(new MyDataGrabber.Callback(){
@Override
public void onComplete(String data){
// Handle data
doDataHandling();
// Dismiss dialog
if(dialog.isShowing){
dialog.dismiss();
}
}
}).execute(String url...other data etc);
Salauyou是正確的,盡管它關於AsyncTask的缺點,但它有一些負面影響,取決於android api的級別,它以並行或串行方式執行,並且即使關閉應用程序,並發問題和異步任務仍在后台運行。 通常,它需要您編寫各種取消方法和弱引用才能正確處理。 使用線程和處理程序絕對是一個值得解決的方案。
好的,所以我誤讀了這個問題。...但是doinbackground不應該阻止任何東西,這就是為什么它在后台。
不,這就是答案。...ui上不是對話框。 只需從常規活動運行它即可,而不是異步任務。 我認為你這樣使用過男女同校
public class MainActivity extends Activity {
private ProgressDialog dialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dialog = new ProgressDialog(this);
dialog.setMessage("Downloading..");
dialog.setIndeterminate(true);
dialog.setCancelable(true);
dialog.show();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
因此從根本上將對話框本身移出asynctask,並使用“ this”作為上下文。 如果您將訪問權限從私人更改為公共,則可以從應用程序中的任何位置啟動或殺死它
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.