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
:
<receiver
android:name=".receivers.GCMReceiver"
android:permission="com.google.android.c2dm.permission.SEND">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<category android:name="com.your.package" />
</intent-filter>
</receiver>
Receiver could look like this:
public class GCMReceiver extends WakefulBroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
ComponentName comp = new ComponentName(context.getPackageName(), GCMService.class.getName());
startWakefulService(context, (intent.setComponent(comp)));
}
}
This receiver launches your Service
(GCMService).
Post a Comment for "Background Service In Android"