Android Studio set an exact alarm with Icon - java

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!

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.

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!

Related

How to send a local repeating notification in certain days?

I have a reminders app, I made a UI that looks like this where the user can pick multiple days and the hour that he wants to be reminded on. For example, I can select Monday and Tuesday at 15:00 and I should be reminded those days at that given hour. But I can´t figure out how to make this work (mainly, the mutiple days thing). Any ideas?
You can send a local repeating notification with the help of AlarmManager. setRepeating() function can be used to schedule notifications.
similar question, Check this link for better understanding.

Create alarm using AlarmManager and BroadcastReciver

How to create an alarm using AlarmManager and BroadcastReciver to allow scheduling alarm within several days of the week (e.g. Sunday, Monday, Wednesday and Friday) ?
Thanks!
Please follow these links below describe will solve your problem:
how can i get the repeat alarm for week days using alarm manager in android?
https://www.journaldev.com/27681/android-alarmmanager-broadcast-receiver-and-service

Android Alarm Scheduling with repeats

I am in need of creating an app with "reminding" facility. For an example, user can set a reminder for "Jogging" and he can do following when setting up this reminder. Below is the normal process.
User setup the reminder with "cause" to be reminded (ex: jogging )
User can set the reminder to fire at 10am Monday, and so on.
If required, user can set the alarm for multiple days (ex: Monday, Tuesday, Friday )
user can set the reminders to be repeated. (Ex: "Every Monday at 10am". Ie else "Every Monday, Tuesday, Sunday at 10am ")
User can set any number of reminders for various causes.
At the time of reminder, the user will get a notification in his phone.
After setting the reminder, entire process will run in background.
I was trying to find a better way to do this, this is basically like an alarm application. I looked into 'AlarmManager' , 'JobScheduler' and so on, nothing seems to match what I want to do, the correct alarm set-up and repeated reminders when set.
I looked into some alarm app examples too, seems too complicated when my core app is not an "alarm" app.
What is the exact library of doing this? I am not creating an alarm app, this is a process required for another app we build.

Setting Event in CalendarView in Android Studio

I am making an Exercise app that is designed so the user uses it once per day. Say the current date is July 9, 2016... How would I go about setting the first event on the current date(July 9) and the second event on July (11) and so forth.
You can access dates using the Calendar class. to get the current date/time you can just do the following
Calendar cal = Calendar.getInstance();
if you wanted to get a different day you can do something like
Calendar cal = Calendar.getInstance();
cal.set(Calendar.DAY_OF_MONTH, 11);
You can use to the Calendar to get events in the following way -
Calendar calendar = Calendar.getInstance();
long today = Calendar.getTimeInMillis(); // July 9
calendar.add(Calendar.DAY_OF_MONTH, 2);
long day_after_tomorrow = Calendar.getTimeInMillis() // July 11
But, automatically events cannot be set like that in Calendar. It can only be used to get the time and date. If you want some sort of periodic notifications, check out -
Alarm Manager
Notification Manager
These have to be used in combination with Services and Broadcast Receivers.
Check these out for reference: Alarm Manager Example, Sending a notification from a service in Android

How To Calculate Android App Runtime ~Android

I need to calculate my Android App runtime in a day. Is there a way to do this?
For example:
If I start(open) my app at Sunday 07:00AM, and I close my app at Sunday 07:15AM, it will get 15 minutes of runtime. And If I open again my app at Sunday 11:50PM and I close my App at Monday 00:20AM in the very next day, It will get 25 minutes of runtime on Sunday and 20 minutes of runtime on Monday.
Is there a practical way to accomplish this?
Thanks in advance...
I would solve this by saving the current dateTime to your sharedpreferences in your onResume.
In your onPause I would get the saved startTime from your preferences and calculate the difference with the current time. Next, clear the saved starttime and update your database with the time the user was active this day.

Categories