since android 10 was launched, there are problems with alarms in my app, I have the following snippet, where I test with AlarmManager
to see if the alarms that I program are executed.
When I use setAlarmClock
, the alarm runs at the set time, but when I try to use setInexactRepeating
(or setRepeating
), android ignores the alarm, it doesn't run at all.
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val am = getSystemService(Context.ALARM_SERVICE) as AlarmManager
val timeToTrigger = System.currentTimeMillis() + 10 * 1000
val intervalo: Long = 60 * 1000
val intent = Intent(this, AlarmReceiver::class.java)
val pending = PendingIntent.getBroadcast(this, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT)
//am.setAlarmClock(AlarmManager.AlarmClockInfo(timeToTrigger , null), pending)
am.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + intervalo,intervalo,pending)
}
}
Here is my Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.lb.alarm_clock_sample">
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission
android:name="android.permission.WRITE_SETTINGS"
android:maxSdkVersion="19" />
<uses-permission android:name="android.permission.DISABLE_KEYGUARD" />
<uses-permission
android:name="android.permission.READ_PHONE_STATE"
android:maxSdkVersion="22" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="com.android.alarm.permission.SET_ALARM" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".AlarmsListActivity" />
<receiver android:name=".AlarmReceiver" />
</application>
</manifest>
Thanks in advance
EDIT
According to @Crispert, using android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS
can help the alarm not be ignored.
In manifest add the following permission:
<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS"/>
Then in the activity, create a function to give the user to choose if they want the application to work in the background:
@RequiresApi(Build.VERSION_CODES.M)
private fun ignoreBatteryOptimization() {
val intent = Intent()
val packN = packageName
val pm = getSystemService(Context.POWER_SERVICE) as PowerManager
if (!pm.isIgnoringBatteryOptimizations(packN)) {
intent.action = Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS
intent.data = Uri.parse("package:$packN")
startActivity(intent)
}
}
If the user agrees to ignore the battery optimization, the execution occurs in the background and the alarms work without any problem (according to the tests I have been doing, so far I have no problems). Thanks.