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


    Reverse Engineer-Andriod

    Sauce Labs
    Anywhere
    • Full Time
    apply now

    Android AOSP Platform Developer (m/w/d)

    Paradox Cat GmbH
    Munich
    • Full Time
    apply now

    Senior Compiler Engineer C++/LLVM – Munich

    Guardsquare
    Munich
    • Full Time
    apply now
Load more listings

OUR VIDEO RECOMMENDATION

,

One does not simply: migrating to Android 12

A not so easy migration to support Android 12! This talk will show you all the subtleties you need to know before migrating your applications to Android 12. From the Manifest to PendingIntent, through the…
Watch Video

One does not simply: migrating to Android 12

Julien Salvi
Lead Android Engineer
Aircall

One does not simply: migrating to Android 12

Julien Salvi
Lead Android Enginee ...
Aircall

One does not simply: migrating to Android 12

Julien Salvi
Lead Android Engineer
Aircall

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
blog
During I/O 2021, Google announced the latest stage for Material Design, Material You (or…
READ MORE
blog
Some apps respond to notification taps by launching an app component (Service, BroadcastReceiver) that…
READ MORE

Leave a Reply

Your email address will not be published.

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

Menu