Reputation: 1634
I have created a method which it is called by pressing a save button under my main activity as follows:
public void scheduleAlarms(Context ctxt) {
String [] calvalue = (MessageDelay.getSelectedItem().toString()).split(" ", 3);
int H = Integer.parseInt(calvalue[1]);
Log.v("Timer","Timer"+H);
switch (H){
case 9:
H=9;
break;
case 12:
H=12;
break;
case 3:
H=15;
break;
case 5:
H=17;
break;
case 6:
H=18;
break;
case 8:
H=20;
break;
}
Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, H);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
AlarmManager mgr=(AlarmManager)getSystemService(Context.ALARM_SERVICE);
Intent i=new Intent(getApplicationContext(), AlarmReceiver.class);
PendingIntent pi=PendingIntent.getService(getApplicationContext(), 0, i, 0);
mgr.setRepeating(AlarmManager.RTC,cal.getTimeInMillis(),AlarmManager.INTERVAL_DAY, pi);
Log.v("Timer","Timer");
}
And then I have added a line to manifest as follows :
<receiver android:process=":remote" android:name=".AlarmReceiver"></receiver>
And then created a receiver class as follows:
public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
try {
Toast.makeText(context, "broadcast received", Toast.LENGTH_SHORT).show();
// Set up new intent
Intent newIntent = new Intent(context, SendMessage.class);
// Start new intent
context.startService(newIntent);
} catch (Exception e) {
Toast.makeText(context, "There was an error somewhere, but we still received an alarm", Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
}
}
But for some reason when I test it on emulator I cannot receive any broadcast :(
could anyone please advice me what I am doing wrong?
Many thanks in advance
Upvotes: 1
Views: 827
Reputation: 52956
You are using a PendingIntent
for a service, but expect to receive a broadcast. Try using PendingIntent.getBroadcast()
.
Upvotes: 2