MindCode MindCode - 1 month ago 10
Android Question

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!

Answer

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!