我创建了一个方法,通过按下我的主要活动下的保存按钮来调用它,如下所示:
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");
}
然后我添加了一行来显示如下:
<receiver android:process=":remote" android:name=".AlarmReceiver"></receiver>
然后创建了一个接收器类,如下所示:
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();
}
}
}
但是由于某种原因,当我在模拟器上测试它时,我无法接收到任何广播:(
谁能告诉我我做错了什么?
提前谢谢了