Setting Alarm for different timezones using AlarmManager - java

My Android application needs to set a number of alarms over the next few weeks using the AlarmManager. As of now, I'm planning to hard code those date/times using an array of calendar objects.
The question I'm facing is, how do I handle different timezones? Supposing I set an alarm for
2nd August 2012 15:00 GMT , how do I make sure that a person using the app somewhere else (say India i.e. GMT+5.30) gets the alarm at 2nd August 2012 20:30
Is there a way the app can get the timezone difference (i.e. the '+5.30' part) of that particular device so that before setting the alarm, to each Calendar object I can add that difference and then set the alarm?

I would suggest you to use System.currentTimeMillis(), Which should return u the time of system depends on what timezone the phone is in

Related

Android Background Service which check current Date

I programmed a simple calendar app where you can add Events to specific Dates.
When I close the app it still should check once a day which Date is it and if an Event is on this Date. If there is an Event a push notification shall appear.
How could I do that? I save the Date with an event within the sharedPrefrences.
Can someone help?
If it doesn't matter when during the day the notification is sent, then #AliasCartellano comment is correct. However, if you want to send the notification at a specific time during the day (i.e. not at 23:59 when the user is sleeping), then you should use AlarmManager. Specifically, look at setAlarmClock(). This is its intended usage.

android studio where does the calendar get the time from

I have an old android phone with no SIM card that I want to use to test smaller screens with my timecard app, what I'm wondering is, where does the calendar method,
For example:
Calender now = Calendar.getInstance()
get the time from? A server or does it get the time data directly from the android cellphone?
The method getInstance returns a calendar whose locale is based on system settings and whose time fields have been initialized with the current date and time.
https://developer.android.com/reference/java/util/Calendar.html

Can't get current time forecast using forecast.io API

I'm building a mega simple weather Android app using the forecast.io service.
Using the default call of
https://api.forecast.io/forecast/<my_key>/37.8136,144.9631?units=auto
Always seems to give me the forecast at a time that I don't care for - ie, its currently 2.40PM here, and it keeps giving me 3.40AM forecast.
So I then try to use the time param and yet it still gives me 3.40AM, not 2.40PM
https://api.forecast.io/forecast/<my_key>/37.8136,144.9631,1451619638?units=auto
I've validated 1451619638 as my current unix time via the helpful site http://www.epochconverter.com/
Any pointers as to why I can't seem to get MY local current time?
The API returns the timezone and then the offset from GMT. Check those.
The idea of the first one is if you do not provide the time, you will get a current forecast, and all you will have to do is convert it into your timezone. So if they are doing GMT and you are GMT - 8, you simply subtract 8 from the time value.
Both fields are there:
latitude: The requested latitude.
longitude: The requested longitude.
timezone: The IANA timezone name for the requested location (e.g. America/New_York). This is the timezone used for text forecast summaries and for determining the exact start time of daily data points. (Developers are advised to rely on local system settings rather than this value if at all possible: users may deliberately set an unusual timezone, and furthermore are likely to know what they actually want better than our timezone database does.)
offset: The current timezone offset in hours from GMT.
I have been using this API for a while.

Change link to download every 7 days?

I'm downloading a certain website in html format to my device, so that I can display it in webview in offline mode. The only problem is that the link is dynamic, and it changes once a week. To keep the html item updated as much as possible, I want the app to download it once a week.
Let's say for example that this is the websites address:
www.mywebsite.com/1
Next week, the address will be:
www.mywebsite.com/2
And week after that, the website will be:
www.mywebsite.com/3
I already figured I would do this be declaring a variable that would be changing, something like
int week;
String urlToDownload = "www.mywebsite.com/" + week;
But how do I make it so that this variable will change everyday even if the app is not started, or is there a better way to do this?
You can maybe use AlarmManager class. That allows you to plan something on the background, when app is not even running.
I would use the most simple solution. Do you know what time does the URL change? You can always check the time of previous start of application and when next app is started check it and determine how many weeks is from that.
Use the java.util.Calendar
Calendar calender = Calendar.getInstance();
MyLog.d("Current Week:", "" + calender.get(Calendar.WEEK_OF_YEAR));
This prints "Current Week: 37"
With that maybe you can write code to get the appropriate page. The week nr are kind of static
You can update the variable during the onStart() phase. Make a constant that has the start date, and then get the current date and figure out the offset. This way even if the app hasnt been started in a long time, once it is started you will have the proper link.

How to handle missing PST hour in Java

I have a webapp that is to be deployed on server having Pacific TimeZone setting. My app is capable of handling PST<->PDT date conversions in calculations but i am facing issue handling missing hour/Invalid Time as described below.
Invalid Time – Between 02:00 AM to 03:00 AM on second Sunday in March due to forward auto-adjustment of clock, this time does not exist in Pacific Time Zone.
So whenever date like 10-Mar-2013 02:00:00 is entered system automatically converts it into 10-Mar-2013 03:00:00. I understood this is happening because this time is actually does not exist in Pacific timezone.
But as per requirement server timezone can not be changed (for eg. GMT) and still need to capture above time (10-Mar-2013 02:00:00). Additionally server auto-adjutment clock settings also can not be changed so i have to do application code change to support above.
Thanks in advance for your help.
Adding code for more clarification:
SimpleDateFormat sdf = new SimpleDateFormat("ddMMyy HH:mm:ss");
//sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
Date date = sdf.parse("100313 02:00:00");
Date date1 = sdf.parse("100313 03:00:00");
System.out.println(sdf.format(date));
System.out.println(sdf.format(date1));
Output:
100313 03:00:00
100313 03:00:00
If i uncomment commented line then output:
100313 02:00:00
100313 03:00:00
Regardless of the time zone setting of the computer, you should always be able to get the current UTC time. Java abstracts this from the OS details, so any time the documentation says you are getting a UTC value (such as milliseconds from 1/1/1970 UTC) then it is accurate. There is no such thing as missing or ambiguous time in UTC.
The reason that it is recommended your servers are set to UTC time zone has to do with the way the system bios clock is synchronized with the operating system's clock. Microsoft Windows operating systems keep the bios on local time and adjust it as necessary, while Linux and Macintosh keep the bios permanently on UTC. Because of this, on Windows systems, there is a remote possibility that the clock is read incorrectly if the adjustment didn't work for some reason. More on this issue here.
If your application is converting dates entered to values that only make sense in the computer's local time zone, then you have projected that time zone on to your users. For desktop applications, this is probably correct because the user is running the code on their own computer. For web applications (and other server-side code), this is certainly the wrong thing to do, because it is possible for the client to be in a different time zone than the server.
Please update your question to show the specific code from your application that is causing the problem you described. We can then point you at the specific changes you need to make so your application behaves correctly.

Categories

Resources