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

11

我正在编写一个使用蓝牙设备发现的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.bluetooth.adapter.action.DISCOVERY_STARTED<intent-filter><receiver> - David Wasser
3个回答

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

像这样发送广播

Intent intentMy = new Intent();
intentMy.setAction("android.intent.action.myreceiver");
intentMy.putExtra("whatever", true);
sendBroadcast(intentMy);

3
确实是这个问题,这个错误信息确实有点奇怪! - crysxd

11

隐式广播在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);
            }

-6

根据这个,似乎没有绕过它的方法。这是真的吗? - BlueLite
当您在代码中注册接收器时,这是可以的。我已经下载了这个代码 https://github.com/googlesamples/android-BluetoothChat ,并且我可以在BroadcastReceiver中获得回调,也许您可以将其与项目进行比较,以找出您项目中的问题。我注意到清单中声明了两个权限:android.permission.BLUETOOTH_ADMIN和android.permission.BLUETOOTH,您可以首先检查这些权限。 - zht2005
15
降低目标并不是解决方案。 - Arnold Balliu
它并不是一个解决方案。 - Trung Đoan
也许这不是一个解决方案,但它确实解释了问题,不像下面的答案。如果您遵循链接,解决方案就在那里。 - StackExploded
这不是一个解决方案,你必须被迫升级你的targetSDKVersion :D - Jorgesys

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接