Android BOOT_COMPLETED广播故障

时间:2015-07-29 14:42:01

标签: android

当我从手机HUAWEI p6测试时,我没有收到BOOT_COMPLETED广播。 广播只在安装后收到一次

这是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:

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);
}
}

和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);
    }

它适用于模拟器。当我发送广播时,它会在手机上工作:

  

./ adb shell

     
    

am broadcast -a android.intent.action.BOOT_COMPLETED

  

我在重启之前打开了几次应用程序。 已安装在设备上。

任何帮助将不胜感激

由于

1 个答案:

答案 0 :(得分:0)

根据documentationWakefulBroadcastReceiver需要WAKE_LOCK权限。对于你正在做的事情,常规的BroadcastReceiver就足够了。像这样:

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());   
        }
    }
}