-1

i have implemented code form the below link to check the idle time of the application How to intent to another page on android/pop up a message from idle time?

Instead using thread i used asyntask...Now my problem once it reaches the idle time..i want to show dialog to the user application is end relogin from the login activity.. How can i call dialog from the 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)

}

}
Community
  • 1
  • 1
vijay
  • 119
  • 1
  • 2
  • 8

2 Answers2

0

You can do it by calling the dialog from either one of the methods except the doInBackground method.

You may call it in the onPreExecute and show the dialog there and after your background task is done you can cancel it from the onPostExecite method. If you want even more control you can also do it using onProgressUpdate. Just dispatch the progress from your background task by calling publishProgress and overwrite the onProgressUpdate method and do whatever you want there.

This is an example taken right out of the 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");
     }
 } 
achie
  • 4,716
  • 7
  • 45
  • 59
-1

The Asynctask need to get the Context. If your Asynctask is embeded into the activity, just call the java Activity.this as a context. You can also put a context as a field in the Asynctask and then give it as an arg to Asynctask.

You can call the Dialog.show in the onPostExecute, it's on UI Thread.

This sample AsyncTask is embeded into an activity

public class AsyncDialogBuilder extends 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;
        }
    }
}
Dam
  • 231
  • 2
  • 17