Blog Infos
Author
Published
Topics
,
Author
Published
Check if the app has permission to set exact alarms?
val alarmManager: AlarmManager = getSystemService(Context.ALARM_SERVICE) as AlarmManager
// check if our app can set exact alarms
if(alarmManager.canScheduleExactAlarms()){
Log.d("MainActivity", "onCreate: SCHEDULE ALARM")
}

canScheduleExactAlarms()

 

Ask users to grant the app access
val alarmManager: AlarmManager = getSystemService(Context.ALARM_SERVICE) as AlarmManager
when {
alarmManager.canScheduleExactAlarms() -> {
Log.d("MainActivity", "onCreate: SCHEDULE ALARM")
}
else -> {
// go to exact alarm settings
Intent().apply {
action = ACTION_REQUEST_SCHEDULE_EXACT_ALARM
}.also {
startActivity(it)
}
}
}

Open Alarms and Reminders Settings

BroadcasterReceiver implementation
class AlarmReceiver : BroadcastReceiver() {
private val TAG = "AlarmReceiver"
override fun onReceive(context: Context, intent: Intent) {
when (intent.action) {
ACTION_SCHEDULE_EXACT_ALARM_PERMISSION_STATE_CHANGED -> {
Toast.makeText(context, "RECEIVED ALARM PERMISSION", Toast.LENGTH_LONG).show()
}
"from alarm" -> {
Toast.makeText(context, "ALARM FIRED", Toast.LENGTH_LONG).show()
}
}
}
}

AlarmReceiver-Android12

 

<receiver
android:name=".alarmreceiver.AlarmReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.app.action.SCHEDULE_EXACT_ALARM_PERMISSION_STATE_CHANGED" />
<action android:name="from alarm" />
</intent-filter>
</receiver>

Add receiver to manifest

 

override fun onCreate() {
super.onCreate()
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
alarmReceiver = AlarmReceiver()
registerReceiver(
alarmReceiver,
IntentFilter(android.app.AlarmManager.ACTION_SCHEDULE_EXACT_ALARM_PERMISSION_STATE_CHANGED),
)
}
}
override fun onDestroy() {
alarmReceiver?.let {
Log.d(TAG, "onStop: Un-register receiver")
unregisterReceiver(it)
}
super.onDestroy()
}

Register runtime receiver

Job Offers

Job Offers


    Developer (m/w/d) Backend/ Mobile

    Payback GmbH
    Cologne, Germany
    • Full Time
    apply now

    Senior Android Engineer – Big Release Team

    Zalando SE
    Berlin
    • Full Time
    apply now

    Lead Android Engineer

    ASOS
    London
    • Full Time
    apply now
Load more listings

OUR VIDEO RECOMMENDATION

Behind the Curtains

All smartphones have cameras, and we know we can use specific APIs to get amazing shots. But are they the best cameras? Probably not! What if we wanted to drive an external camera, much more powerful than a smartphone? How would we connect to it, and how would we trigger a shot? This and much more…
READ MORE

Jobs

YOU MAY BE INTERESTED IN

YOU MAY BE INTERESTED IN

blog
Splash screen is the initial screen you’ll see when you open your app. It…
READ MORE
blog
Android 12 brought several changes and one of them, that raises some questions, is…
READ MORE

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Menu