I've encountered an issue with my BroadcastReceiver for the BOOT_COMPLETED action. Despite declaring the necessary permissions and configuring the BroadcastReceiver (AlarmBroadcastReceiver) in the AndroidManifest.xml, it only seems to work when I manually start the app at least once after a reboot.
Here's a snippet from my AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:dist="http://schemas.android.com/apk/distribution"
xmlns:tools="http://schemas.android.com/tools">
<uses-sdk />
<dist:module dist:instant="true" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM" />
<uses-permission android:name="android.permission.ACTIVITY_RECOGNITION" />
<uses-permission android:name="com.android.alarm.permission.SET_ALARM" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.DISABLE_KEYGUARD" />
<uses-permission android:name="android.permission.BODY_SENSORS" />
<uses-permission android:name="android.permission.ACCESS_NOTIFICATION_POLICY" />
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.USE_FULL_SCREEN_INTENT" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="com.android.vending.BILLING" />
<uses-permission android:name="com.android.voicemail.permission.ADD_VOICEMAIL" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<application
android:name=".HealthCareApp"
android:allowBackup="true"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:networkSecurityConfig="@xml/network_security_config"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme"
tools:targetApi="n">
<receiver
android:name=".feature.scheduler.services.AlarmBroadcastReceiver"
android:exported="false">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.MY_PACKAGE_REPLACED" />
<action android:name="android.intent.action.TIMEZONE_CHANGED" />
</intent-filter>
</receiver>
</application>
</manifest>
@AndroidEntryPoint
class AlarmBroadcastReceiver : BroadcastReceiver() {
@Inject
lateinit var stateManager: StateManager
override fun onReceive(context: Context?, intent: Intent?) {
when (intent?.action) {
ACTION_MY_PACKAGE_REPLACED -> {
startRescheduleAlarmsService(context!!)
}
ACTION_BOOT_COMPLETED -> {
startRescheduleAlarmsService(context!!)
}
}
}
private fun startRescheduleAlarmsService(context: Context) {
Log.d("TAG", "startRescheduleAlarmsService: ")
val toastText = String.format("Alarm Reboot")
Toast.makeText(context, toastText, Toast.LENGTH_SHORT).show()
stateManager.rescheduleAlarm(context)
}
}
I've also been trying to figure out how to enable auto-start permissions by default for my app, so the BroadcastReceiver works without manual user interaction.
I'd appreciate any guidance on resolving the issue with the BroadcastReceiver not functioning automatically on boot and any suggestions on ensuring auto-start permissions by default. Thank you for your assistance!
Permission ScreenShot: Flipkart auto-launch is enable by default in Realme(Android13)
I found your answer in another thread. here is a link to it.
But if you want it in Kotlin just use Android studio code convertor Java to Kotlin.
Have a nice day.