java - 为了捕获 BOOT_COMPLETED,只需在 `AndroidManifest.xml` 中注册 BroadcastReceiver 就足够了吗?

标签 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

但是什么也没发生。

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

最佳答案

将以下 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

有关更多信息,您应该查看developer's guideIntentIntentFilter 上。

关于java - 为了捕获 BOOT_COMPLETED,只需在 `AndroidManifest.xml` 中注册 BroadcastReceiver 就足够了吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37880369/

相关文章:

java - iTextpdf PdfPTable 如何在网格中的任意位置添加单元格?

java - 边唱歌边连续语音识别?

未使用的 Javac 配置参数包含警告?

android - 为什么 CordovaWebViewClient 不再适用于 Cordova 6

android - 更改BroadcastReceiver中用于通知非流媒体首选项的输出音量

android - 何时广播 ACTION_DEVICE_STORAGE_LOW Intent ?

java - 如何截取 lwuit 应用程序的屏幕截图,但不在模拟器中

android - Eclipse 创建安卓应用程序

android - 防止在显示 fragment 时单击 Activity 中的按钮

java - 我的 BroadcastReceiver 类中的空指针异常