0

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.

Vamsi Abbineni
  • 479
  • 3
  • 7
  • 23
guest86
  • 2,894
  • 8
  • 49
  • 72

2 Answers2

2

You can create a BroadcastReceiver that will launch the service immediately after getting killed by the phone.

On your onDestroy() method in service class, start a custom BroadcastReceiver,

@Override
public void onDestroy() {
Intent in = new Intent();
    in.setAction("immortal_service");
    sendBroadcast(in);
}

Now register the BroadcastReceiver in manifest file,

<receiver android:name=".ServiceDestroyReceiver" >
        <intent-filter>
            <action android:name="immortal_service" >
            </action>
        </intent-filter>
    </receiver>

The ServiceDestroyReceiver class will start your service after getting killed,

@Override
public void onReceive(Context context, Intent intent) {
    // start the service again here
}
Prokash Sarkar
  • 11,723
  • 1
  • 37
  • 50
0

So, solution from Prokash Sarker was good, improved things, but wasn't a definitive solution.

Definitive solution which made sure my service will be active even if there's shortage of RAM (remember, my device has only 1 GB in total!) can be found here.

Basically i implemented described "Alarm" that will make sure my service is being restarted at most 2 seconds after being turned off. I also added some "extra" to intent in order to be able to recognize intents sent from "alarm service". As services in Android are singletons, in case one is already running "Alarm" won't start another.

Community
  • 1
  • 1
guest86
  • 2,894
  • 8
  • 49
  • 72