Boot_complete 广播不工作

Boot_complete broadcast not working

我的应用程序中有一个 boot_completed 广播,但它不起作用。该应用程序未安装在 SD 卡上。

Android manfest.xml

<receiver android:name="BootReceiver"
        android:enabled="true"
     android:exported="true">
     <intent-filter>
         <action android:name="android.intent.action.BOOT_COMPLETED" />

        </intent-filter> 
    </receiver>

Bootreceiver.java

package nl.bicknos.TWPD;

import android.content.BroadcastReceiver; import android.content.Context;
import android.content.Intent;
 public class BootReceiver extends BroadcastReceiver{
    @Override   public void onReceive(Context context, Intent intent) {
        Toast.makeText(this, "Gestart", Toast.LENGTH_SHORT).show();} }

我已经搜索过,但我找到的解决方案没有用

首先检查您是否已经在 Manifest.xml

中实施
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

你的intent-filter应该是

 <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
 </intent-filter>

在您的广播中,您必须添加

 @Override
public void onReceive(Context context, Intent intent) {
   Toast.makeText(context, "Gestart", Toast.LENGTH_SHORT).show();
}

注意:我猜你的错误是在没有 context.

的情况下试图通过 this

编辑

你也可以尝试做一个 Log.d 而不是 Toast 放 :

Log.d("Restarted", "I'm on BR");

如果您的 BroadcastReceiver 没有呼叫,请尝试将您的 manifest 接收器更换为:

<receiver android:name="nl.bicknos.TWPD.BootReceiver"
    android:enabled="true"
    android:exported="true">
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" /> 
    </intent-filter> 
</receiver>

很明显,但可能存在错误...您的 <uses-permission> 需要是元素的子元素...

让我知道它是否有效:)

你的错误在

Toast.makeText(this, "Gestart", Toast.LENGTH_SHORT).show(); this这里不属于任何UIActivity。您的接收器实际上正在被调用。但是由于您 "Toast" 不在 UI 上下文中,因此您看不到 Toast 消息。如果您尝试在此 onReceive() 中添加日志,您将在 "logcat".

中看到它

希望对您有所帮助:)

我发现了问题,不是代码问题,而是我的 phone。在我的设备启动时,没有一个应用程序启动。由于我是 运行 测试版,我降级了,现在它正在运行。