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
Related
I'm using JSP for a website, and there I have to display time. I have previously tried using LocalDate.now() and Date together with Calendar, like this:
Date date = new Date();
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
But this also gave me UTC Time instead of PST. I can't set it manually with ZoneSet because all user's will have a different timezone. How can I fix this? I'm using Java 8
Java runs on the server, it cannot access the client's clock. If you want the time on the client's computer, you need to use JavaScript's Date object.
See How to get the exact local time of client?.
Another option is to allow users to specify their timezone, and use this to localize times when displayed to the user. You would typically provide a drop-down in some kind of Account Settings page.
But in either case, the time (or timezone) comes from the client. You can make guesses on the server using, for example, geolocation services, but you cannot actually know what the user's local time is from the server.
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!
I'm quite new to Java however I'm building small fitness app and I am stuck on how I would go about resetting the number of steps taken when it hits midnight using Calender object. I'm trying to get the steps reset at 12:00 but save the steps that are there into my shared preference at 23:59. Does anybody know how I would go about doing this?
This is what I have so far in regards to using Calander:
public String getTheDate (){
date = Calendar.getInstance().getTime();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm");
return simpleDateFormat.format(date);
}
I'm a bit stuck so I would be really grateful for any help.
Thanks
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.
As the title says, is it possible to get the holidays from the android calendar? For instance Christmas Eve, how can I determine that this is a holiday in my android application?
The calendar app does not know about holidays and other events. However, you can display in it multiple calendars so include a calendar that shows the events you want and you will have access to it via the calendar API.
For example, here is a calendar that you can add to your Google calendar:
https://www.google.com/calendar/b/0/embed?src=usa__en#holiday.calendar.google.com&gsessionid=OK
As #Tim has said, there is no intrinsic information stored within the Calendar application about holidays. But once you figure out the algorithm for each day you can determine annually when each holiday will fall.
This link might contain information that will help you determine the algorithm for each holiday.