从onD的AlertDialog执行AsyncTask不会调用opPostExecute

时间:2013-07-27 05:28:13

标签: android android-asynctask android-alertdialog

从AlertDialog执行AsyncTask时,我会遇到奇怪的行为。我需要一些建议/解决方法来解决它。我被困在这一点上。

当我从AlertDialog执行AsyncTask时,它没有调用onPostExecute。它调用doInBackground,但在完成后,它不会调用onPostExecute。我希望AsyncTask基于AlertDialog的按键按下值执行。

这是创建AlertDialog并执行AsyncTask的函数:

private void processDownloadChoosen(String msg, int __position){
    final AlertDialog.Builder alertBox = new AlertDialog.Builder(new ContextThemeWrapper(ShivaniMP3Activity.this, android.R.style.Theme_Dialog));
    final int position = __position;
    alertBox.setMessage(msg);
    alertBox.setCancelable(false)
            .setPositiveButton("Download", new DialogInterface.OnClickListener(){
                    public void onClick(DialogInterface dialog,int id){
                        dialog.dismiss();
                        String downloadURL = entries.get(position);
                        AsyncTaskDownload atd = new AsyncTaskDownload(downloadURL);
                        if((downloadURL != null) &&(downloadURL != "")){
                            EnglishMP3Activity.totalDownloads++;
                            if(downloadWindow == null){
                                downloadWindow = new PopupWindow(downloadPopupLayout, LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT, true);
                                downloadWindow.showAtLocation(downloadPopupLayout, Gravity.CENTER, 0, 0);
                            }
                            atd.execute();
                        }
                    }
            }).setNegativeButton("Listen", new DialogInterface.OnClickListener(){
                public void onClick(DialogInterface dialog,int id){
                    dialog.dismiss();
                    String downloadURL = entries.get(position).replace("%20", " ");
                    emp = new EasyMediaPlayer(mp3PopupLayout,buttonPlayPause,seekBarProgress,tv_mp3,downloadURL);
                    emp.startPlayingMP3();          
                }
        }).show();
}

我在listview的项目点击中调用此函数:

//lv is listview 
    lv.setOnItemClickListener(new AdapterView.OnItemClickListener(){
        public void onItemClick(AdapterView<?> p, View v, int position, long id) {
            processDownloadChoosen("Do you want to Listen or Download this file ?",position);
        }
    });

我的AsyncTask定义如下:

public class AsyncTaskDownload extends AsyncTask<Void, String, Void> {
    //
    protected void onPreExecute(){
        pBar1.setVisibility(View.INVISIBLE);
        //
    }

    protected Void doInBackground(Void... vd){
        try{
            //do something
        }
        catch(Exception e){
            //do somting
        }
        return null;
    }

    protected void onProgressUpdate(String... msg) {
        //do smething
    }

    protected void onPostExecute(Void in){
        cancelDownloadButton.setVisibility(View.GONE);
        //do smtthing
    }
}

请注意:当我直接从ListView的项目点击功能执行AsyncTask时,一切正常。但是在从AlertDialog调用时,它不会调用onPostExecute。

任何有关此问题的解决/解决方法的帮助表示赞赏..提前谢谢

2 个答案:

答案 0 :(得分:0)

尝试使用处理程序/ Runnable,以便在alertDialog的onPositiveClick回调中执行

Handler mHandler;
prtected void onCreate(Bundle sis) {
    //other code
    this.mHandler = new Handler();
}

//In your DialogInterface.OnClickListener

mHandler.post(new Runnable() { 
    @Override 
    public void run() {
        //Init and launch your asynctask here
    }
});

这不起作用的原因是必须在UI线程上调用AsyncTask并且我非常确定在正向按钮回调上调用不直接与活动上下文链接(假设该任务在ListView的onItemClick回调中起作用) )

答案 1 :(得分:0)

经过几个帖子后,看起来AsyncTask在AlertDialog中无法正常工作。 所以我通过“愚弄”Android来实现这一目标。 Noe我正在doInBackground函数中做所有事情,并且感觉好像preexecute / postexecute被调用。 以下是我的所作所为:

1)从AsyncTask中删除了onPreExecute()和onProgressUpdate()。 2)定义doInBackground之类的东西:

protected Void doInBackground(Void... vd){
publishProgress("I_AM_STARTED");
//Other Work
publishProgress("I_AM_DONE");
}

3)定义onProgressUpdate类似:

protected void onProgressUpdate(String... msg) {
if(msg[0].equals("I_AM_STARTED")){
//code for onPreExecute
}
else if(msg[0].equals("I_AM_DONE")){
//code for onPostExecute
}
else{
//normal code
}
}

希望这对像我这样陷入困境的人有所帮助。 : - )

相关问题