Neha Mittal
Lurker
I developed an app in which a background service for tracking location should run always (every half an hour sending location coordinates to server).My app is working fine with android 5 and android 7(in doze mode as well) but in android 6 the service stops working when i exit the app.Boot receive is also not working in android 6. Using alarmmanager on main thread for starting service.
Below is the alarmmanager function i m using on my main thread.
public void AlarmManagerFunc()
{
Context ctx = getBaseContext();
AlarmManager am = (AlarmManager) ctx.getSystemService(Context.ALARM_SERVICE);
long interval = 1000 * 60 * 15; //15 minutes in milliseconds
long period = 1000;
Intent serviceIntent = new Intent(ctx, MyService.class);
PendingIntent servicePendingIntent =
PendingIntent.getService(ctx,
1, // integer constant used to identify the service
serviceIntent,
PendingIntent.FLAG_CANCEL_CURRENT); // FLAG to avoid creating a second service
if (android.os.Build.VERSION.SDK_INT >= 23) {
am.setAndAllowWhileIdle(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + period, servicePendingIntent);
}
}
else { am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + period
,interval, servicePendingIntent);
}
Any one facing the same issue with marshmallow?Can any one suggest me what i m doing wrong for marshmallow?
Below is the alarmmanager function i m using on my main thread.
public void AlarmManagerFunc()
{
Context ctx = getBaseContext();
AlarmManager am = (AlarmManager) ctx.getSystemService(Context.ALARM_SERVICE);
long interval = 1000 * 60 * 15; //15 minutes in milliseconds
long period = 1000;
Intent serviceIntent = new Intent(ctx, MyService.class);
PendingIntent servicePendingIntent =
PendingIntent.getService(ctx,
1, // integer constant used to identify the service
serviceIntent,
PendingIntent.FLAG_CANCEL_CURRENT); // FLAG to avoid creating a second service
if (android.os.Build.VERSION.SDK_INT >= 23) {
am.setAndAllowWhileIdle(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + period, servicePendingIntent);
}
}
else { am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + period
,interval, servicePendingIntent);
}
Any one facing the same issue with marshmallow?Can any one suggest me what i m doing wrong for marshmallow?