我对这里的概念感到困惑。我读过许多教程,为了在设备启动时收到通知(例如为了重新安排闹钟),您需要有一个 boot_completed 广播接收器。
然而,令人困惑的部分是,我没有看到像“register(广播接收器)”这样的方法。人们只有广播接收器类,并且在 list 中拥有它。但您不需要某种注册过程吗?系统如何知道有一个 APP x 有一个广播接收器 y 监听启动事件,除非通过注册明确告知
有点像您创建点击监听器,但您需要将其注册/添加到按钮,以便在单击时调用它。
有人可以帮我消除困惑吗?
谢谢
谢谢
最佳答案
People just have the broadcast receiver class and they have it in manifest. but dont you need some sort of registration process?
<receiver>
list 中的元素具有 <intent-filter>
子元素,记录Intent
它希望接收的结构:
<?xml version="1.0" encoding="utf-8"?>
<manifest android:versionCode="1"
android:versionName="1.0"
package="com.commonsware.android.sysevents.boot"
xmlns:android="http://schemas.android.com/apk/res/android">
<uses-sdk android:minSdkVersion="3"
android:targetSdkVersion="6" />
<supports-screens android:largeScreens="false"
android:normalScreens="true"
android:smallScreens="false" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application android:icon="@drawable/cw"
android:label="@string/app_name">
<receiver android:name=".OnBootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
</application>
</manifest>
在这里,我们注册一个BroadcastReceiver
,名为OnBootReceiver
,接收BOOT_COMPLETED
广播。
How will the system know that there is an APP x who has a broadcast receiver y listening for boot events unless explicitly told through registering
Android 是“通过注册明确告知的”。碰巧“注册”是通过 list 完成的,而不是通过应用程序中的 Java 代码完成的。
关于android - 为什么我们不为 BOOT_COMPLETED 事件注册广播接收器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12874763/