Using Alarm Manager to Start a Service

I have a code that I want to execute at 3 a.m. every day. I read the Service Class Documentation , and it looks like I can use the AlarmManager to trigger the intent (Activity or Service, I think?), And then, in that intent, create and post a message in the Android notification area.

Calendar threeAM = Calendar.getInstance();
threeAM.set(Calendar.HOUR_OF_DAY,2);
threeAM.set(Calendar.MINUTE,0);
threeAM.set(Calendar.SECOND,0);
threeAM.set(Calendar.MILLISECOND,0);
AlarmManager alarmManager =
         (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(context, myNotifier.class);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, threeAM.getTimeInMillis(),
        AlarmManager.INTERVAL_DAY, PendingIntent.getService(context, 1, i , 0));
Log.i("Service TEST", "Alarm set?" );

It runs the code without problems, but there is no indication that the alarm is set, and the action does not start. I use an activity that I know works. I tried wrapping it in try / catch, nothing in logcat ...

Any help would be appreciated! :)

+3
source share
2 answers

, 3 . .

+1

Source: https://habr.com/ru/post/1793295/


All Articles