开发者

Alarm Manager doesn't go off

开发者 https://www.devze.com 2023-04-10 04:09 出处:网络
I\'m building an Alarm Application. for it, I\'m need to supply a time as input. I\'m trying following code but alarm doesnt\' trigger. I searched online and modified the code much alot but still no l

I'm building an Alarm Application. for it, I'm need to supply a time as input. I'm trying following code but alarm doesnt' trigger. I searched online and modified the code much alot but still no luck.

Can someone help me out. Thanks in advance.

Intent intent = new Intent(NapAppActivity.this, OnetimeAlarmReceiver.class);
 PendingIntent pendingIntent = PendingIntent.getBroadcast(NapAppActivity.this, 1,  intent, 0);

     Calendar cur_cal = new GregorianCalendar();
    cur_cal.setTimeInMillis(System.currentTimeMillis());

    Calendar cal = new GregorianCalendar();
    cal.add(Calendar.DAY_OF_YEAR, cur_cal.get(Calendar.DAY_OF_YEAR));
    cal.set(Calendar.HOUR_OF_DAY, 12);
    cal.set(Calendar.MINUTE, 17);
    cal.set(Calendar.SECOND, cur_cal.get(Calendar.SECOND));
    cal.set(Calendar.MILLISECOND, cur_cal.get(Calendar.MILLISECOND));
    cal.set(Calendar.DATE, cur_cal.get(Calendar.DATE));
    cal.set(Calendar.MONTH, cur_cal.get(Calendar.MONTH));

    AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
    alarmManager.set(AlarmManager.RTC_WAKEUP, cal.ge开发者_如何学GotTimeInMillis(), pendingIntent);


I think that you should use cal=Calendar.getInstance() to get the current date/time instead of using GregorianCalendar constructor.

Then, just set HourOfDay and Minutes, do not touch the other fields.


<receiver android:name=".OnetimeAlarmReceiver" android:process=":remote" />

have you declared receiver in your manifest.

Also preceed Receiver name with full package declaration at place of . If your Receiver is in another package I have used "." in above declaration ...

With Regards,

Arpit

0

精彩评论

暂无评论...
验证码 换一张
取 消