Bharat Gupta
Bharat Gupta

Reputation: 13

Android : How to know if service has finished execution when called from a non-activity class

  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

}

Upvotes: 1

Views: 549

Answers (2)

Bharat Gupta
Bharat Gupta

Reputation: 13

Thanks Dori It worked. LocationService is my own class . I trapped the Notfication message in PushReciever and displayed it in the Service.

public class PushReceiver extends ParsePushBroadcastReceiver  {
    public   static Context pushContext;
    public static Notification pushNoti;
        @Override
        public void onPushReceive(Context context , Intent intent) {

            pushContext = context;
            Intent i1 = new Intent (context, LocationService.class);
            pushNoti= getNotification(context,intent);
            context.startService(i1);

        }// end of onPushRecieve
}

// and in LocationService Service just fired it


    NotificationManager nm = (NotificationManager) 

    PushReceiver.pushContext.getSystemService(android.content.Context.NOTIFICATION_SERVICE);


    public void onConnected(Bundle bundle) {

            Log.d("onConnected ", "Value: ");
            mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
            nm.notify(0,PushReceiver.pushNoti);
        }

Upvotes: 0

Dori
Dori

Reputation: 18413

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...

Upvotes: 1

Related Questions