I have a question about GcmListenerService.
I have a service which receives GCM messages:
public class MyGcmListener extends GcmListenerService {
@Override
public void onMessageReceived(String from, Bundle data) {
Log.i("mytag", "message received");
....
}
}
Usually the main activity of my app startes it:
context.startService(new Intent(context, MyGcmListener.class));
But it seems that sometimes two service objects are created and a receiceive doubled log messages:
01-07 17:15:02.090 3655-3668/package: message received
01-07 17:15:02.090 3655-3668/package: message received
So I want to try if the service is already active when I start it from main activity:
if (!MyGcmService.isActive())
context.startService(new Intent(context, MyGcmListener.class));
One of the solutions could be to make a variable boolean active
which could be set true
in onStartCommand()
method and false
- in onDestroy()
(like here). But GcmListenerService does not have onStartCommand()
method.
So how can I implement it? Have already seen such a problem?