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

How to fix the Android error "Background execution not allowed: receiving Intent {...}"

所以我正在编写一个 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 正是我要"logging"的东西。

问题是,每当我 运行 该代码时,我都会收到此错误并且我的代码无法运行。我假设它不起作用的原因是因为这个错误。

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 O 的隐式广播禁令,要解决这个问题,您可以使您的 targetSDKVersion 小于 26 。你可以在这里找到更多 https://commonsware.com/blog/2017/04/11/android-o-implicit-broadcast-ban.html

在我的例子中 - 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);

Implicit Android Oreo 及更高版本不再允许广播。

隐式广播是不专门针对该应用程序的广播。

不只是使用 intent-filter 动作名称来构造 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);
            }