0

I'm new on android programming and i'm working on android application and i'm stuck in creating multiple alarms. my code only fires the last alarm and ignore the previous ones the dates are stored in a database (SQlite) that the user specifies

this is my reminder manager class

public class ReminderManager {

    private Context mContext; 
    private AlarmManager mAlarmManager;

    public Appointments_ReminderManager(Context context) {
        mContext = context; 
        mAlarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
    }

    public void setReminder(Long reminderId, Calendar when) {

        Intent i = new Intent(mContext, Appointments_OnAlarmReceiver.class);
        i.putExtra(RemindersDbAdapter.KEY_ROWID_APPOIN, (long)reminderId); 

        PendingIntent pi = PendingIntent.getBroadcast(mContext, 0, i, PendingIntent.FLAG_ONE_SHOT); 

        mAlarmManager.set(AlarmManager.RTC_WAKEUP, when.getTimeInMillis(), pi);
    }
}

and this is the receiver class

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ComponentInfo;
import android.util.Log;

public class OnAlarmReceiver extends BroadcastReceiver {

    private static final String TAG = ComponentInfo.class.getCanonicalName(); 


    @Override   
    public void onReceive(Context context, Intent intent) {
        Log.d(TAG, "Received wake up from alarm manager.");

        long rowid = intent.getExtras().getLong(RemindersDbAdapter.KEY_ROWID_APPOIN);

        WakeReminderIntentService.acquireStaticLock(context);

        Intent i = new Intent(context, ReminderService.class); 
        i.putExtra(RemindersDbAdapter.KEY_ROWID_APPOIN, rowid);  
        context.startService(i);

    }
}

can someone please help me modifying my code to fire multiple alarms specified by the user that is stored in a database.

一二三
  • 21,059
  • 11
  • 65
  • 74
zoza
  • 127
  • 3
  • 12

1 Answers1

5

Luckily the answer is pretty simple. Your code is:

PendingIntent pi = PendingIntent.getBroadcast(mContext, 0, i, PendingIntent.FLAG_ONE_SHOT);

The second parameter is the request_code is like an id for the PendingIntent and since you're always override the previous PendingIntent. So what you have to do is to set a unique request code for every alarm you need. In your case you can use the reminderId, which is, I guess the DB row id. And because this is a unique ID for every Alarm it perfectly fits.

PendingIntent pi = PendingIntent.getBroadcast(mContext, reminderId, i, PendingIntent.FLAG_ONE_SHOT);
Dominic
  • 3,353
  • 36
  • 47
  • thanks for answering my question. but i tried what you suggested and it gives me an error because it only takes int type and my reminderIDs of type long – zoza Nov 30 '11 at 07:32
  • and i tried to change its type to int but it gives me errors everywhere in my project. – zoza Nov 30 '11 at 07:36