I want to do stuff only when my service gets completely stopped.
So I made a public static variable initialized it to false, in onCreate()
, to true and in onDestroy()
, to false.
In my activity:
if(TripService.trip_service_state)
{
stopService(newIntent(ProfileActivity.this,TripService.class));
while (TripService.trip_service_state);
do_stuff();
}
In my service:
public static boolean trip_service_state=false;
onCreate()
{
super.onCreate();
trip_service_state=true;
}
onDestroy()
{
trip_service_state=false;
super.onDestroy();
}
Now when my service is running, app gets NOT RESPONDING message.
Since a Service
is executed on the same thread as an Activity
, the code which would set the flag to false can't be executed: the Activity method does never stop. That's why you get an ANR.
You can send a Broadcast
from the Service
to the Activity
via LocalBroadcastManager
instead. (The Activity would have to dynamically register and unregister a corresponding BroadcastReceiver
)