0

I have a class which reads the calllog. This could last some minutes. So I have to use a thread if I don't want to block the gui thread.

In C# I start a thread, notify the gui thread with a event when the data is ready and give the data to the gui within the event.

But how should I do it with android? What I read so far told me that's the best to use AsyncTask with android. I use it in this way (Pseudo code):

class MyClass{
   private myClassVariable;

   private coid startTask(){
      GetDataTask data = new GetDataTask();
      data.execute(varibale);
   }

   private void displayData{
      doAnythingUsefullHere;
   }


   class GetDataTask extends AsyncTask<variables>{
      protected variable doInBackground(variable){
         return = CallLog.getData();
      }

      protected void onPostExecute(variable){
         myclassVariable = variable;
         displayData;
      }
   }
}

Works fine so far, but I can't cancel the thread in this way. I could cancel the task, but I have to check within the data collect loop of the calllog class if the onCancelled is called but this function is only known in the class GetDataTask and not in CallLog.

Is there a way to use AsyncTask and make "outside" classes cancelable? Or have I switch to Threads and events? What's the best way in this situation?

hitzi
  • 852
  • 2
  • 10
  • 23

1 Answers1

0

Could this be what you are looking for: Ideal way to cancel an executing AsyncTask ?

Community
  • 1
  • 1
lobner
  • 593
  • 1
  • 7
  • 15
  • I know this code. My problem is I don't know how I can tell the CallLog.getData() function that there is a cancel request and it should stop the loop? The CallLog.getData() is in extra class outside the class MyClass and class GetDataTask. – hitzi Jul 06 '11 at 06:40
  • I belive there is some way to make the cancel-call propergate upwards. If you in onCancelled inside the GetDataTask make a call to super.cancel(); will that maybe work? **EDIT** I think i misread it. Is it maybe a solution to in the onCancelled event, set a flag in CallLog? And then in CallLog, handle this flag in order to stop the looping. – lobner Jul 06 '11 at 06:56