0

In my app I put 4 checkboxes and a button in a fragment. When I press the button, it sets the alarms according to the selected checkbox

The checkboxes and everything else seems to work, but there are problems with the requestCode and then with the output, here are some examples of operation:

(The outputs are the notifications in order that take place every 5 seconds as per code)

If I check only one of them:

Error, reqCode: 0

If I check the first two:

Error, reqCode: 0

Alarm 1

If I check all:

Error, reqCode: 0

Alarm 1

Alarm 2

Alarm 1


As you can see the first alarm is always with requestCode = 0 but this really does not make sense to me.. I would appreciate if you could help me solve this problem. Thanks have a nice day


Here is the code:

AlarmReceiver.java

public class AlarmReceiver extends WakefulBroadcastReceiver {

private AlarmManager alarmMgr;

private PendingIntent alarmIntent = null, alarmIntent2 = null,
        alarmIntent3 = null, alarmIntent4 = null;

@Override
public void onReceive(Context context, Intent intent) {

    Intent service = new Intent(context, AlarmSchedulingService.class);

    service.putExtra("requestCode", intent.getExtras()
            .getInt("requestCode"));

    startWakefulService(context, service);

}

public void setAlarm(Context context) {

    alarmMgr = (AlarmManager) context
            .getSystemService(Context.ALARM_SERVICE);

    Intent intent = new Intent(context, AlarmReceiver.class);

    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(System.currentTimeMillis());

    if (/* Checkbox1.. */) {
        alarmIntent = PendingIntent.getBroadcast(context, 1,
                intent, 0);
        intent.putExtra("requestCode", 1);
        calendar.add(Calendar.SECOND, 5);

        alarmMgr.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
                alarmIntent);

    }

    if (/* Checkbox2.. */) {
        alarmIntent2 = PendingIntent.getBroadcast(context,
                2, intent, 0);
        intent.putExtra("requestCode", 2);
        calendar.add(Calendar.SECOND, 5);

        alarmMgr.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
                alarmIntent2);
    }
    if (/* Checkbox3.. */) {
        alarmIntent3 = PendingIntent.getBroadcast(context,
                3, intent, 0);
        intent.putExtra("requestCode", 3);
        calendar.add(Calendar.SECOND, 5);

        alarmMgr.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
                alarmIntent3);
    }

    if (/* Checkbox4.. */) {
        alarmIntent4 = PendingIntent.getBroadcast(context,
                4, intent, 0);
        intent.putExtra("requestCode", 4);
        calendar.add(Calendar.SECOND, 5);

        alarmMgr.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
                alarmIntent4);
    }

}

public void cancelAlarm(Context context) {

    if (alarmMgr != null) 
    {
        if (alarmIntent != null) {
            alarmMgr.cancel(alarmIntent);
        }
        if (alarmIntent2 != null) {
            alarmMgr.cancel(alarmIntent2);
        }
        if (alarmIntent3 != null) {
            alarmMgr.cancel(alarmIntent3);
        }
        if (alarmIntent4 != null) {
            alarmMgr.cancel(alarmIntent4);
        }
    }

}

}

AlarmSchedulingService.java

public class AlarmSchedulingService extends IntentService {
public AlarmSchedulingService() {
    super("AlarmSchedulingService");
}

public static final int NOTIFICATION_ID = 1;

private NotificationManager mNotificationManager;
NotificationCompat.Builder builder;

@Override
protected void onHandleIntent(Intent intent) {

    int reqCode = intent.getExtras().getInt("requestCode");

    if(reqCode == 1)
    {
        sendNotification("Alarm 1");
    }

    else if(reqCode == 2)
    {
        sendNotification("Alarm 2");
    }

    else if(reqCode == 3)
    {
        sendNotification("Alarm 3");
    }

    else if(reqCode == 4)
    {
        sendNotification("Alarm 4");
    }
    else {
        sendNotification(" Error, reqCode: " + reqCode);
    }

    AlarmReceiver.completeWakefulIntent(intent);

}

private void sendNotification(String msg) {

mNotificationManager = (NotificationManager)
           this.getSystemService(Context.NOTIFICATION_SERVICE);

    PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
        new Intent(this, MainActivity.class), 0);

    NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(this)
    .setAutoCancel(true)
    .setSmallIcon(R.drawable.ic_launcher)
    .setContentTitle(getString(R.string.app_name))
    .setStyle(new NotificationCompat.BigTextStyle()
    .bigText(msg))
    .setContentText(msg);

    mBuilder.setContentIntent(contentIntent);
    mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}

}
Effe
  • 33
  • 1
  • 7
  • Try this http://stackoverflow.com/questions/4315611/android-get-all-pendingintents-set-with-alarmmanager/9111330?noredirect=1#comment31451160_9111330 – gilsaints88 Mar 07 '14 at 12:05
  • Thanks that worked! Instead of passing the id using putExtra(), I passed the String.valueOf(my_id) as intent with setAction(). After that I checked for that action in my HandleIntent(). Now... should i write how i fixed this with an answer... or editing my own post? This is my second question, could you tell me how to do? – Effe Mar 07 '14 at 15:58
  • @user3151134 You could answer the question with your code. Makes others use your solution. – Anoop Chandrika HarisudhanNair Aug 14 '14 at 12:46

0 Answers0