How to fix the Android error “Background execution not allowed: receiving Intent {…}”

笑着哭i 提交于 2020-01-22 02:52:05

问题


So I'm programming an Android app that uses Bluetooth discovery of devices. Here is the code I use to start discovery.

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

I then register a BroadcastReceiver to watch for when devices are found. Here is my code for that

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);

The listView, arrayAdapter, and stringArrayList are just things I'm "logging" to.

The problem is that whenever I run that code I get this error and my code doesn't function. I'm assuming that the reason it doesn't function is because of this error.

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

Can someone tell me what this error means as well as how to fix it?

I also find other questions on Stack Overflow with errors that look very similar; like, instead of Bluetooth, it will be in the context of BOOT_COMPLETED, ACTION_POWER_DISCONECTED, or BATTERY_LOW. How are those similar to this.


回答1:


I think it's because of Implicit Broadcast Ban of Android O , to solve this you can make your targetSDKVersion be less than 26 . You can find more here https://commonsware.com/blog/2017/04/11/android-o-implicit-broadcast-ban.html




回答2:


In my case - Android 9 (API level 28) I had to define my BroadcastReceiver inside the code only to make it work.

Like this (added inside a service in my case - its not matter)

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

Send the broadcast like this

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


来源:https://stackoverflow.com/questions/55565466/how-to-fix-the-android-error-background-execution-not-allowed-receiving-intent

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!