如何在android中的活动中显示asynctask对话框?

时间:2012-07-31 09:33:45

标签: android

我已经通过以下链接实现了代码来检查应用程序的空闲时间 How to intent to another page on android/pop up a message from idle time?

相反使用我使用asyntask的线程...现在我的问题一旦达到空闲时间..我想向用户应用程序显示对话框是从登录活动结束重新登录.. 如何从asynctask onpostExcute

调用对话框
public class session extends AsyncTask<Void,Void,Void> {
private static final String TAG=session.class.getName();
private long lastUsed;
private long period;
private boolean stop;
Context context;


final Dialog dialog = new Dialog(context);
@Override
protected Void doInBackground(Void... params) {
    // TODO Auto-generated method stub
    //here i do the process.......
}
@Override
protected void onPostExecute(Void x){
        //stuff to be done after task executes(done on UI thread)

    // For Dialog Button**********************************
    dialog.setContentView(R.layout.dialog);

    dialog.setTitle("Result");

    final TextView dialogtxt = (TextView) dialog
            .findViewById(R.id.textView1);

    final Button closeButton = (Button) dialog
            .findViewById(R.id.button1);

    closeButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            dialog.dismiss();
        }
    });

    dialogtxt.setText("session time out");
    dialog.show();

    // ****************************************************

}
@Override
protected void onPreExecute(){
        //stuff to be done after task executes(done on UI thread)

}

}

2 个答案:

答案 0 :(得分:0)

您可以通过从除doInBackground方法之外的任何一种方法调用对话框来完成此操作。

您可以在onPreExecute中调用它并在那里显示对话框,在完成后台任务后,您可以从onPostExecite方法中取消它。如果您想要更多控制,您也可以使用onProgressUpdate来完成。只需通过调用publishProgress从后台任务调度进度并覆盖onProgressUpdate方法并在那里做任何你想做的事。

这是一个直接从docs开始的例子。

private class DownloadFilesTask extends AsyncTask<URL, Integer, Long> {
     protected Long doInBackground(URL... urls) {
         int count = urls.length;
         long totalSize = 0;
         for (int i = 0; i < count; i++) {
             totalSize += Downloader.downloadFile(urls[i]);
             publishProgress((int) ((i / (float) count) * 100));
             // Escape early if cancel() is called
             if (isCancelled()) break;
         }
         return totalSize;
     }

     protected void onProgressUpdate(Integer... progress) {
         setProgressPercent(progress[0]);
     }

     protected void onPostExecute(Long result) {
         showDialog("Downloaded " + result + " bytes");
     }
 } 

答案 1 :(得分:-1)

Asynctask需要获取Context。 如果您的Asynctask嵌入到活动中,只需将java Activity.this作为上下文调用。 您还可以将上下文作为字段放在Asynctask中,然后将其作为arg提供给Asynctask。

您可以在onPostExecute中调用Dialog.show,它位于UI线程上。

此示例AsyncTask嵌入到活动

公共类AsyncDialogBu​​ilder扩展了AsyncTask {

    private Context context = DriverOnTripActivity.this;
    private final AlertDialog.Builder dialog = new AlertDialog.Builder(context);
    private Integer remoteAllWaitinOnCount;

    public Context getContext() {
        return context;
    }

    public void setContext(Context context) {
        this.context = context;
    }

    @Override
    protected void onPreExecute() {
    }

    @Override
    protected Integer doInBackground(Integer... integers) {
        remoteAllWaitinOnCount = User.getRemoteAllWaitinOnCount(latestClosestKojo.getRemoteId());
        if (remoteAllWaitinOnCount > 0) {
            try {
                makeDialog();
            } catch (Exception e) {
                e.printStackTrace();
            }
            return 100;
        } else {
            return 99;
        }
    }

    private void makeDialog() {
        dialog.setTitle(latestClosestKojo.getName()
                + " - "
                + remoteAllWaitinOnCount
                + " Kojoalas");
        dialog.setPositiveButton("S'arreter", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                isDialogPrompted = false;
                dialogInterface.dismiss();
                goToOnBoardingActivity();
            }
        });
        dialog.setNegativeButton("Ignorer", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                isDialogPrompted = false;
                dialogInterface.dismiss();
            }
        });
    }

    @Override
    protected void onPostExecute(Integer integers) {
        if (integers >= 100 && dialog != null) {
            dialog.show();
            isDialogPrompted = true;
        }
    }
}