Reputation: 36
Trying to send broadcast and receive it at a specific time. RegisterBroadcast method of Class A in which I registered broadcast:
cal = Calendar.getInstance();
int lap = 1000 * 60 * 10;
cal.setTimeInMillis(System.currentTimeMillis());
cal.set(Calendar.HOUR, 5);
cal.set(Calendar.MINUTE, 01);
AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
am.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), lap, pi);
Intent i = new Intent(getApplicationContext(), MyAlarm.class);
pi = PendingIntent.getBroadcast(getApplicationContext(), 0, i, 0);
Receiver:
@Override
public void onReceive(Context cont, Intent i) {
Toast.makeText(cont, "I am broadcasting", Toast.LENGTH_LONG).show();
}
Manifest:
<receiver android:name="com.anarkali.MyAlarm">
But this code is not able to broadcast properly. Sometimes it do sometimes it does not why? I am confused.
Upvotes: 0
Views: 1405
Reputation: 8774
You seem to be using the PendingIntent
pi
before initializing it, in which case it would use whatever value is assigned to pi
at that time, which could be an old PendingIntent
or possibly null
:
am.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(),lap,pi);
Intent i = new Intent(getApplicationContext(),MyAlarm.class);
pi=PendingIntent.getBroadcast(getApplicationContext(), 0,i,0);
should probably be:
Intent i = new Intent(getApplicationContext(),MyAlarm.class);
pi=PendingIntent.getBroadcast(getApplicationContext(), 0,i,0);
am.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(),lap,pi);
Upvotes: 2