— Android 14, Broadcast Receiver, Security — 1 min read
Android 14 introduces stricter security measures for broadcast receivers. You might encounter the error "One of RECEIVER_EXPORTED or RECEIVER_NOT_EXPORTED should be specified when a receiver isn't being registered exclusively for system broadcasts" if your app registers a broadcast receiver without specifying its access.
Here's how to fix it:
Understanding the Issue:
Fixing the Error:
Update your targetSdkVersion:
targetSdkVersion
in the build.gradle
file to at least 34 (Android 14). This ensures your app adheres to the latest security standards(but this is usually what causes this error in the first place).Specify Receiver Access with ContextCompat.registerReceiver:
ContextCompat.registerReceiver
is a convenient way to register broadcast receivers within your code. It provides an additional parameter for specifying the receiver flags:1MyBroadcastReceiver receiver = new MyBroadcastReceiver();2IntentFilter filter = new IntentFilter("com.example.custom.broadcast");3ContextCompat.registerReceiver(context, receiver, filter, ContextCompat.RECEIVER_NOT_EXPORTED);
In this example, ContextCompat.RECEIVER_NOT_EXPORTED
clarifies that the receiver is only intended for internal use within your app.
Using ContextCompat.registerReceiver offers these benefits:
An Additional Tip
By following these steps, you should be able to resolve the "RECEIVER_EXPORTED or RECEIVER_NOT_EXPORTED" error and ensure your app functions smoothly on Android 14 and beyond.