I'm developing an app that synchronize your local date with the cloud. So I need to check automatically when the O.S. starts my local data to get the new camera files to upload to the cloud. And to do each 10 minutes a local data checking to get new files inside and uploading them to the cloud.
I'm having a trouble with the Service, because it starts only when I open the app, later I can close this app and it keeps in background (well!).But I need that the service starts when the operative system starts too.
I have defined in my Manifest.xml:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-permission android:name="android.permission.WAKE_LOCK" />
<service android:name=".receiver.UploadDeleteService" android:exported="false"/>
<receiver
android:name=".receiver.AlarmReceiver" android:process=":remote" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
Also I use an Interactor (from MVP architecture) where I define the alarm manager and the period of time:
public class ServiceInteractorImpl implements ServiceInteractor {
private Context context;
public ServiceInteractorImpl(Context context){
this.context = context;
}
@Override
public void launchService() {
// Construct an intent that will execute the AlarmReceiver
Intent intent = new Intent(context, AlarmReceiver.class);
// Create a PendingIntent to be triggered when the alarm goes off
final PendingIntent pIntent = PendingIntent.getBroadcast(context, AlarmReceiver.REQUEST_CODE,
intent, PendingIntent.FLAG_UPDATE_CURRENT);
long firstMillis = System.currentTimeMillis(); // alarm is set right away
AlarmManager alarm = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
// First parameter is the type: ELAPSED_REALTIME, ELAPSED_REALTIME_WAKEUP, RTC_WAKEUP
// Interval can be INTERVAL_FIFTEEN_MINUTES, INTERVAL_HALF_HOUR, INTERVAL_HOUR, INTERVAL_DAY
Calendar cal = Calendar.getInstance();
cal.add(Calendar.SECOND, 30);
alarm.setInexactRepeating(AlarmManager.RTC_WAKEUP, firstMillis,
60*1000, pIntent);
}
}
My AlarmReceiver is:
public class AlarmReceiver extends BroadcastReceiver {
public static final int REQUEST_CODE = 12345;
@Override
public void onReceive(Context context, Intent intent) {
Intent i = new Intent(context, UploadDeleteService.class);
context.startService(i);
}
}
And my Service:
public class UploadDeleteService extends IntentService implements ApiConnector.GetObjectListener{
private RemoteInteractor remoteInteractor;
private LocalInteractor localInteractor;
private LocalDBInteractor localDBInteractor;
List<String> pathsToUpload;
private String uploading;
public UploadDeleteService(String name) {
super(name);
}
public UploadDeleteService() {
super("UpdateDeleteService");
}
@Override
protected void onHandleIntent(Intent intent) {
localInteractor = new LocalInteractorImpl(getApplicationContext());
localDBInteractor = new LocalDBInteractorImpl(getApplicationContext());
remoteInteractor = new RemoteInteractorImpl(getApplicationContext());
uploading = "";
remoteInteractor.checkNews(this);
}
}
Hope it helps to understand my trouble...Thanks a lot!