Cristof
Cristof

Reputation: 33

AlarmManager doesn't fire alarm at the corresponding time

Can you plese help me understand why my alarm won't fire :
I am trying to register a pending intent in oder to fire the alarm at 12:22 (current hour now) but the problem is that the alarm won't fire . I tried substituting the miliseconds from calendar.getTimeInMilies() with a hard-encoded time : 10 000 milies (10 seconds); Not even that it had worked .

    AlarmManager alarm_manager =(AlarmManager)this.getSystemService(Context.ALARM_SERVICE);
    calendar = Calendar.getInstance();

    Intent intent = new Intent(MainActivity.this , AlarmManagerHelper.class);

    PendingIntent register =PendingIntent.getActivity(getBaseContext(), 0, intent,0);

    calendar.set(Calendar.YEAR,2014);
    calendar.set(Calendar.MONTH, 10);
    calendar.set(Calendar.DAY_OF_MONTH, 2);
    calendar.set(Calendar.HOUR_OF_DAY,12);
    calendar.set(Calendar.MINUTE,22);
    calendar.set(Calendar.SECOND, 0);
    calendar.set(Calendar.AM_PM, Calendar.AM);




    alarm_manager.set(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(), register);

    SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss");
    Log.d(TAG ,"Time format is :" +formatter.format(calendar.getTime()));

I also have the following permission set in the Manifest

    android:name="android.permission.WAKE_LOCK"

And the output :

    Time format is :02/11/2014 12:22:00

Upvotes: 0

Views: 554

Answers (1)

sam
sam

Reputation: 2820

Try to use getBroadcast method instead of getActivity:

PendingIntent register =PendingIntent.getBroadcast(MainActivity.this, 0, intent,0);

Hope this helps.

Upvotes: 1

Related Questions