简体   繁体   English

Android BOOT_COMPLETED广播故障

[英]Android BOOT_COMPLETED Broadcast malfunction

I dont receive the BOOT_COMPLETED broadcast when I test from my phone HUAWEI p6. 通过手机HUAWEI p6进行测试时,我没有收到BOOT_COMPLETED广播。 The Broadcast is received only once after install 安装后仅接收一次广播

Here is AndroidManifest 这是AndroidManifest

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>
....
<receiver android:name=".receiver.WakefulBootReceiver"
        android:enabled="true"
        android:exported="false">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED"/>
            <action android:name="android.intent.action.QUICKBOOT_POWERON"/>
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </receiver>

    <receiver android:name=".receiver.AlarmNotificationReceiver">
    </receiver>

    <receiver android:name=".receiver.MyWakefulReceiver">
    </receiver>

BootReceiver: BootReceiver:

public class WakefulBootReceiver extends WakefulBroadcastReceiver {
public static String CALLER_ID = "WakefulBootReceiver";

@Override
public void onReceive(Context context, Intent intent) {
    Alog.debug("WakefulBootReceiver Before IntentService");
    Intent service = new Intent(context, AlarmSetService.class);
    service.putExtra("SERVICE_CALLER", CALLER_ID);
    startWakefulService(context, service);
}
}

And IntentService: 和IntentService:

@Override
protected void onHandleIntent(Intent intent) {
    Alog.debug("AlarmSetService/onHandleIntent");
    Bundle extras = intent.getExtras();
    String caller = "";
    if (extras != null) {
        caller = extras.getString("SERVICE_CALLER");
    }

    checkForTodayAlarms();

    if (caller.equals("MyWakefulReceiver")) {
        Alog.debug("AlarmSetService/caller MyWakefulReceiver");
        MyWakefulReceiver.completeWakefulIntent(intent);
    } else {
        Alog.debug("AlarmSetService/caller WakefulBootReceiver");
        WakefulBootReceiver.completeWakefulIntent(intent);
    }

It works on emulator. 它可以在模拟器上运行。 And works on phone when I send the broadcast with: 当我通过以下方式发送广播时,可以在电话上使用:

./adb shell ./adb外壳

am broadcast -a android.intent.action.BOOT_COMPLETED 广播-android.intent.action.BOOT_COMPLETED

I opened the app several times before reboot. 在重启之前,我多次打开该应用程序。 Is installed on device. 已安装在设备上。

Any help would be appreciated 任何帮助,将不胜感激

Thanks 谢谢

According to the documentation , a WakefulBroadcastReceiver requires the WAKE_LOCK permission. 根据文档WakefulBroadcastReceiver需要WAKE_LOCK权限。 For what you are doing, a regular BroadcastReceiver should be sufficient. 对于您正在做的事情,常规的BroadcastReceiver应该足够了。 Something like this: 像这样:

public class WakefulBootReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        // Make sure we are getting the right intent
        if( "android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {
            // Your code here
        } else {
            Log.e(TAG, "Received unexpected intent " + intent.toString());   
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM