-1

I want to call a method every time when the battery goes to 50%, even when the app is closed or the screen is turned off. I tried Service and BroadcastReceiver but when I remove the app from recent apps list the Service is stopped and I doe't get calls of this method anymore. How can I save it in backgroud?

User
  • 305
  • 1
  • 3
  • 15
  • Are you registering the receiver in your manifest? Can you post relevant parts of your BroadcastReceiver, Service and AndroidManifest.xml? – Bryan Dunlap Mar 07 '14 at 18:41
  • Just looked at your related question: http://stackoverflow.com/questions/22257310/android-keep-broadcastreceiver-in-background.. Are you returning `Service.START_STICKY` from your service's `onStartCommand()` implementation? This tells the system to restart your service any time it is killed. – Bryan Dunlap Mar 07 '14 at 19:11
  • Yes, I did it, but the service is stopped when I remove the app from recent apps. – User Mar 07 '14 at 19:25
  • Are you testing this on a device running KitKat? If so take a look here: https://groups.google.com/forum/#!topic/android-developers/H-DSQ4-tiac indications there is a bug in KitKat that kills sticky services of apps removed from recent task list. – Bryan Dunlap Mar 07 '14 at 19:54
  • Yes, I use KitKat. Does this bug happen on older versions? – User Mar 07 '14 at 19:55
  • Edit, Looks like it could be present in Jelly Bean as well as KitKat. See answer. – Bryan Dunlap Mar 07 '14 at 20:21

2 Answers2

0

Apparently, there was a bug introduced with KitKat (EDIT: and Jelly Bean?) which prevents sticky services from being restarted when an app is swiped away from the recent apps list. see https://groups.google.com/forum/#!topic/android-developers/H-DSQ4-tiac

This workaround, provided in the above link by user Musikant, should resolve the issue with devices running KitKat. Implement the following in your Service class:

    public void onTaskRemoved(Intent rootIntent) {
        Intent restartServiceIntent = new Intent(getApplicationContext(), this.getClass());
        restartServiceIntent.setPackage(getPackageName());

        PendingIntent restartServicePendingIntent = PendingIntent.getService(getApplicationContext(), 1, restartServiceIntent, PendingIntent.FLAG_ONE_SHOT);
AlarmManager alarmService = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);
        alarmService.setExact(
                AlarmManager.ELAPSED_REALTIME,
                SystemClock.elapsedRealtime() + 1000,
                restartServicePendingIntent);

        super.onTaskRemoved(rootIntent);
    }

See START_STICKY does not work on Android KitKat for other possible solutions.

Community
  • 1
  • 1
Bryan Dunlap
  • 1,156
  • 10
  • 7
0

Best Solution I get for this:-

even though all the suggested answers on Stack overflow for the same question ,if doesnot help you in any way read this: this just happens in some devices not in all and if this is happening with your app only in that particular device please see the autostart permission for your app is granted or not under Settings. because

brodcast are still delivered to our app but without the permission granted for autostart app wont come to foreground

.