0

I am trying to implement repeating multiple alarms at different specific times. The problem is the Alarm Receiver is not getting invoked at any given time.

My code in the activity:

private void setAlarms() {
    Intent myIntent=new Intent(this, AlarmReceiver.class);
    myIntent.putExtra("MedName",medication_name);
    myIntent.setAction("b5.project.medibro.receivers.AlarmReceiver");
    AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    //am.set(AlarmManager.RTC_WAKEUP, time, pendingIntent);
    ArrayList<PendingIntent> intentArray = new ArrayList<PendingIntent>();
    int counter=0;
    for(String timer: timers){
       //timers is an array of Strings in the format hh:mm
         try {
            String[] comps=timer.split(":");
            Calendar cal=Calendar.getInstance();
            cal.set(Calendar.HOUR_OF_DAY, Integer.valueOf(comps[0]));
            cal.set(Calendar.MINUTE, Integer.valueOf(comps[1]));
            cal.set(Calendar.SECOND, 0);
            cal.set(Calendar.MILLISECOND, 0);
            Log.d(TAG,comps[0]+" "+comps[1]+ "Alarm Time: " + cal.getTime().toString());
            PendingIntent pendingIntent = PendingIntent.getService(this, counter, myIntent,0);
            am.setInexactRepeating(AlarmManager.RTC_WAKEUP,
                    cal.getTimeInMillis(),
                    AlarmManager.INTERVAL_DAY,
                    pendingIntent);
            counter++;
        } catch (ParseException e) {
            e.printStackTrace();
            Log.d(TAG,"Time Parsing error: "+e.getMessage());
        }
    }
}

and this is my Alarm Receiver:

public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
    if(intent.getAction().equals("b5.project.medibro.receivers.AlarmReceiver")) {
        Toast.makeText(context, "I'm running", Toast.LENGTH_SHORT).show();
        Log.d("Alarm Receiver", "Alarm is invoked. ");

        NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
        builder.setSmallIcon(R.drawable.messenger_bubble_large_blue);
        builder.setContentTitle("Time to take your medicine");
        String medName = intent.getStringExtra("MedName");
        String text = "Medicine name: " + medName;
        builder.setContentText(text);

        Notification notification = builder.build();
        NotificationManagerCompat.from(context).notify(0, notification);
        Toast.makeText(context, "I'm running", Toast.LENGTH_SHORT).show();
    }
}

}

Manifest declaration:(its within application tags)

<receiver android:name="b5.project.medibro.receivers.AlarmReceiver"
        android:enabled="true">
        <intent-filter>
            <action android:name="b5.project.medibro.receivers.AlarmReceiver"/>
        </intent-filter>
    </receiver>

I feel the above code should execute the broadcast receiver atleast once but nothing shows up in the logcat. Can someone tell me what am I missing?

Abhijith Gururaj
  • 437
  • 4
  • 14
  • What's the output of the Log for Alarm Time? – Gavriel Jan 31 '16 at 20:55
  • @Gavriel `02-01 02:27:41.855: D/Add Prescription(29441): No. of Alarms: 2 02-01 02:27:41.881: D/Database Handler(29441): Inserting: Testing22:29$$$2:301/2/2016CONTINUOUSTesting 02-01 02:27:41.890: D/Add Prescription(29441): 2 29Alarm Time: Mon Feb 01 02:29:00 GMT+05:30 2016 02-01 02:27:41.906: D/Add Prescription(29441): 2 30Alarm Time: Mon Feb 01 02:30:00 GMT+05:30 2016`. Nothing shows up after the Alarm Time' logs. – Abhijith Gururaj Jan 31 '16 at 20:59

1 Answers1

0

Instead of getService try getBroadcast:

PendingIntent pendingIntent = PendingIntent.getService(this, counter, myIntent,0);

PendingIntent pendingIntent = PendingIntent.getBroadcast(this, counter, myIntent,0);

Also look at http://developer.android.com/reference/android/app/PendingIntent.html#getBroadcast(android.content.Context, int, android.content.Intent, int), and figure out the flags you need, 'cause I wouldn't be surprised if only the last alarm (in the for loop) will be working.

Gavriel
  • 18,880
  • 12
  • 68
  • 105
  • Thank you! flag_update_current did the work. Can you give me a hint on how to cancel these alarms at a later point of time?(in another activity. I'm facing some difficulty here too) – Abhijith Gururaj Jan 31 '16 at 21:26
  • Try this: http://stackoverflow.com/questions/14485368/delete-alarm-from-alarmmanager-using-cancel-android, you will identify them by their id (counter in your case) – Gavriel Jan 31 '16 at 22:34
  • I've seen that. My problem is there are multiple medicines. And the counter will reset for each medicine. So I can't access a particular pendingIntent when there are two PI's with same counter value. – Abhijith Gururaj Feb 01 '16 at 05:03
  • It sounds like an algorithmical problem. Open a new question with it – Gavriel Feb 01 '16 at 06:23