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); } }; Bellow 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";
//这里我添加了BroadcastReceiver注册和接收器相关的代码。它运行良好,但一旦在 android14 之后更新基于 SDK 34 > 手机出现以下错误 java.lang.SecurityException:当接收器未专门为系统广播注册时,应指定 RECEIVER_EXPORTED 或 RECEIVER_NOT_EXPORTED 之一 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
将广播接收器标记为导出,以便您的应用程序可以收听其他应用程序广播,例如
registerReceiver(syncStatusReceiver, new IntentFilter(Config.getUCHAction(getApplicationContext(), SYNC_NOTIFY)), RECEIVER_EXPORTED);
从 Android 14 开始需要这样做