Handlers in Android

前端 未结 2 1115
野性不改
野性不改 2021-01-07 06:53

What are Handler used in android proggraming for? How do we use them to dismiss the ProgressDialog?

I have referred few things from the Net regerding this but couldn

相关标签:
2条回答
  • 2021-01-07 07:13

    Use AsyncTask instead. It has a function called onPostExecute where in you can dismiss the Progress Dialog.

    @Override
        protected void onPostExecute(Void result) {
            Toast.makeText(ctx, "Completed Synch with Server", Toast.LENGTH_SHORT)
                    .show();
    
                    mProgressDialog.dismiss();
    
        }
    

    Handlers are used to run a small section of code at a predetermined time. Typically one uses AlarmManager to launch intents (activities, services, broadcasts), but if you are interested in running only a small section of code you can use handlers:

    if(condition == true){
    
        Handler timer = new Handler();
        timer.postDelayed(task, (5 * 60 * 1000);
    }
    
    ---
    
          private Runnable task = new Runnable() {
            public void run() {
                mProgressDialog.dismiss();
            }
        };
    

    I'd strongly recommend using a AsyncTask for anything thread related on Android.

    0 讨论(0)
  • 2021-01-07 07:34

    Handlers are used for communication between the UI and background thread. Basically Handler will send messages and runnables on to the message Queue of a thread.

    0 讨论(0)
提交回复
热议问题