0

I know this a basic problem but it is still driving me crazy. I am setting a repeating alarm but the receiver is never called.

Intent intent = new Intent(NewSchedule.this, RepeatingAlarm.class);
PendingIntent sender = PendingIntent.getBroadcast(NewSchedule.this, 0, intent, 0);


Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.SECOND, 10);

AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, calendar.getTimeInMillis(), 5 * 1000, sender);
Log.i("calendar",calendar.getTimeInMillis() + "");
Toast.makeText(NewSchedule.this, "repeating_scheduled", Toast.LENGTH_SHORT).show();


public class RepeatingAlarm extends BroadcastReceiver {

      @Override
      public void onReceive(Context context, Intent intent) {
        Toast.makeText(context, "repeating_received", Toast.LENGTH_LONG).show();



      }
    }

<receiver android:name=".RepeatingAlarm" android:process=":remote" />

I am testing on my phone. The calendar log shows the exact time. I never get the Toast in the receiver class.

erdomester
  • 11,789
  • 32
  • 132
  • 234

2 Answers2

0

Reference : Android Alarm Manager with broadcast receiver

Intent sender = new Intent("WhatEverYouWant");
PendingIntent senderPIntent = PendingIntent.getBroadcast(context, 0, sender, 0);
AlarmManager alarm = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarm.set(AlarmManager.RTC_WAKEUP, triggerTime, senderPIntent);

// In Manifest.xml file
<receiver android:name="com.package.YourOnReceiver">
    <intent-filter>
       <action android:name="WhatEverYouWant" />
    </intent-filter>
</receiver>
Community
  • 1
  • 1
Vishal Vyas
  • 2,571
  • 1
  • 22
  • 39
0

Actually it turned out my code was good. Somehow the alarm was up and running and thus for some unknown reason (at least to me) the recevier could not be called. I figured it out when I created a new project and tested that that receiver was working fine. I also had to stop that alarm. Then I went back to my original project and started the same alarm without changing any lines and it was working fine. Has anyone experienced this?

erdomester
  • 11,789
  • 32
  • 132
  • 234