为了捕获android.intent.action.BOOT_COMPLETED
个事件,仅仅在BroadcastReceiver
中注册AndroidManifest.xml
是否足够?像这样:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.myDomain.myApp"
android:installLocation="internalOnly">
<!-- ... stuff ... -->
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<!-- ... stuff ... -->
<application ...>
<!-- ... stuff ... -->
<receiver android:name=".bgServices.MyBootBroadcastReceiver">
</receiver>
<!-- ... stuff ... -->
</application>
</manifest>
BroadcastReceiver
:
package com.myDomain.myApp.bgServices;
// imports ...
public class MyBootBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.d("my-log", "MyBootBroadcastReceiver.onReceive()");
Toast.makeText(context, "YEAY!", Toast.LENGTH_LONG).show();
}
}
我问,因为我正在发送:
adb shell am broadcast -a android.intent.action.BOOT_COMPLETED -n com.myDomain.myApp/.bgServices.MyBootBroadcastReceiver
但没有任何反应。
(我运行应用程序,而不仅仅是将其推送到设备上)
答案 0 :(得分:1)
不,那不是。您没有指定将接收ACTION android.intent.action.BOOT_COMPLETED
的组件。所以你需要在你的接收器中添加intent-filter
你应该像下面这样做。
<receiver android:name=".bgServices.MyBootBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
答案 1 :(得分:1)
将以下IntentFilter
添加到您的BroadcastReceiver
:
<receiver android:name=".bgServices.MyBootBroadcastReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
</intent-filter>
</receiver>
虽然android.intent.action.BOOT_COMPLETED
应该足够了,但似乎某些设备也需要android.intent.action.QUICKBOOT_POWERON
。
有关详细信息,请查看Intent
和IntentFilter
上的developer's guide。