Skip to content

Commit 735cda7

Browse files
committed
Fix for #56
We are using the RECEIVER_NOT_EXPORTED flag in BroadcastMessageReceiver.Therefore, it is mandatory to set the package name of this app with the custom action in the intent .Not setting the package name will prevent the broadcast receiver from being triggered with custom actions in Android 14 or later. For more information, see issue https://stackoverflow.com/questions/76919130/android-14-context-registered-broadcast-receivers-not-working
1 parent 560abb9 commit 735cda7

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

app/src/main/java/github/umer0586/sensorserver/activities/MainActivity.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,10 @@ class MainActivity : AppCompatActivity(), NavigationBarView.OnItemSelectedListen
224224
ContextCompat.startForegroundService(applicationContext, intent)
225225
}
226226
else if (!isChecked && isServerRunning) {
227-
this.sendBroadcast(Intent(HttpService.ACTION_STOP_SERVER))
227+
val intent = Intent(HttpService.ACTION_STOP_SERVER).apply {
228+
setPackage(applicationContext.packageName)
229+
}
230+
this.sendBroadcast(intent)
228231
}
229232
}
230233
}

app/src/main/java/github/umer0586/sensorserver/fragments/ServerFragment.kt

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,14 @@ class ServerFragment : Fragment(), ServerStateListener
120120
{
121121
Log.d(TAG, "stopServer()")
122122

123-
// getContext().stopService(new Intent(getContext(),SensorService.class));
124-
requireContext().sendBroadcast(Intent(WebsocketService.Companion.ACTION_STOP_SERVER))
123+
// We are using the RECEIVER_NOT_EXPORTED flag in BroadcastMessageReceiver.
124+
// Therefore, it is mandatory to set the package name of this app with the custom action in the intent.
125+
// Not setting the package name will prevent the broadcast receiver from being triggered in Android 14 or later.
126+
// For more information, see https://stackoverflow.com/questions/76919130/android-14-context-registered-broadcast-receivers-not-working
127+
val intent = Intent(WebsocketService.ACTION_STOP_SERVER).apply {
128+
setPackage(requireContext().packageName)
129+
}
130+
requireContext().sendBroadcast(intent)
125131
}
126132

127133
override fun onPause()

0 commit comments

Comments
 (0)