I am trying to implement alarm application using AlarmManager and Service
In my Activity, i have declared the following to initiate a alarmservice
Intent intent = new Intent(getApplicationContext(), OnAlarmManager.class);
PendingIntent pendingIntent = PendingIntent.getService(getApplicationContext(),
234324243, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+(i * 1000), pendingIntent);
Toast.makeText(getApplicationContext(), "Alarm set in " + i + " seconds", Toast.LENGTH_LONG).show();
My OnAlarmManager code looks like this
public class OnAlarmManager extends Service{
private static final String TAG = "OnAlarmReceiver";
private NotificationManager nm;
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Intent mainActivityIntent = new Intent(this, MainActivity.class);
PendingIntent pIntent = PendingIntent.getActivity(this, 0, mainActivityIntent, 0);
//setting up notification
Notification notification = new NotificationCompat.Builder(this)
.setContentTitle("New Notification")
.setContentText("This is a successfull app on service")
.setSmallIcon(R.mipmap.ic_launcher)
.setContentIntent(pIntent)
.build();
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.flags |= Notification.FLAG_SHOW_LIGHTS;
notification.defaults |= Notification.DEFAULT_SOUND;
notification.defaults |= Notification.DEFAULT_VIBRATE;
notification.defaults |= Notification.DEFAULT_LIGHTS;
nm.notify(0, notification);
Toast.makeText(getApplicationContext(), "Alarm has begun", Toast.LENGTH_LONG).show();
Log.i(TAG, "Alarm has begun");
return START_STICKY;
}
}
Now when i try running this, the notification is triggered at given time in device running on JellyBean even when i close the app after setting the alarm. But in the device running on Marshmallow, the service class is never triggered when the app is closed
Is the something i am missing here?