user2531590
user2531590

Reputation:

Alarm Manager fail to setRepeating

I am having some problem with Alarm Manager in Android.

Inside the onCreate method:

        notificationCount = notificationCount + 1;
        AlarmManager mgr = (AlarmManager) context
                .getSystemService(Context.ALARM_SERVICE);
        Intent notificationIntent = new Intent(context,
                ReminderAlarm.class);
notificationIntent.putExtra("NotifyCount", notificationCount);
        PendingIntent pi = PendingIntent.getBroadcast(context,
                notificationCount, notificationIntent,
                PendingIntent.FLAG_UPDATE_CURRENT);
        mgr.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
                60000+System.currentTimeMillis(), pi);

        ComponentName receiver = new ComponentName(context, BootReceiver.class);
        PackageManager pm = context.getPackageManager();

        pm.setComponentEnabledSetting(receiver,
                PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
                PackageManager.DONT_KILL_APP);

Reminder Alarm class:

public class ReminderAlarm extends BroadcastReceiver {
private NotificationManager mNotificationManager;
private Notification notification;

@Override
public void onReceive(Context context, Intent intent) {
    mNotificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);
    PendingIntent contentIntent = PendingIntent
            .getActivity(context, 0, new Intent(), 0);
    notification = new Notification(
            R.drawable.ic_launcher, "Notification",
            System.currentTimeMillis());
    notification
            .setLatestEventInfo(context, "AlarmActivated", "Hello", contentIntent);
    mNotificationManager.notify(
            Integer.parseInt(intent.getExtras()
                    .get("NotifyCount").toString()),
            notification); }}

In the boot receiver class:

 public void onReceive(Context context, Intent i) {
    scheduleAlarms(context);
}

static void scheduleAlarms(Context context) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(System.currentTimeMillis());
    calendar.set(Calendar.HOUR_OF_DAY, 0);
    calendar.set(Calendar.MINUTE, 1);
    AlarmManager mgr = (AlarmManager) context
            .getSystemService(Context.ALARM_SERVICE);
    Intent notificationIntent = new Intent(context, ReminderAlarm.class);
    PendingIntent pi = PendingIntent.getService(context, 0,
            notificationIntent, 0);

    mgr.setInexactRepeating(AlarmManager.RTC_WAKEUP,
            calendar.getTimeInMillis(),
            60000+System.currentTimeMillis(),
                pi);
}

And in my manifest file:

 <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
 <receiver android:name=".ReminderAlarm" >
    </receiver>
    <receiver
        android:name=".BootReceiver"
        android:enabled="false" >
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" >
            </action>
        </intent-filter>
    </receiver>

I am trying to set the alarm to repeat every minute and prompt user notification. But it works in this way: when I launch the apps, it sets the alarm for once. After it prompt notification for once, then it stopped the repeating for every minute.

I wonder which part of my code went wrong. Thanks in advance.

EDIT

So basically I want the alarm manager to repeat everyday at 12am. Here is the codes:

        Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(System.currentTimeMillis());
    calendar.set(Calendar.HOUR_OF_DAY, 0 );
    calendar.set(Calendar.MINUTE, 1);
        notificationCount = notificationCount + 1;
        AlarmManager mgr = (AlarmManager) context
                .getSystemService(Context.ALARM_SERVICE);
        Intent notificationIntent = new Intent(context,
                ReminderAlarm.class);
    PendingIntent pi = PendingIntent.getBroadcast(context,
                notificationCount, notificationIntent,
                PendingIntent.FLAG_UPDATE_CURRENT);
        mgr.setRepeating(AlarmManager.RTC_WAKEUP,
                calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pi);

        ComponentName receiver = new ComponentName(context, BootReceiver.class);
        PackageManager pm = context.getPackageManager();

        pm.setComponentEnabledSetting(receiver,
                PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
                PackageManager.DONT_KILL_APP);

So by setting the repeating as such, will it still run after 44 years as previous one? or it will repeat everyday at 12am?

Upvotes: 2

Views: 695

Answers (1)

Mike M.
Mike M.

Reputation: 39191

The third parameter in the setInexactRepeating() method is the interval in milliseconds. You're setting that as the number of milliseconds that have elapsed since January 1, 1970 00:00:00.0 UTC plus 60000, making the next scheduled event about 44 years in the future. Change your code as follows:

mgr.setInexactRepeating(AlarmManager.RTC_WAKEUP, 
    calendar.getTimeInMillis(), 60000, pi);

Upvotes: 1

Related Questions