Android: How to tell if the service has finished running when called from a non-activity class

advertisements
  1. I have Push Notification receiver class
  2. Am calling LocationService , a service to check for me the location of user and calculate if user is within 1 km radius, and updates a variable in shared pref.
  3. I display the notification

Question : How do I know service has finished execution ? Please note that I can not use bindservice() as service is called from a non activity class

public class PushReceiver extends ParsePushBroadcastReceiver  {
    public   static Context mContext;

        @Override
        public void onPushReceive(Context context , Intent intent) {
            mContext = MyApp.getContext();
            //startService(new Intent(this, LocationService.class));
            Intent i1 = new Intent (context, LocationService.class);
            context.startService(i1);

// here I want to check if LocationService has finished execution
 if (1 == 1){
           super.onPushReceive(context, intent);
            }
        }// end of onPushRecieve

}


Just fire off your notification inside your custom LocationService.

Plus, you don't want to do anything async inside a BroadcastReceiver as it will exit before the async method performs any callback.

See BroadcastReceiver

A BroadcastReceiver object is only valid for the duration of the call to onReceive(Context, Intent). Once your code returns from this function, the system considers the object to be finished and no longer active.

This has important repercussions to what you can do in an onReceive(Context, Intent) implementation: anything that requires asynchronous operation is not available, because you will need to return from the function to handle the asynchronous operation, but at that point the BroadcastReceiver is no longer active and thus the system is free to kill its process before the asynchronous operation completes.

In particular, you may not show a dialog or bind to a service from within a BroadcastReceiver. For the former, you should instead use the NotificationManager API. For the latter, you can use Context.startService() to send a command to the service.A BroadcastReceiver object is only valid for the duration of the call to onReceive(Context, Intent). Once your code returns from this function, the system considers the object to be finished and no longer active.

This has important repercussions to what you can do in an onReceive(Context, Intent) implementation: anything that requires asynchronous operation is not available, because you will need to return from the function to handle the asynchronous operation, but at that point the BroadcastReceiver is no longer active and thus the system is free to kill its process before the asynchronous operation completes.

In particular, you may not show a dialog or bind to a service from within a BroadcastReceiver. For the former, you should instead use the NotificationManager API. For the latter, you can use Context.startService() to send a command to the service.

In the event that you have no control over the custom LocationService code then you should probably just start your own Service and poll for the shared pref value your waiting for. This is however not ideal.

I notice the Parse receiver you are extending does not extend WakefulBroadcastReceiver which may be an issue - check out the docs for this...