This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Android and the fly likes Implement Service Or AsyncTask or what? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Mobile » Android
Bookmark "Implement Service Or AsyncTask or what?" Watch "Implement Service Or AsyncTask or what?" New topic
Author

Implement Service Or AsyncTask or what?

Trupti Mehta
Ranch Hand

Joined: Oct 08, 2000
Posts: 79

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


Regards,
Trupti (SCJP)
Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1477
    
    6

May be you could create an interface to send notification to your Activity



Swastik
Trupti Mehta
Ranch Hand

Joined: Oct 08, 2000
Posts: 79

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

Joined: Jan 08, 2009
Posts: 1477
    
    6

Yes, you are right. It should be this. I did it in a hurry.
Trupti Mehta
Ranch Hand

Joined: Oct 08, 2000
Posts: 79


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.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Implement Service Or AsyncTask or what?