为了捕获BOOT_COMPLETED,仅仅在“AndroidManifest.xml”中注册BroadcastReceiver就足够了吗?

时间:2016-06-17 11:24:30

标签: java android broadcastreceiver android-broadcastreceiver

为了捕获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

但没有任何反应。

(我运行应用程序,而不仅仅是将其推送到设备上)

2 个答案:

答案 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

有关详细信息,请查看IntentIntentFilter上的developer's guide