Reputation: 21
I made an AlarmManager which starts a Service at a specific time and every 30 Minutes after that. The Service starts a notification. The Problem is, that I only get the Notification after I restarted the Application. The notification works but the AlarmManger doesn't because I get the Notification everytime after I restarted the app. It doesn't matter when I restart the app, I get the notification every time.
Heres my AlarmManger code:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent myIntent = new Intent(MainActivity.this , MyAlarmService.class);
alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
pendingIntent = PendingIntent.getService(MainActivity.this, 0, myIntent, 0);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 19);
calendar.set(Calendar.MINUTE, 4);
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
AlarmManager.INTERVAL_HALF_HOUR, pendingIntent);
}
Here is my Notification Code:
public class MyAlarmService extends Service {
@Override
public void onCreate() {
Intent intent1 = new Intent(this, MainActivity.class);
intent1.putExtra("EXTRA_EVENT_ID", 101);
PendingIntent pI = PendingIntent.getActivity(this, 0, intent1, 0);
Notification noti =
new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.logo)
.setContentTitle(gr)
.setContentText(r)
.setDefaults(Notification.DEFAULT_ALL)
.setStyle(bigStyle)
.extend(wearableExtender)
.build();
NotificationManagerCompat nM = NotificationManagerCompat.from(this);
nM.notify(0,noti);
}
Upvotes: 2
Views: 338
Reputation: 19
Try setting your alarmManager to setRepeating(), like so
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
AlarmManager.INTERVAL_HALF_HOUR, pendingIntent);
or
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
(30*60*1000), pendingIntent);
Upvotes: 1