【发布时间】:2012-07-18 00:35:37
【问题描述】:
我已经能够让我的 BroadcastReceiver 运行这个:
<receiver
android:name=".AlarmReceiver"
android:enabled="true"
android:exported="false">
</receiver>
如您所见,没有<intent-filter>。使用正确的Intent extras 可以正确调用它。但我一直在环顾四周,我很困惑我是否需要一个?我确实有一个在我的Intents 上调用的setAction() 方法,但要使它们与其他方法不同,以确保通知的特定问题,而不是实际使用该操作的字符串。但究竟是什么相关性?提前致谢。
Intent intent = new Intent(this.getContext(), AlarmReceiver.class);
intent.setAction("com.something"+System.currentTimeMillis());
//... extras are here
PendingIntent pi = PendingIntent.getBroadcast(this.getContext(), 123, intent, PendingIntent.FLAG_CANCEL_CURRENT|Intent.FILL_IN_DATA);
AlarmManager alarm = (AlarmManager)getContext().getSystemService(Activity.ALARM_SERVICE);
alarm.set(AlarmManager.RTC_WAKEUP, time.getTimeInMillis(), pi);
将它与我在清单文件中的内容一起使用。
编辑 我在 Android 开发者博客上找到了这个,它说:
Implicit Intents 仅指定它们应该匹配的“什么”,使用操作、类别、数据、 MIME 类型等等。他们将找到的确切组件仅在 运行时,由包管理器将其与当前应用程序匹配。
Explicit Intents 指定一个明确的“谁”他们应该匹配,通过 组件名称。无论 Intent 中还有什么其他内容,它只与 在其 ComponentName 中给出的确切清单包名称和类名称。
我仍然对这个解释有些困惑,但这似乎是最接近我应该做的事情。所以我确定我使用的是隐含的意图。问题是,我可以忽略<intent-filter> 吗?我将它与特定的课程相匹配。可能没有实际的动作将它们联系在一起,珀斯,但是这个类就足够了吗?
【问题讨论】:
标签: android broadcastreceiver intentfilter