Android BroadcastReceiver基础知识
示例
BroadcastReceivers用于接收由AndroidOS,其他应用程序或同一应用程序内发送的广播Intent。
每个Intent都使用Intent过滤器创建,该过滤器需要String动作。可以在Intent中配置其他信息。
同样,BroadcastReceivers注册以使用特定的Intent过滤器接收Intent。它们可以通过编程方式注册:
mContext.registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
//您的实现在这里进行。
}
}, new IntentFilter("Some Action"));或在AndroidManifest.xml文件中:
<receiver android:name=".MyBroadcastReceiver">
<intent-filter>
<action android:name="Some Action"/>
</intent-filter>
</receiver>要接收该Intent,请使用以下命令将Action设置为AndroidOS,另一个应用程序或API或您自己的应用程序中记录的内容sendBroadcast:
mContext.sendBroadcast(new Intent("Some Action"));此外,该Intent可以包含可在中查看的信息,例如字符串,基元和ParcelablesonReceive。
热门推荐
10 儿子立冬祝福语简短独特
11 对当兵的祝福语简短
12 侄儿高考试祝福语简短
13 伴郎红包祝福语朋友简短
14 媳妇生日简短祝福语朋友
15 公司年会祝福语简短最好
16 元旦感恩祝福语简短大全
17 红包祝福语简短10字
18 周六早晨祝福语简短