Skip to content

Commit 3cf94df

Browse files
apuruniLuna Wei
authored andcommitted
For targeting SDK 34 - Added RECEIVER_EXPORTED/RECEIVER_NOT_EXPORTED flag support in DevSupportManagerBase (#38256)
Summary: Pull Request resolved: #38256 Add RECEIVER_EXPORTED/RECEIVER_NOT_EXPORTED flag support to DevSupportManagerBase for Android 14 change. See https://developer.android.com/about/versions/14/behavior-changes-14#runtime-receivers-exported for details. Without this fix, app crashes during launch because of : ```SecurityException: {package name here}: One of RECEIVER_EXPORTED or RECEIVER_NOT_EXPORTED should be specified when a receiver isn't being registered exclusively for system broadcasts``` Changelog: [Targeting SDK 34] Added RECEIVER_EXPORTED/RECEIVER_NOT_EXPORTED flag support in DevSupportManagerBase Reviewed By: javache Differential Revision: D47313501 fbshipit-source-id: 12e8299559d08b4ff87b4bdabb0a29d27763c698
1 parent 938bd78 commit 3cf94df

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/devsupport/DevSupportManagerBase.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import android.graphics.Color;
2121
import android.graphics.Typeface;
2222
import android.hardware.SensorManager;
23+
import android.os.Build;
2324
import android.util.Pair;
2425
import android.view.Gravity;
2526
import android.view.View;
@@ -1142,7 +1143,7 @@ private void reload() {
11421143
if (!mIsReceiverRegistered) {
11431144
IntentFilter filter = new IntentFilter();
11441145
filter.addAction(getReloadAppAction(mApplicationContext));
1145-
mApplicationContext.registerReceiver(mReloadAppBroadcastReceiver, filter);
1146+
compatRegisterReceiver(mApplicationContext, mReloadAppBroadcastReceiver, filter, true);
11461147
mIsReceiverRegistered = true;
11471148
}
11481149

@@ -1258,4 +1259,21 @@ public void setPackagerLocationCustomizer(
12581259

12591260
return mSurfaceDelegateFactory.createSurfaceDelegate(moduleName);
12601261
}
1262+
1263+
/**
1264+
* Starting with Android 14, apps and services that target Android 14 and use context-registered
1265+
* receivers are required to specify a flag to indicate whether or not the receiver should be
1266+
* exported to all other apps on the device: either RECEIVER_EXPORTED or RECEIVER_NOT_EXPORTED
1267+
*
1268+
* <p>https://developer.android.com/about/versions/14/behavior-changes-14#runtime-receivers-exported
1269+
*/
1270+
private void compatRegisterReceiver(
1271+
Context context, BroadcastReceiver receiver, IntentFilter filter, boolean exported) {
1272+
if (Build.VERSION.SDK_INT >= 34 && context.getApplicationInfo().targetSdkVersion >= 34) {
1273+
context.registerReceiver(
1274+
receiver, filter, exported ? Context.RECEIVER_EXPORTED : Context.RECEIVER_NOT_EXPORTED);
1275+
} else {
1276+
context.registerReceiver(receiver, filter);
1277+
}
1278+
}
12611279
}

0 commit comments

Comments
 (0)