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

做~自己de王妃 提交于 2019-12-01 05:30:13

Use boolean cancel (boolean mayInterruptIfRunning).

Make the asyncTask a class variable.

AsyncTask translate_ = new translate();

Then call it like so in place.

translate_.execute(textToTranslate.getText().toString(), "az");

Within the button onclick cancel the asyncTask.

translate_.cancel(true);

Then check within your loop that the task has been cancelled and set a break.

for (int i=0; i<translatedTextObj.length(); i++) {
    translatedText = translatedTextObj.getString(i);
    Log.d("translatedText", translatedText);
    if(isCancelled())
        break;
}

These questions also give some more variety in how to choose to do this:

Android - Cancel AsyncTask Forcefully

How to stop asynctask thread in android?

It would be wise to manage the task on backpress also. Perhaps cancel the task in the activity onpause.

public void listenButtonOnClick(final View view) {
    // aiService.startListening();
    // add more details in here

    if (aiService != null) {
        switch (currentState) {
            case normal:
                aiService.startListening();
                break;
            case busy:
                aiService.cancel();
                translate_.cancel(true);
                break;
            default:
                aiService.stopListening();
                translate_.cancel(true);
                break;
}

Or cancel the task when the aiservice is cancelled or stopped.

@Override
public void onListeningStarted() {}

@Override
public void onListeningCanceled() {
    translate_.cancel(true);
}

@Override
public void onListeningFinished() {
    translate_.cancel(true);
}

There's other ways it can be managed, explore your options. Have a good look at this and the SO questions linked above.

If you have a look at the docs.

apiai-android-client/ailib/src/main/java/ai/api/ui/AIButton.java

@Override
protected void onClick(final View v) {
    super.onClick(v);

    if (aiService != null) {
        switch (currentState) {
            case normal:
                aiService.startListening();
                break;
            case busy:
                aiService.cancel();
                break;
            default:
                aiService.stopListening();
                break;
        }
    }

}

It's clear from our discussion you're struggling with how to implement this.

Please study this apiai-android-client/ Android SDK for api.ai/ Tutorial

Go back to the basics. Forget about the ai.api for a while and learn the basics. Browse around, read the docs Input Events read questions and apply the programming principles. For example:
Android button onclick override

Beyond this I cannot help you any further.

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);

   }
}

Try to set resultListener for your AIButton and implement onCancelled() method canceling your AsyncTasks from there.

To mark an AsyncTask as required to stop you should call

asyncTask.cancel(false);

And in your AsyncTask you have to check periodically whether it was cancelled or not by calling isCancelled(). If this method returns true you should stop execution.

if you want to cancel the AsyncTask class translate.cancel()

if you want to cancel the server request I recommend to use OKHTTP has a cancel request feature

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!