Android Studio set an exact alarm with Icon
Asked Answered
S

2

6

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!

Suk answered 21/9, 2016 at 12:57 Comment(1)
try the update part tooGlendon
S
0

I found the solution and it works even on Samsung devices. Not Sure if there is still a need for a package name containing "alarm". But this ist not a Problem since you can just create a additional Package.

If you want the alarm to be exact on the second, just set also the Seconds to 0:

 Calendar calendar = Calendar.getInstance();
 calendar.setTimeInMillis(System.currentTimeMillis());
 calendar.set(Calendar.HOUR_OF_DAY, hour);
 calendar.set(Calendar.MINUTE, minute);
 calendar.set(Calendar.SECOND, 0); // This part will solve the Problem!

Thats it, that solved my Problem!

Suk answered 14/10, 2016 at 19:1 Comment(0)
G
1

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.

Glendon answered 21/9, 2016 at 14:6 Comment(8)
I on phone so can't add docs link and do proper formatting so please avoid and would be great if somebody do the formatting. ThanksGlendon
I tried this but still: It is not exact and there is no Notification in the Statusbar (Like with .setAlarmClock). The alarm fires some 30 to 50 seconds after the set Time.Suk
What's the latency with setextact below api 20Glendon
As I said: on both devices (api 19 and the other api 21) there is a delay of around 30 seconds. It doesnt matter if set or setExactSuk
For efficiency >=19 all alarams are inexact u can't do much about it plus u required to use setExactAndAllowWhileIdle() to escape from doze mode restrictionsGlendon
How is it possible then, that other Alarm Apps like "AlarmDroid" are able to do this?Suk
Above 18 u meanGlendon
Yes, i have tested it in 19 and 21, both are exact with AlarmDroid AppSuk
S
0

I found the solution and it works even on Samsung devices. Not Sure if there is still a need for a package name containing "alarm". But this ist not a Problem since you can just create a additional Package.

If you want the alarm to be exact on the second, just set also the Seconds to 0:

 Calendar calendar = Calendar.getInstance();
 calendar.setTimeInMillis(System.currentTimeMillis());
 calendar.set(Calendar.HOUR_OF_DAY, hour);
 calendar.set(Calendar.MINUTE, minute);
 calendar.set(Calendar.SECOND, 0); // This part will solve the Problem!

Thats it, that solved my Problem!

Suk answered 14/10, 2016 at 19:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.