androidbroadcastreceiverandroid-broadcastandroid-14

Android 14 context registered broadcast receivers not working after sdk 34 update


private BroadcastReceiver syncStatusReceiver = new BroadcastReceiver() {

    @Override
    public void onReceive(Context context, Intent intent) {
        /** This method can trigger multiple times. So we are unregistering after first received it. */
        Log.e("sucess","#"+syncStatusReceiver);
        unRegisterReceiver();
        initiateMainActivity(true);
    }
 };
 
 

Below function using

 private void registerReceiver() {
    try {
        registerReceiver(syncStatusReceiver,
                new IntentFilter(Config.getUCHAction(getApplicationContext(), SYNC_NOTIFY)));
    } catch (Exception e) {
        e.printStackTrace();
    }
 }
 
 
    public static String getUCHAction(Context appContext, String actionName) {
     return appContext.getPackageName() + actionName;
 }

public static final String SYNC_NOTIFY = ".sync_notification";

        

Added BroadcastReceiver registered and receiver related code. It's working good but once update SDK 34 based after android14

Error I am getting:

java.lang.SecurityException: One of RECEIVER_EXPORTED or RECEIVER_NOT_EXPORTED should be specified when a receiver isn't being registered exclusively for system broadcasts
    android.os.Parcel.createExceptionOrNull(Parcel.java:3057)
    android.os.Parcel.createException(Parcel.java:3041)
    android.os.Parcel.readException(Parcel.java:3024)
    android.os.Parcel.readException(Parcel.java:2966)
    android.app.IActivityManager$Stub$Proxy.registerReceiverWithFeature(IActivityManager.java:6085)
    android.app.ContextImpl.registerReceiverInternal(ContextImpl.java:1863)
    android.app.ContextImpl.registerReceiver(ContextImpl.java:1803)
    android.app.ContextImpl.registerReceiver(ContextImpl.java:1791)
    android.content.ContextWrapper.registerReceiver(ContextWrapper.java:755)
    android.content.ContextWrapper.registerReceiver(ContextWrapper.java:755)
    

How solve this error in android 14.


Solution

  • Mark the broadcast receiver as exported so your app can listen to other apps broadcasts like

    registerReceiver(syncStatusReceiver,  new IntentFilter(Config.getUCHAction(getApplicationContext(), SYNC_NOTIFY)), RECEIVER_EXPORTED);
    

    this is required starting from Android 14