I tried implementing some solutions found on "Stack" but nothing i found helped me. This is the problem: I have Asus Memopad 8 tablet with 1 GB of RAM. It runs "KitKat". Basically it constantly has about 200 MB of free RAM. For that tablet i'm trying to create a service that will monitor currently running foreground app and in case it's a particular app i choose, do certain action. In essence service (extends IntentService) does this every 2 seconds:
ActivityManager am = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE);
List<ActivityManager.RunningTaskInfo> taskInfo = am.getRunningTasks(1);
ComponentName componentInfo = taskInfo.get(0).topActivity;
return componentInfo.getPackageName();
The problem is that my service gets killed by Android while my tablet is sleeping. So, I start my service, it works ok. When i finish using my tablet i put it away. After few hours i use my tablet again - service is gone. If i use my tablet few minutes after putting it away service still works.
I tried this:
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
return START_REDELIVER_INTENT;
}
I also tried with "START_STICKY" without luck.
What should i do in order to prevent Android from turning off my service? I don't wanna run it as a "foregroundService" as it's a utility service that should be running all the time and i don't want my status bar populated with a permanent notification. I already have a notification when service detects the right foreground app running.