Scheduled Alarm Manager not working Android

前端 未结 1 2028
抹茶落季
抹茶落季 2021-02-10 00:05

I am trying to start an alarm service that repeats every day at a particular time. I have gone through a lot of threads on stack overflow regarding this but no luck. I followed

相关标签:
1条回答
  • 2021-02-10 00:55

    Here is what I did to get it working:

    1) Added <uses-permission android:name="com.android.alarm.permission.SET_ALARM"/> to my manifest file.

    2) Changed code in my Main activity to:

     AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    
            Calendar calendar = Calendar.getInstance();
            calendar.set(Calendar.HOUR, 3);
            calendar.set(Calendar.MINUTE, 29);
            calendar.set(Calendar.AM_PM, Calendar.PM);
    
             Intent myIntent = new Intent(this, MyReceiver.class);
             pendingIntent = PendingIntent.getBroadcast(this, 0, myIntent,0);
    
             alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
                     AlarmManager.INTERVAL_DAY, pendingIntent);
    

    Hope someone finds this helpful!

    0 讨论(0)
提交回复
热议问题