开发者

Threads and Events - what's the best way?

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 myCla开发者_运维知识库ssVariable;

   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?


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜