2

I have one activity and one service. My requirement is to start service from activity and in activity countdown timer will start, but problem is that I'm unable to get value from service to my activity. Please help me out.

Is any code,tutorial,example which will help me for this.

TIMER SERVICE

public class TimerService extends Service {

    MyCounter timer;

    @Override
    public void onCreate() {
        timer = new MyCounter(1 * 60 * 1000, 1000);
        super.onCreate();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        timer.start();
        return super.onStartCommand(intent, flags, startId);
    }

    private class MyCounter extends CountDownTimer {

        public MyCounter(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        @Override
        public void onFinish() {
            Toast.makeText(getApplicationContext(), "death", Toast.LENGTH_LONG)
                    .show();
            stopSelf();

        }

        @Override
        public void onTick(long millisUntilFinished) {
            Toast.makeText(getApplicationContext(),
                    (millisUntilFinished / 1000) + "", Toast.LENGTH_SHORT)
                    .show();
        }
    }

    @Override
    public void onDestroy() {
        // TODO Auto-generated method stub
        timer.cancel();
        super.onDestroy();

    }

    @Override
    public IBinder onBind(Intent arg0) {
        return null;
    }

}

what should i write in this service so i will get toast message value in my activity

  • [Here](http://developer.android.com/guide/components/bound-services.html) you have a tutorial about how to do it. Also in [this answer](http://stackoverflow.com/questions/22440064/accessing-context-from-another-thread/22443582#22443582) you have some explanations – Guillermo Merino Mar 24 '14 at 12:56

2 Answers2

2

Read about broadcast receivers.

Create a broadcast receiver in activity and register it with some IntentFilter(Set action in a string value).

private BroadcastReceiver receiver = new BroadcastReceiver() {
        public void onReceive(android.content.Context context, Intent intent) {
       Toast.makeText(context, "death", Toast.LENGTH_LONG)
                .show();
}}

Register in onResume

     IntentFilter intentFilter = new IntentFilter();    
 intentFilter.addAction("Your Action");
this.registerReceiver(receiver, intentFilter);

And in your service just call setBroadcast

 @Override
    public void onFinish() {
  Intent intent = new Intent();
intent.setAction("YourAction");
sendBroadcast(intent);
        stopSelf();

    }
vipul mittal
  • 17,343
  • 3
  • 41
  • 44
1

You can achieve this in many ways. A elegant one would be to have a controller class which sends events to your activity. You could register your Activity as observable in your onResume method and unregister it in the onPause method. Then, send the data from your service to the controller, and it pass the data to your Activity from there. For example:

//Let your activity implement this interface
interface MyObservableActivity{
    public receiveData(Data yourData);
}

//Your observer controller
class MyController{
    Vector<MyObservableActivity> observedItems;

    public void registerObservable(MyObservableActivity a){
         if(!observedItems.contains(a))
             observedItems.add(a);
    }

    public void unregisterObservable(MyObservableActivity a){
         if(observedItems.contains(a))
             observedItems.remove(a);
    }

    public void sendDataToObservers(Data d){
        for(MyObservableActivity a: observedItems){
            a.receiveData(d);
        }
    }

}

So from your service, you should call the sendDataToObservers method and you'll get it from your activity.

Sebastian Breit
  • 6,137
  • 1
  • 35
  • 53