Dateformat not updated when the time is changed - android

i am developing an android app where in i am displaying the date and time for the user on the main screen of my app. I am using the below code for it.
SimpleDateFormat sdf = new SimpleDateFormat("EE, dd MMM yyyy");
String strtDates = sdf.format(cal.getTime());
textviewfordate.setText(strtDates);
clk = (DigitalClock)findViewById(R.id.digitalClock);
However, when the time is changed, the date doesnot get updated without refreshing the screen. For example when the time is 11:59:00 PM and when the time changes to 12:00:00 AM, the date will not be updated. It gets updated only when the screen is refreshed. I want the date to be changed automatically without refreshing the screen.
Not getting how to do it! Please Help! Thanks!

If you set a text to textview in onCreate(), it will run only once when that method is called and won't get updated everytime the time changes.
For your need, you have to use handler or executor or any other thread which runs for every user defined time, and you have to set the current time text to textview inside them. So, that the text will be updated.
And also refer the following links, they will help you:
Android run thread in service every X seconds
How to execute a function every two seconds in Java on Android?
display data after every 10 seconds in Android

Related

Timer - to count Time

I am making a quiz application, in which I want to show the time used by the user while playing a session of game. It should be in format HH:MM:SS starting from 00:00:00 till he selects the answer. Timer should be updating every second while user is playing each second. Also, I want to pause the timer while next question is loading. I am not able to understand how to do this. Right now I am able to fetch current time by this code.
Calendar c = Calendar.getInstance();
SimpleDateFormat timeformat = new SimpleDateFormat("HH:mm:ss");
final String startTime = timeformat.format(c.getTime());
Play.timer_box.setText(""+startTime);
It just display whatever the current time is in the box in right format. But not able to understand how to achieve what i want to
You cant just show the time using calendar,you have to write the logic
Start counter when activity/fragment is resumed till user selects
answer
Stop it once user selects the ans,ans start it again once next
question (activity/fragment is loaded)
Convert counter to the time format you want to display,you will need
a logic for the same

How to make simulate time passing in phone

I want to test if a service will still run after lets say, 5 days has passed. I do not want to just set the phone's time, I want the phone to think that the time has passed so that it will get rid of background apps, etc.
How do I do that in adb?
I want the phone to think that the time has passed so that it will
get rid of background apps, etc.
Android can actually finish your application only and not other app.
To get rid of background apps, you can use Task Manager but that's what manual process.
If you have background service running over 5 days, you can actually stop your service, use AlarmManager for it.
As your question is not properly clarified, there could be multiple solution to your problem, of which i have tried to address.
I use the following code in my app to get the current date:
Calendar c = Calendar.getInstance();
System.out.println("Current time => " + c.getTime());
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
AndroidDate = df.format(c.getTime());
Now with that said, all you can do is set the AndroidExpiryDate (string) and set your own date of expiry:
AndroidExpiryDate = "2016-02-28"
Then you can just compare current date (AndroidDate) with expiry date (AndroidExpiryDate ) using if statement:
if ("AndroidDate".equals("AndroidExpiryDate") )
{
//code to get rid of background app
}

how to put current time in database for different locale?, even if in android mobile device time is not set in android

hello my bro & sis i am new in android, currently i am working in android database site. my application user used app from the multiple country, So my problem is how can i insert current time of every user from different locale wise when user perform any database transaction, even if user's android mobile device current time is not proper set yet i want to insert current time in database of his region.
Note: currently i am using MySQL database
Now i am insert current time of user i this way
String date = new SimpleDateFormat("yyyy-MM-dd").format(new Date()).concat(" 00:00:00");
but this is working for only one locale which is default time zone is set. So, Please somebody help me how to resolve my problem.
For saving time inside a database, the best and most country compatible way is to use the time as Long. For example, create a calendar instance:
Calendar cal = Calendar.getInstance();
Long timeToSave = cal.getTimeInMillis();
Then You have to save this Long value inside the database. With this value, You can set all Dateformats for all time zones, also AM/PM values and so on.
I'd recommend to keep the DateTime type in SQL as Long.
Please try this code:
Long now = System.currentTimeMillis();
If you ever need to, you will be able to convert the Long value to string for the current locale:
DateFormat dateFormat = DateFormat.getTimeInstance(DateFormat.LONG, Session.currentLocale);
String date = dateFormat.format(yourStoredDateAsLong);
or
String date = dateFormat.format(System.currentTimeMillis());

Android system time

in my application i need to get current Date and time, every time the user inputs data with it.
I know i can use System.currentTimeMillis(), but it can give me wrong time(because it gives system time, witch can be edited by user)
So i see the only way is to call server for current time, when the user makes data input. but i am not sure that internet connection is always awailable.
Is there any way to get current time (not system time) in android, without using internet connection?
If you don't want system time you need some other source then.
There are a few possibilities that I know:
Get it from web - Internet needed
Get it from router - enabled wifi needed (NTP)
Get it from GPS - GPS fix needed
All of these aren't very helpful I believe. I don't think you can find a way of getting current time without connecting so something externally.
In my opinion you should use system time and assume it's set correctly. When it's not your application shouldn't crash and should gently know user that he has wrong content because of wrong dates ...
I believe there's no way to get the current system time without the timezone.
A good approach would be getting the current system time first
long time= System.currentTimeMillis();
And then getting the correct TimeZone to handle it
Calendar cal = Calendar.getInstance();
TimeZone tz = cal.getTimeZone();
long dateInMillis = System.currentTimeMillis();
String format = "yyyy-MM-dd HH:mm:ss";
final SimpleDateFormat sdf = new SimpleDateFormat(format);
String dateString = sdf.format(new Date(dateInMillis));
Use dateString as your variable which contains current date time as timestamp.
Well, I googled about your topic, and i got logic solution but not tested:
" Use The network-provided values ", in the android phone settings, it;a as shown in the picture bellow:
The screen I show is DateTimeSettings. The checkbox "Use network-provided values" is associated to the shared preference String KEY_AUTO_TIME = "auto_time"; and also to Settings.System.AUTO_TIME
This settings is observed by an observed called mAutoTimeObserver in the 2 network ServiceStateTrackers: GsmServiceStateTracker and CdmaServiceStateTracker.
Both implementations call a method called revertToNitz() when the settings becomes true. Apparently NITZ is the equivalent of NTP in the carrier world.
Bottom line: You can set the time to the value provided by the carrier thanks to revertToNitz(). Unfortunately, I haven't found a mechanism to get the network time. If you really need to do this, I'm afraid, you'll have to copy these ServiceStateTrackers implementations, catch the intent raised by the framework (I suppose), and add a getter to mSavedTime.
For more informations, i suggest you to check this link here
Use the ScheduledExecutorService with scheduleAtFixedRate to send you "clock ticks". If the user initiates an event and the number of accumulated "clock ticks" since the last event doesn't match the time change on the system clock, you're being lied to.
You don't need to know the correct time. You need to know the correct interval. This can be done with any periodic source, even a local one. (Timekeeping is two jobs: a metronome and a labeler for the intervals of the metronome. You don't want the system's labels because they can be made to lie, but the metronome ticks on even if the labels are changed.)
I'd recommend a relatively slow tick rate (<= 1 tick per minute) and rather sloppy comparisons (within 2%, maybe) since the various clocks may not be all that accurate.

gps time vs device time

I create a GPS tracking, but when I use DDMS to send coordinate to the emulator and I want to get time of this coordinate through
Date date = new Date(gpsPos.getTime_stamp()); //gpsPos is an obj
gpsPos.setDate_time(date.toString());
The time I got from the function in miliseconds and I converted it to date using date class in java, but the date I got from converted is different from the real date of device.
How can i get the time location equals real time ? any idea? thx
what is the different between date.toString() and date.toGMTString()?
EDIT:
how can we solve it i want my gps time is equal to the real time (logic enough) in order to use it test sth later?
Date(milliSeconds) will create the Date object based on GMT, Date.toString will create a string based on your locale (by default but can be modified). If the milliseconds you used to create the Date object is actually based on your Locale then your time will be off by 4 hours if you're in EDT as an example.

Categories

Resources