Set default time zone to use for an android application - android

I'm developing an application for android that has to list activities with its date and time. I would like to ignore the time zone of the device and show always the time in timezone +02:00.
The application is for Spain, but If someone from UK that is in another time zone use the application I want that the time for activities appear in local spanish time.
The date is in UTC format, using Calendar.getTime().toLocaleString(), the time appers in local time zone of device.
I would like to know if the is a way to set a default time zone that has to use my application, and when I invoke Calendar.getTime().toLocaleString() method get the time always in local time zone +2 despite of the device timezone

TimeZone.setDefault(TimeZone.getTimeZone("GMT+3"));
This sets the default timezone. Subsequent calls to Calendar.getInstance() always return as if the device was in that timezone.

You need to set the time zone of your calendar.
This can be done like this :
Calendar.setTimeZone(TimeZone timezone);
And then your calendar will work with a different time zone.
If you want to change the system time zone this can be done by using setTimeZone() of AlarmManager.
When doing that the manifest has to contain the right permission, which is
android.permission.SET_TIME
Link to Documentation
And there is some other solution i found Here, check it out.

Related

If I set an in-app alarm according to GMT time then how am I am able to trigger it according to various timezones automatically in android

Calendar calendar = Calendar.getInstance(TimeZone
.getTimeZone("GMT"));
This is how I am setting my timezone to GMT for alarm purpose and setting the alarm according to GMT. Now is there any auto configuration way in android by which the alarm time will be set to according to its timezone ? if there is no auto configuration for this purpose then can anyone suggest me any idea ?
Please read the dst/tz best practices, and the timezone tag wiki.
In particular, you should not schedule a future event (such as an alarm) by GMT. You should set an alarm based on local time - either for a specific time zone, or for whatever the system time zone happens to be.
Typically on a mobile device, you would not bind an alarm to anything but the system local time zone. For example, if a user has a phone with a daily alarm set to 8:00 AM every day, then even if the user travels with their phone to a different time zone, the alarm would still go off at 8:00 am - even though that's a different point in GMT/UTC than was originally scheduled. This is how all modern smartphones work.
Anyone that tells you "always use UTC/GMT" has not thought about scheduling.

TextClock setTimeZone() not changing TimeZone

I have a TextClock in my app. I want to give the user the option of the TimeZone it displays (rather than just using the device default).
I'm using the code
textClock.setTimeZone("PST");
Which according to the API documentation should change the TimeZone of the clock to whatever the string timezone identifier is (in this case Pacific Daylight Time) but the clock isn't changing. Interestingly though it isn't showing the current time either. I'm in the UK so we are using GMT+1 and if I don't .setTimeZone() it shows that but if I do it shows GMT+0 regardless of the timeZone I enter.
Turns out that despite the documentation saying you can use three letter identifiers (or maybe I picked that up wrong), you can't.
textClock.setTimeZone("America/Los_Angeles");
That works for setting to PDT for example.

How does Automatic date and time work in Android?

For some date-based calculation I needed today's correct date, for which I enabled Automatic date and time option in Date and Time Settings and this gives me the correct time and date.
I want to know how Android gets the correct date and time, even though I have not enabled any internet accessibility (WiFi, SIM Data).
Also look at this: There is no response from requestLocationUpdates
Please give me some reference on this.
It capture date and time from the network provider. not only android lots of other phone can do the same.
Autometic date time is provided be the network provide (exmp: vodafone,airtel etc etc ).

One month restiction for application free use

Requirement:
In my application I want to allow user to free use all features for one month from the day of application's first run. For this I have store first run date in application database and compare first run date with current date every time when application launch.
Implementation:
For getting current date we have several function like Calendar.getInstance(); Date date=new Date(); these function will return the date\datetime of the standard "wall" clock (time and date). According to document Which can be set by the user or the phone network.
The Problem:
Every thing seems to be work fine but what if user change date from Setting and set it to past date. For example user have first run application on 7 June 2013 so after 6 July 2013 application must show that he have to purchase subscription, but if user change date of device back to 30 June etc. then the restriction will not work.
So is there any why to implement it correctly?
Is there any why to get actual time of device that is not be editable by user?
Edit:
Application can work offline, so user can turn off internet connectivity.
How about storing two dates - the date the app was first used and the date it was last used. 30 minus the difference between the two dates will give you the number of days remaining and if the current date is earlier than the last used date you know that they have changed the date back.
Just an idea - not tried it but in theory it should work.
Store the first day it was used and the last day it was used, and listen to the ACTION_TIME_CHANGED intent. Also, try using an AlarmManager.
Or if you have network, store it in a database off the phone.
But you probably need to ask yourself if someone really would change the date of their phone just to use your app. It would seriously mess up a lot of things for them: alarms, calenders, syncing to other services and so on. I don't think it is a problem in reality.
If it's a possibility, you could use a one-shot GPS request to get a time value. The user can't change the GPS time that it receives. Adding the permission is a pain if you don't need it, but this would prevent bad behavior caused by the user mucking with the date/time.
Event(APP_INSTALL) -> storeOnServer(INSTALL_TIME_KEY,getTimeFromServer());
Event(APP_LAUNCH) -> IF(getTimeFromServer -getStoredTime(INSTALL_TIME_KEY) >30)
-> showTrialPeriodEndedAlert()
-> quitApp();

How to set custom TimeZone in Android

In my Application I am displaying some info to the users according to the user's TimeZone.
I am allowing the users to select different cities in the world and to get the required info. By default I will get the TimeZone of the user from their Device using
`TimeZone myTZ = TimeZone.getDefault();`
And once the user changes the TimeZone , I am saving it in preferences. No problem in storing and retrieving the values from the preferences.
When another TimeZone is selected I set that TimeZone as default TimeZone using
`TimeZone.setDefault(TimeZone.getTimeZone("my_timezone"));`
when I print the TimeZone in the Log I am getting the TimeZone value as I set it. But when I use that TimeZone in my calculations I am getting previous TimeZone Value.
So I think the problem is the TimeZone is not changing , I want the user to set the selected TimeZone as their default TimeZone in their device. How shall I do this. What is the mistake I am doing?
Getting stuck in this for three days. Any suggestions to get out from this? Quick suggestion will helps me a lot. Thanks in Advance!!
As per documentation for TimeZone.setDefault(...) -
Overrides the default time zone for the current process only.
Warning: avoid using this method to use a custom time zone in your
process. This value may be cleared or overwritten at any time, which
can cause unexpected behavior. Instead, manually supply a custom time
zone as needed.
Apparently to change timezone through code you have to use AlarmManager. See the discussion here.
AlarmManager alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarm.setTimeZone(timezone);
It requires this permission to be set in the manifest
<uses-permission android:name="android.permission.SET_TIME_ZONE"></uses-permission>

Categories

Resources