0

I have AlarmReceiver(BrodcastReceiver) that starts DownloadService(Service) after check if service is not running state

context.startDownloadServiceIfNotRunning()// kotlin extension function check service state

after this line i am sending otto event to previously started service

BusStation.getBus().post(DownloadEvent())  // Actually this line fired before start of service

but download service is not receiving this event even it is registerd for event.

after Debugging i have found that Otto event is firing before service actually starts therefore it is not receiving any event

so is there any way to know when android Service is Started properly to Receive events by otto

1 Answers1

0

You can add parameter to intent. How about use parameters, not the Otto?

Intent serviceIntent = new Intent(this,DownloadService.class); 
serviceIntent.putExtra("Key", "Value");
startService(serviceIntent);

and, onStart of service, you can use parameters.

@Override
public void onStart(Intent intent, int startId) {
    super.onStart(intent, startId);
    Bundle extras = intent.getExtras();

    if(extras == null) {
        // Do something
    } else {
        String value = (String) extras.get("Key");
        if (value.equals("Value")) {
            // Do same thing you want to do with Otto event.
        }
    }
}


Edited: If your event is not a time-critical one, you can simply delay your event.

new Handler().postDelayed(new Runnable() {
    @Override
    public void run() {
        BusStation.getBus().post(DownloadEvent());
    }
},1000);

Otherwise, you need to make your own message queue.

Stanley Ko
  • 3,383
  • 3
  • 34
  • 60
  • but also when app is running onStart of app i am starting this service in background after that in between i send otto events to DownloadService. so in onStart i don't have any value to pass in intent so how to handle this case – Kulwinder Singh Rahal Jul 28 '17 at 09:04