I have a service and I need to check if it's still running after some time from a static context. [I noticed this happens only on Android KitKat]
What I did:
My service:
public class Servizio extends Service {
public static Service servizio;
[...Other things...]
@Override
public void onCreate() {
super.onCreate();
servizio = this;
scheduleMyAlarmToDoSomethingAfterSomeTime();
}
[...Other things...]
}
Then I launch the service from the main Activity in onCreate:
@Override
protected void onCreate(Bundle savedInstanceState) {
[...]
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ServizioUtility.toggleService(this, true);
[...]
}
Then I check if it's null after a minute (with an alarm receiver whose alarm was launched by the service. Note that the receiver works fine and the following code is called):
if (Servizio.servizio!=null) {// FIXME
[...]
//THIS IS NOT CALLED IN THE 2ND SCENARIO
}
There are 2 scenario:
- I run the activity, wait for the alarm to be launched and Servizio.servizio is not null.
- I run the activity, "kill" it using the
, wait for the alarm receiver to be called, Servizio.servizio is now null. Note that the service, on the other hand, is still running (checked from Settings->App->Running menu).
What's happening in the 2nd scenario?