问题
My receiver is not getting called when I declare an implicit broadcast receiver in AndroidManifest.
<receiver
android:name=".BluetoothConnectionReceiver_"
android:enabled="true"
android:exported="true"
android:permission="android.permission.BLUETOOTH,
android.permission.BLUETOOTH_ADMIN">
<intent-filter>
<action android:name="android.bluetooth.device.action.ACL_CONNECTED" />
<action android:name="android.bluetooth.device.action.ACL_DISCONNECTED" />
</intent-filter>
</receiver>
I understand that in Oreo, there is a restriction on some implicit broadcasts. But in the doc, https://developer.android.com/guide/components/broadcast-exceptions, ACL_CONNECTED and ACL_DISCONNECTED are not among them.
回答1:
Declare <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
in your AndroidManifest and you must check in runtime to verify if the permission is granted for the versions >= Lollipop.
Use this code:
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, 1);
} else {
//Permission is already granted
}
Using ContextCompat
and ActivityCompat
classes you don't check if the running SDK/OS version is >= Lollipop. In case of the version < Lollipop the method ContextCompat.checkSelfPermission()
will return true.
来源:https://stackoverflow.com/questions/50284008/implicit-broadcast-android-bluetooth-device-action-action-acl-connected-not-work