android - 如何修复 Android 错误 "Background execution not allowed: receiving Intent {...}"

标签 android bluetooth broadcastreceiver runtime-error android-broadcast

所以我正在编写一个使用蓝牙发现设备的 Android 应用程序。这是我用来开始发现的代码。

try {
    myBluetoothAdapter.startDiscovery();
    Log.d("Bluetooth Started successfully","yes");
} catch (Error e) {
    Log.d("FAILED","Ya failed mate");
    e.printStackTrace();
}

然后我注册一个 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);

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

有人能告诉我这个错误是什么意思以及如何解决吗?

我还在 Stack Overflow 上找到了其他问题,它们的错误看起来非常相似;例如,它将在 BOOT_COMPLETED、ACTION_POWER_DISCONECTED 或 BATTERY_LOW 的上下文中,而不是蓝牙。怎么和这个相似。

最佳答案

在我的情况下 - 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
        }
    }
}

像这样发送广播
Intent intentMy = new Intent();
intentMy.setAction("android.intent.action.myreceiver");
intentMy.putExtra("whatever", true);
sendBroadcast(intentMy);

关于android - 如何修复 Android 错误 "Background execution not allowed: receiving Intent {...}",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55565466/

相关文章:

java - 输入字符串未知时的字符串分割(Java Android 短信分割转发)

Android NDK eabi - 如何让 ndk 知道使用 hard fp?

android - 如何检测断开连接的蓝牙音频设备是当前播放音乐流的设备?

python - pyinstaller 给出 "ImportError: DLL load failed"

iOS 音频流仅适用于**某些**蓝牙设备?

android - Android 中自动检测短信

android - 尝试使用没有过滤器的广播接收器

Android Intent 数据 Uri 查询参数

android - 从 Assets 中解析 RecyclerView 中的 JSONArray

android - 滑动抽屉看起来像半圆形