API.AI: How to stop AsyncTask on click of the AIButton?

前端 未结 4 2035
借酒劲吻你
借酒劲吻你 2021-01-18 10:37

I’m using AIButton in my app and I have a AsyncTask which gets executed after AIButton is clicked and receives some command and AsyncTask sometimes

4条回答
  •  后悔当初
    2021-01-18 11:19

    You have to do one following thing when canceling AsyncTask. on your button click method.

    public void AIButtonClick(View view){
      asyncTask.cancel(false);
    }
    

    and check in doInBackground if it's canceled then return from it.

        @Override
        protected String doInBackground(String... params) {     
         if(asyncTask.isCanceled()) return;
       }
    

    Also on in your onPause method you have cancel it was well.

    @Override
    public void onPause() {
        super.onPause();
         if(asyncTask!=null){
          asyncTask.cancel(false);
    
       }
    }
    

提交回复
热议问题