开发者

Async Task Can it partially run?

It is de开发者_C百科finitely working as postData sends to my site and forwards an email which it is doing. But loadingDialog does not execute ( it may be but it is a very quick process). The last process sentdialog is not executing as it brings up a new dialog saying sent and is not happening. I have this script for the async

    protected class sendReport extends AsyncTask<Void, Void, Void> {
    protected void onProgressUpdate()  {
         progressdialog();
    }
    protected void onPostExecute() {
        sentdialog();
        loadingDialog.dismiss();
    }
    @Override
    protected Void doInBackground(Void... arg0) {
        postData();
        publishProgress();
        return null;
    }

}

Below is the sent dialog script.

public void sentdialog(){
    //set up dialog
    final Dialog sentdialog = new Dialog(context);
    sentdialog.setContentView(R.layout.sentdialog);
    sentdialog.setTitle("Sent");
    sentdialog.setCancelable(true);

    final Button button = (Button) sentdialog.findViewById(R.id.Button01);
    button.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            sentdialog.dismiss();
        }
    });   
    sentdialog.show();
} 


EDIT: You could just do this with a handler given activity Main:

first create ONE handler per activity:

private ProgressDialog progress;
private Handler myHandler= new Handler(){
    @Override
    public void  handleMessage(Message msg){         
        switch(msg.what){
            case 0:
                progress.dismiss();
                sentDialog();
                break;
            default:
                super.handleMessage(msg);
                break;
        }
    }
};

I wrote your progressDialog, postData, sentDialog methods:

private ProgressDialog progressDialog() {
    progress= new ProgressDialog(this);
    progress.setIndeterminate(true);
    progress.setMessage("I am thinking");
    return progress;
}
private String postData() {
    try{
    Thread.sleep(1000);
    }
    catch(Exception e){;}
    Log.d(TAG,"postData");
    return "done";
}
private void sentDialog() {
    Toast.makeText(Main.this,"Sent", Toast.LENGTH_SHORT).show();
}

And called in onclick:

// DISPLAY ON CLICK HANDLER
displayButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            progressDialog().show();
        Thread thread= new Thread( new Runnable() {
            public void run() {
                postData();
                myHandler.sendEmptyMessage(0);
            }
        });
        thread.setDaemon(true);
        thread.start();
        }
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜