• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Implement Service Or AsyncTask or what?

 
Trupti Mehta
Ranch Hand
Posts: 79
Android Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I need a class with a Timer that chekcs the status after regular intervals. If the status is false, should inform the calling UI class. The Timer class will be started and stopped from the UI Class (Activity) only.

I am confused - don't know what will be best to implement the Tiemr class as. Should it be implemetned as a Service, or as as AsyncTask (as it will be running in background only) or something else. And how to inform the UI class about change of status from the Timer Class. Like if the status is found to be false, then only inform the Timer class else ignore and keep on checking on regular intervals.

Can anyone guide me for this ? I am a newbie in Android and couldn't make what to use and implement. Any help, guidance is highly appreciated.

Thanks
 
Swastik Dey
Rancher
Posts: 1602
5
Android Java Java ME
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
May be you could create an interface to send notification to your Activity

 
Trupti Mehta
Ranch Hand
Posts: 79
Android Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Swastik Dey wrote:
public class ServiceHandler extends TimerTask{
private UINotifier _notifier;
public ServiceHandler(UINotifier notifier){
this._notifier=notifier;
}

}


public class MyActivity extends Activity implements UINotifier{
//requiered methods

private void startTimer(){
ServiceHandler sh=new ServiecHandler(sh);
Timer tmr=new Timer();
tmr.scheduleAtFixedRate(sh,0,1000);
}

public void notify(){
//do something
}
}
[/code]


Thanks Swastik. Wondering bout just 1 thing. In new ServiceHandler(sh) why are you passing the object of Service Handler itself. Shouldn't that be "this" instead of "sh" as the class implements UINotifier & ServiceHandler needs an object of UINotifier.

Is it your mistake or I am getting it wrong ! Can you please clear it .

 
Swastik Dey
Rancher
Posts: 1602
5
Android Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, you are right. It should be this. I did it in a hurry.
 
Trupti Mehta
Ranch Hand
Posts: 79
Android Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Swastik, instead of UINotifier I had to use Handler. UINotifier notifies it properly, but I als have ASyncTask in my Activity and with that all was throwing exception for "Can't hanlde .... Looper.prepare..". Use of Handler instead of Timer & TimerTask and Handler.Callback instead of UINotifier did the work smoothly.

This info might be useful to someone else also and to you too, Swastik. Refer http://developer.android.com/resources/samples/TicTacToeLib/src/com/example/android/tictactoe/library/GameActivity.html for best example to show how and why to use Handler.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic