我正在尝试设置一个 BroadcastReceiver 来处理预定的事件。我一直在关注http://mobile.tutsplus.com/tutorials/android/android-fundamentals-scheduling-recurring-tasks/,并且该功能正常工作,或者我相信。有人能帮我找出为什么它现在不起作用吗?
这是接收器:
public class ScheduledEventReceiver extends BroadcastReceiver {
private static final String DEBUG_TAG = "ScheduledEventReceiver";
@Override
public void onReceive(Context context, Intent intent) {
Log.d(DEBUG_TAG, "ScheduledEventReceiver " + intent.getData());
Intent activity = new Intent(context, Model.class);
activity.setData(intent.getData());
context.startService(activity);
}
}
还有一点清单:
<receiver android:name=".receiver.ScheduledEventReceiver" />
从发送 Intent 的 DialogFragment 中:
private void setEventTest(Calendar time) {
setEvent(getActivity(), Uri.parse("http://teststring"), time);
}
private void setEvent(Context context, Uri data, Calendar when) {
Intent intent = new Intent(context, ScheduledEventReceiver.class);
intent.setData(data);
PendingIntent scheduledEvent = PendingIntent
.getBroadcast(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager schedule = (AlarmManager) getActivity().getSystemService(Context.ALARM_SERVICE);
schedule.set(AlarmManager.RTC_WAKEUP, when.getTimeInMillis(), scheduledEvent);
}
我认为它不起作用的原因是我从来没有看到 onReceive 的日志输出。