I need to have ALWAYS a background service that will synchronize my Android application and a server. I know how to launch it through my application, but when the Android tu
use for starting your service when the device turns on.
In AndroidManifest.xml:
Add permission in your AndroidManifest.xml as:
In code part BootBroadcastReceiver:
public class BootBroadcastReceiver extends BroadcastReceiver {
static final String ACTION = "android.intent.action.BOOT_COMPLETED";
@Override
public void onReceive(Context context, Intent intent) {
// BOOT_COMPLETED” start Service
if (intent.getAction().equals(ACTION)) {
//Service
Intent serviceIntent = new Intent(context, StartOnBootService.class);
context.startService(serviceIntent);
}
}
}
EDIT: if you are talking about device screen on/off then you need to register and for starting your service when user is present or screen is on.