gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 03:32:36 25 4
gpt4 key购买 nike

为了抓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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com