Ideal way to cancel an executing AsyncTask

前端 未结 9 2125
不思量自难忘°
不思量自难忘° 2020-11-22 05:54

I am running remote audio-file-fetching and audio file playback operations in a background thread using AsyncTask. A Cancellable progress bar is sh

9条回答
  •  死守一世寂寞
    2020-11-22 06:43

    Our global AsyncTask class variable

    LongOperation LongOperationOdeme = new LongOperation();
    

    And KEYCODE_BACK action which interrupt AsyncTask

       @Override
        public boolean onKeyDown(int keyCode, KeyEvent event) {
            if (keyCode == KeyEvent.KEYCODE_BACK) {
                LongOperationOdeme.cancel(true);
            }
            return super.onKeyDown(keyCode, event);
        }
    

    It works for me.

提交回复
热议问题