Blu*_*ite 11 android runtime-error bluetooth broadcastreceiver android-broadcast
所以我正在编写一个使用蓝牙发现设备的 Android 应用程序。这是我用来开始发现的代码。
try {
myBluetoothAdapter.startDiscovery();
Log.d("Bluetooth Started successfully","yes");
} catch (Error e) {
Log.d("FAILED","Ya failed mate");
e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
然后,我注册一个 BroadcastReceiver 以监视何时找到设备。这是我的代码
IntentFilter intentFilter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
final ArrayList<String> stringArrayList = new ArrayList<>();
final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<>(getApplicationContext(),android.R.layout.simple_list_item_1,stringArrayList);
final ListView listView = findViewById(R.id.listView);
listView.setAdapter(arrayAdapter);
BroadcastReceiver myReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
Log.d("ACTION RECEIVED","Action was received");
Log.d("Device Name", String.valueOf(intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE)));
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
stringArrayList.add(device.getName());
arrayAdapter.notifyDataSetChanged();
listView.invalidateViews();
}
}
};
registerReceiver(myReceiver,intentFilter);
Run Code Online (Sandbox Code Playgroud)
listView、arrayAdapter 和 stringArrayList 只是我要“记录”的内容。
问题是,每当我运行该代码时,我都会收到此错误,并且我的代码无法运行。我假设它不起作用的原因是因为这个错误。
W/BroadcastQueue: Background execution not allowed: receiving Intent { act=android.bluetooth.adapter.action.DISCOVERY_STARTED flg=0x10 } to com.verizon.messaging.vzmsgs/com.verizon.vzmsgs.receiver.DevicePairingListener
Run Code Online (Sandbox Code Playgroud)
有人可以告诉我这个错误意味着什么以及如何修复它吗?
我还在 Stack Overflow 上发现了其他问题,其中的错误看起来非常相似;例如,它将处于 BOOT_COMPLETED、ACTION_POWER_DISCONECTED 或 BATTERY_LOW 的上下文中,而不是蓝牙。那些和这个有什么相似之处。
Dan*_*iel 15
就我而言 - Android 9(API 级别 28)我必须定义我的BroadcastReceiver在代码内部定义才能使其正常工作。
像这样(在我的例子中添加到服务中 - 没关系)
private MyReceiver myReceiver;
@Override
public void onCreate() {
myReceiver = new MyReceiver();
this.registerReceiver(myReceiver, new IntentFilter("android.intent.action.myreceiver"));
}
@Override
public void onDestroy() {
super.onDestroy();
unregisterReceiver(myReceiver);
}
private class MyReceiver extends BroadcastReceiver {
@Override
public void onReceive(final Context context, final Intent intent) {
try {
if (intent.getAction().equals("android.intent.action.myreceiver")) {
//logic goes here
}
} catch (Exception e) {
//some log goes here
}
}
}
Run Code Online (Sandbox Code Playgroud)
像这样发送广播
Intent intentMy = new Intent();
intentMy.setAction("android.intent.action.myreceiver");
intentMy.putExtra("whatever", true);
sendBroadcast(intentMy);
Run Code Online (Sandbox Code Playgroud)
k_o*_*_o_ 12
Android Oreo 及更高版本中不再允许隐式广播。
隐式广播是不专门针对该应用程序的广播。
不要只使用意图过滤器操作名称来构造 Intent,而是在包管理器中搜索提供广播接收器的组件:
Intent intent = new Intent("the intent-filter action name in the different app").setFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
PackageManager packageManager = getPackageManager();
List<ResolveInfo> infos = packageManager.queryBroadcastReceivers(intent, 0);
for (ResolveInfo info : infos) {
ComponentName cn = new ComponentName(info.activityInfo.packageName,
info.activityInfo.name);
intent.setComponent(cn);
sendBroadcast(intent);
}
Run Code Online (Sandbox Code Playgroud)
小智 -6
我认为这是因为 Android O 的隐式广播禁令,要解决此问题,您可以使 targetSDKVersion 小于 26 。您可以在这里找到更多信息https://commonsware.com/blog/2017/04/11/android-o-implicit-broadcast-ban.html
| 归档时间: |
|
| 查看次数: |
33996 次 |
| 最近记录: |