BroadcastReceiver设置忽略了一些东西

时间:2013-01-21 02:45:20

标签: android broadcastreceiver

我正在尝试设置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" />

来自发送意图的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的日志输出。

1 个答案:

答案 0 :(得分:1)

检查以确保您的接收器实际位于您的活动的子包中,如果没有,请删除'.receiver'。

<receiver android:name=".receiver.ScheduledEventReceiver" />