How to completely kill/remove/delete/stop an AsyncTask

前端 未结 7 2100
终归单人心
终归单人心 2020-11-28 09:57

I made an app that downloads videos from our server. The issue is:

When i cancel the downloading i call:

myAsyncTask.cancel(true)

I

7条回答
  •  渐次进展
    2020-11-28 10:29

    I have used with success inside an activity with TextView onclick ...

            //inside the doInBackground() ...
    
            try {
            while (true) {
            System.out.println(new Date());
    
            //should be 1 * 1000 for second
            Thread.sleep(5 * 1000);
            if (isCancelled()) {
                  return null;
            }
              }
    
            } catch (InterruptedException e) {
    
            }
    

    and in my onCreate() ...

         //set Activity
         final SplashActivity sPlashScreen = this;
    
         //init my Async Task
         final RetrieveFeedTask syncDo = new RetrieveFeedTask();
         syncDo.execute();
    
         //init skip link
         skip_text = (TextView) findViewById(R.id.skip_text);
         skip_text.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
    
                //cancel Async
                syncDo.cancel(true);
    
                //goto/start another activity
                Intent intent = new Intent();
                intent.setClass(sPlashScreen, MainActivity.class);
                startActivity(intent);
                finish();
    
            }
        });
    

    and my xml TextView element ...

       
    

提交回复
热议问题