Create alarm using AlarmManager and BroadcastReciver - java

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

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.

How to make notifications appear during the week and only at certain times?

I want that app user receive notifications for example Monday, Tuesday, Wednesday, Thursday and Friday at 10:45. How can I do that?
You should use AlarmManager. Initialize it in your start activity. Create BroadcastReciever to handle notifications. Change your Manifest file.
Here is example

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!
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!

Android Date: Determining Future Date From Today

I have an app that allows users to set reoccurring alarms. In a fragment, they're allowed to pick a time from a TimePicker and set switches for what day they want the alarm to occur i.e. Mon,Wed,Fri.
I'm saving the time from the TimePicker in milliseconds and the days they choose as a string in an Alarm object like below.
public class Alarm extends RealmObject{
#PrimaryKey
private int id;
private Long time;
private String title;
private String dayOfWeek;}
What I'm trying to determine is how I figure out the future dates based on the days they want to schedule. For example, if today is Monday and they want to schedule an alarm for Mon, Wed and Fri, I would save an alarm object with a time of today, another alarm object with a time of today + 2 days and lastly one with an alarm of + 4 days.
However, the issue is this changes based on what day today is. If today is Tuesday, then I'll need to schedule an alarm for + 1 day, +3 days and + 6 days. What's the easiest way to calculate this day offset?
If I understand your question correctly, I would consider adding user shared preference (http://developer.android.com/reference/android/content/SharedPreferences.html)
containing a string alarmDaysOfWeek="Mon,Wed,Fri" or whatever value is based on user input.
Then you'd implement a Service that checks current day of week against the alarmDaysOfWeek in user's SharedPreferences and alarms if it's a match.
Just a suggestion:-
Set an alarm for everyday and when an Alarm Occur(Service,BroadcastReceiver) then check it if you have to something today(as par user's selection) or just do nothing.
By above ways you wont need calculations for +1 or +2.
I was able resolve this by capturing what day the user selected and then setting the day of the week on the calendar object like newAlarm.setDayOfWeek(x.getDayOfWeek) and then setting the time of the alarm object based on that. This way of a user selected a time in the past i.e. if today were Tuesday and they set an alarm for Monday, it would actually set the calendar object time in the past. Then when the alarm was scheduled, it would immediately fire and I could reschedule it for +7 days or next Monday.

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