0

I have a big problem with Android KitKat and Alarm Manager.

All my apps work with a service that always run in background without Android kill it.

Before Android 4.4 KitKat the solution I found was to start the service through a BroadcastReceiver triggered by an AlarmManager.

...

Intent intent = new Intent(c, MyReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(c, 0, intent, PendingIntent.FLAG_ONE_SHOT);
        AlarmManager am = (AlarmManager) c.getSystemService(Context.ALARM_SERVICE);

        if (Build.VERSION.SDK_INT<Build.VERSION_CODES.KITKAT) {
            am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), pendingIntent);
        } else {
            setAlarmFromKitkat(am, System.currentTimeMillis(), pendingIntent);
        }
...




@TargetApi(19)
    private static void setAlarmFromKitkat(AlarmManager am, long ms, PendingIntent pi){
        am.setExact(AlarmManager.RTC_WAKEUP, ms, pi);
    }

...


public class MyReceiver extends BroadcastReceiver {

     @Override
     public void onReceive(Context context, Intent intent) {

            Intent service = new Intent(context, MyService.class);
            context.startService(service);
     }

}

On Android 4.4 KitKat by this solution I can start service but after some time Android kill it!

Is there a way to have a Service that works in background without Android 4.4 KitKat kill it?

Many Thanks

Meroelyth
  • 5,310
  • 9
  • 42
  • 52
  • The issue with background service getting is not specifically related to the AlarmManager. See following question http://stackoverflow.com/q/20636330 – Muzikant Jan 13 '14 at 05:22
  • Disregard my previous comment. Take a look at this answer: http://stackoverflow.com/a/21157035/624109 – Muzikant Jan 16 '14 at 08:48

2 Answers2

1

For Android Kitkat version

If your app uses AlarmManager...

When you set your app's targetSdkVersion to "19" or higher, alarms that you create using either set() or setRepeating() will be inexact.

To improve power efficiency, Android now batches together alarms from all apps that occur at reasonably similar times so the system wakes the device once instead of several times to handle each alarm.

If your alarm is not associated with an exact clock time, but it's still important that your alarm be invoked during a specific time range (such as between 2pm and 4pm), then you can use the new setWindow() method, which accepts an "earliest" time for the alarm and a "window" of time following the earliest time within which the system should invoke the alarm.

If your alarm must be pinned to an exact clock time (such as for a calendar event reminder), then you can use the new setExact() method.

This inexact batching behavior applies only to updated apps. If you've set the targetSdkVersion to "18" or lower, your alarms will continue behave as they have on previous versions when running on Android 4.4.

Original Source: http://developer.android.com/about/versions/android-4.4.html

Community
  • 1
  • 1
Lucifer
  • 29,392
  • 25
  • 90
  • 143
1

in kitkat,use the code snippet below to restart te service automatically:

@Override
public void onTaskRemoved(Intent rootIntent) {
    // TODO Auto-generated method stub
    Intent restartService = new Intent(getApplicationContext(),
            this.getClass());
    restartService.setPackage(getPackageName());
    PendingIntent restartServicePI = PendingIntent.getService(
            getApplicationContext(), 1, restartService,
            PendingIntent.FLAG_ONE_SHOT);
    AlarmManager alarmService = (AlarmManager)getApplicationContext().getSystemService(Context.ALARM_SERVICE);
    alarmService.set(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime() +1000, restartServicePI);

}
r_allela
  • 792
  • 11
  • 23