Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android alarmManager.setAlarmClock Crash Android 7

I am getting a crash (on android 7 only) when calling alarmmanager.setAlarmClock in a function where i am setting alarm. I have no idea what is wrong here. And how to fix this crash.

This is the function which is getting crash when calling alarmManager.setAlarmClock

fun setBeforeAlert(
        context: Context,
        offset: Long,
        beforeMin: Int,
        pendingIntent: PendingIntent
    ) {
        if (offset - beforeMin * 60 * 1000 > 0) {
            val alarmManager = context.getSystemService(Context.ALARM_SERVICE) as AlarmManager
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                val alarmClockInfo = AlarmManager.AlarmClockInfo((System.currentTimeMillis() + offset) - beforeMin * 60 * 1000, pendingIntent)
                alarmManager.setAlarmClock(alarmClockInfo, pendingIntent)
            }
            else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                alarmManager.setExact(AlarmManager.RTC_WAKEUP, (System.currentTimeMillis() + offset) - beforeMin * 60 * 1000, pendingIntent)
            }
            else {
                alarmManager.set(AlarmManager.RTC_WAKEUP, (System.currentTimeMillis() + offset) - beforeMin * 60 * 1000, pendingIntent)
            }
        }
    }

This is the pending intent which i am passing to above function:

val intent = Intent(context, AlertAlarm::class.java)
        intent.putExtra(Constants.key_alert_alarm_code, reqCode)
        intent.putExtra(Constants.key_alert_type, type)
        intent.putExtra(Constants.key_alert_min, min)
        val pi = PendingIntent.getBroadcast(context, reqCode, intent, PendingIntent.FLAG_UPDATE_CURRENT)

This is the crash report i am getting:

java.lang.NullPointerException: Attempt to invoke virtual method 'int com.android.server.AlarmManagerService$Batch.size()' on a null object reference
at android.app.ActivityThread.handleReceiver(ActivityThread.java:3377)
at android.app.ActivityThread.-wrap18(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1800)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:232)
at android.app.ActivityThread.main(ActivityThread.java:6802)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1103)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:964)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int com.android.server.AlarmManagerService$Batch.size()' on a null object reference
at android.os.Parcel.readException(Parcel.java:1714)
at android.os.Parcel.readException(Parcel.java:1661)
at android.app.IAlarmManager$Stub$Proxy.set(IAlarmManager.java:246)
at android.app.AlarmManager.setImpl(AlarmManager.java:680)
at android.app.AlarmManager.setAlarmClock(AlarmManager.java:601)
at com.mypackage.util.AlarmUtils.setBeforeAlert(AlarmUtils.kt:42)
at com.com.mypackage.util.AzanUtils.setBeforeAlertAlarms(AzanUtils.kt:358)
at com.com.mypackage.util.AzanUtils.setAlarms(AzanUtils.kt:160)
like image 481
daliaessam Avatar asked Sep 13 '19 06:09

daliaessam


1 Answers

We also get the same crash on OPPO devices (Android 7). If you look at size() method in AlarmManagerService.Batch.class, it NEVER should be a null. I assume some manufacturers have changed the implementation of the IAlarmManager (AlarmManagerService) which is causing the problem. You can't do anything about it.

like image 171
Zhanbolat Raimbekov Avatar answered Sep 18 '22 14:09

Zhanbolat Raimbekov