My Source Code
WeatherLockReceiver.kt
@AndroidEntryPoint
class WeatherLockReceiver : BroadcastReceiver() {
@Inject
lateinit var preferenceRepository: PreferenceRepository
override fun onReceive(context: Context, intent: Intent) {
val isEnabled = runBlocking(Dispatchers.IO) {
preferenceRepository.userPreference.first().weatherLockEnabled
}
if (!isEnabled) {
return
}
when (intent.action) {
Intent.ACTION_BOOT_COMPLETED,
Intent.ACTION_SCREEN_OFF -> {
val activityIntent = Intent(context, WeatherLockActivity::class.java)
activityIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
activityIntent.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS)
activityIntent.addFlags(Intent.FLAG_ACTIVITY_NO_USER_ACTION)
context.startActivity(activityIntent)
}
}
}
companion object {
private val intentFilter = IntentFilter().apply {
addAction(Intent.ACTION_SCREEN_OFF)
addAction(Intent.ACTION_BOOT_COMPLETED)
}
fun register(context: Context) {
context.registerReceiver(WeatherLockReceiver(), intentFilter)
}
fun unregister(context: Context) = runCatching {
context.unregisterReceiver(WeatherLockReceiver())
}
}
}
AndroidManifest.xml
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
...
<receiver
android:name="com.skplanet.weatherpong.feature.weatherlock.broadcast.WeatherLockReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.SCREEN_OFF" />
</intent-filter>
</receiver>
How I tried:
- static registration using Manifest
- dynamic registration using Context
In both methods, BOOT_COMPLETE was not received.
Why is it not being received?
Test Device
Samsung Galaxy Z Fold3 (AOS 13) Android Emulator (AOS 12, 13)