Skip to content

Instantly share code, notes, and snippets.

@kishan-vadoliya
Created December 30, 2023 13:44
Show Gist options
  • Save kishan-vadoliya/153a6a57ad11dbb7575d09f358146b49 to your computer and use it in GitHub Desktop.
Save kishan-vadoliya/153a6a57ad11dbb7575d09f358146b49 to your computer and use it in GitHub Desktop.
Set Exact Alarm in Android 13 and above
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="com.huawei.permission.external_app_settings.USE_COMPONENT" />
<uses-permission android:name="oppo.permission.OPPO_COMPONENT_SAFE" />
<uses-permission android:name="com.google.android.finsky.permission.BIND_GET_INSTALL_REFERRER_SERVICE" />
val receiver = ComponentName(context, AlarmReceiver::class.java)
context.packageManager.setComponentEnabledSetting(
receiver,
PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
PackageManager.DONT_KILL_APP
)
fun Context.openExactAlarmSettingPage() {
val manager = getSystemService(Context.ALARM_SERVICE) as AlarmManager
Log.d("TestData","Utils openExactAlarmSettingPage :${manager.canScheduleExactAlarms()}")
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
val alarmManager = ContextCompat.getSystemService(this, AlarmManager::class.java)
if (alarmManager?.canScheduleExactAlarms() == false) {
Intent().also { intent ->
intent.action = Settings.ACTION_REQUEST_SCHEDULE_EXACT_ALARM
startActivity(intent)
}
}
}
}
fun Context.isScheduleExactAlarm() :Boolean {
val manager = getSystemService(Context.ALARM_SERVICE) as AlarmManager
val alarmManager = ContextCompat.getSystemService(this, AlarmManager::class.java)
return alarmManager?.canScheduleExactAlarms()!!
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment