Skip to content Skip to sidebar Skip to footer

Background Service In Android

I'm working on an app where it has it's own DB and will be syncing with the backend via GCM, I'm thinking of using background service but I'm not sure if this is the right way to t

Solution 1:

In general your idea is ok. But you don't need to have always running background Service. Just create WakefulBroadcastReceiver and add it into your Manifest:

<receiverandroid:name=".receivers.GCMReceiver"android:permission="com.google.android.c2dm.permission.SEND"><intent-filter><actionandroid:name="com.google.android.c2dm.intent.RECEIVE" /><categoryandroid:name="com.your.package" /></intent-filter></receiver>

Receiver could look like this:

publicclassGCMReceiverextendsWakefulBroadcastReceiver {
    @OverridepublicvoidonReceive(Context context, Intent intent) {
        ComponentNamecomp=newComponentName(context.getPackageName(), GCMService.class.getName());
        startWakefulService(context, (intent.setComponent(comp)));
    }
}

This receiver launches your Service(GCMService).

Post a Comment for "Background Service In Android"