Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Studio set an exact alarm with Icon

I'am trying to make an Alarmclock App. Currently I'am using AlarmManager. My minSdkVersion is API Level 19.

The Problem is, i cant get an exact Alarm like other apps. For Example AlamDroid in the Playstore: It fires the Alarm right in the second when the clock switches to the set Time. It works also on API 19.

My Code right now is:

 Calendar calendar = Calendar.getInstance();
 calendar.setTimeInMillis(System.currentTimeMillis());
 calendar.set(Calendar.HOUR_OF_DAY, hour);
 calendar.set(Calendar.MINUTE, minute);

 if (calendar.getTimeInMillis() < System.currentTimeMillis())
     calendar.add(Calendar.HOUR, 24);

 Intent myIntent = new Intent(context, AlarmReceiver.class);
 myIntent.putExtra(DB_ID, myID);
 pendingIntent = PendingIntent.getBroadcast(context, myID, myIntent, PendingIntent.FLAG_UPDATE_CURRENT);
 alarmManager.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);

The Problem is that in AlarmDroid I get on API Levl 19 an AlarmIcon in the upper Android Statusbar. But how did he do that? Because alarmManager.setAlarmClock is only available at API>= 21. And the Icon comes only with this one!

But still, the accuracy in AlarmDroid is perfect on the second! While mine is worse. Is there any other Method than AlarmManager? Or: What I'am doing wrong? Why I Can't set an very accurate Alarm like the other AlarmClock Apps in the PlayStore. Even with Notification in the Statusbar...

EDIT: On AlarmDroid and other Alarm Apps there is a Notification in the Statusbar one Minute before the Alarm fires off. This seems to be like something built in as every AlarmApp has it!

EDIT2: How is it possible that other Alarms in the Appstore are able to be exact?

EDIT -> POSSIBLE SOLUTION:

For all with the same Problem please look over here:

Link to Stackoverflow Question - Answer from Paweł Nadolski / Mathieu H.

This might be an Explanation / Solution for Samsungphones. It seems that they are checking the Pakagename for Keywords like "alarm", "alert" in order to make the Alarm more exact!

like image 962
MindCode Avatar asked Sep 21 '16 12:09

MindCode


1 Answers

You are facing this issue because above API 18 android optimize the alarm to wake up the phone as less as possible so the solution is use setExact method for preciseness in API 19 and above and other for above .

Api 19

alarmManager.set(AlarmManager.RTC_WAKEUP, time, pendingIntent);

So it will look like this:

if(android.os.Build.VERSION.SDK_INT >=19){
        alarmManager.setExact(AlarmManager.RTC_WAKEUP, time,pendingIntent );
}else{
   alarmManager.set(AlarmManager.RTC_WAKEUP, time, pendingIntent)
}

You may need to add a lint warning

From docs

Beginning with API 19 (KITKAT) alarm delivery is inexact: the OS will shift alarms in order to minimize wakeups and battery use. There are new APIs to support applications which need strict delivery guarantees; see setWindow(int, long, long, PendingIntent) and setExact(int, long, PendingIntent).

You are out of luck here cuz there is no other API option can make alaram exact

Update : seems like your missed the seconds field to set so try adding the seconds to your calendar too.

like image 169
Pavneet_Singh Avatar answered Sep 28 '22 10:09

Pavneet_Singh