I am new to Android development and have googled a lot. Unfortunately without success.
My problem is the following: I want to retrieve data from the device every 5 minutes in the background and then process it further. The whole thing should run 24/7.
Therefore I have created a Foreground Service which also shows a notification. The service starts with START_STICKY.
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
setSERVICE_NOTIFICATION_ID(R.string.tab_text_locationprofile);
helperNotification = new HelperNotification(this);
startForeground(R.string.tab_text_locationprofile, helperNotification.createForegroundNotification(this, R.string.tab_text_locationprofile));
doWork();
return START_STICKY;
//return START_REDELIVER_INTENT;
}
Additionally I have created the permission in the Android Manifest.
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
Maybe it is interesting to know how I stop the service.
public void onDestroyDefaults(){
helperNotification.cancelNotification(SERVICE_NOTIFICATION_ID);
//stopForeground(true);
//stopSelf();
}
The whole thing runs fine so far. But after 24 hours the service and the app will be killed.
Many people have this problem, but I haven't found a solution for me anywhere. Can anyone help me here? Maybe someone can tell me another solution to do such things.