I'm using GA for an Android App.
I'm trying to use user timings to report how much time has passed for some actions in my code, so what I basically do is this:
At some point in the code I get System.currentTimeMillis(), at another point I do it again and subtract the latter from the former to get how much time has passed. I then report it to GA like this:
long time1 = System.currentTimeMillis();
...
long time2 = System.currentTimeMillis();
long timingValue = time2 - time1;
tracker.send(new HitBuilders.TimingBuilder()
.setCategory(timingCategory)
.setValue(timingValue)
.setVariable(timingVariable)
.setLabel(timingLabel)
.setCustomDimension(1,1)
.setCustomMetric(1, timingValue).build());
When I look at the "App Speed" section everything looks fine. It seems to report a logical average time in seconds like I expect.
The problem is that I want to use several dimensions (the secondary dimension is not enough) so I created all these timings as metrics as well so I can see them in a custom report. When I look at the report, the time I see there is 09:43:39 and I'm not sure what's the format here. Is it seconds:tenth of a second:hundredth of a second? And How can I see the average time of these metrics? I'm not sure if what I see is only the total amount of time or something else?
A Value of type time (for both events and custom metrics) should be passed as a whole number (no commas or decimals) representing seconds. So for example, 10 seconds should be 10, and 5 minutes should be 300, etc. Note that in the reports, it will be formatted as hh:mm:ss.
Related
I'm having fierce problems trying to get DateUtils.getRelativeTimeSpanString to return what it's supposed to in Android.
I've been through every similar question here and none of the solutions posted are working or relevant.
Here's my code:
CharSequence relativeDate =
DateUtils.getRelativeTimeSpanString(System.currentTimeMillis(),
currentNewsItem.getTimeInMilliseconds(),
DateUtils.HOUR_IN_MILLIS, DateUtils.FORMAT_ABBREV_RELATIVE);
dateView.setText(relativeDate);
The currentNewsItem.getTimeInMilliseconds method does indeed return a long (for example, just now it passed 1521759734 which seems correct) and the getRelativeTimeSpanString passes a correctly formatted date string to the view in the format "March 23, 2018" so it knows the correct date. But I need it to pass "x Hours ago" or "x minutes ago" and I cannot for the life of me figure out why it's not.
I've checked the time on the emulator, and the problem occurs on my device so it's not the timezone settings.
Documentation says that it will revert to a date string like this if the time span is greater than 7 days but I've checked the long that's being passed as the old time and the converter websites shows it's the exact time 4 hours ago. Besides, it's showing the correct date (today), just not the hours/minutes ago.
I've tried the HOUR_IN_MILLIS, MINUTES_IN_MILLIS and SECONDS_IN_MILLIS constants for the third parameter and I've tried removing the last abbreviation parameter and it's still the same result.
I've tried the various public method signatures to try find one that might work but they all have the same result.
Has anyone experienced this or can anyone point out where I'm going wrong?
Many thanks.
If you check DateUtils.getRelativeTimeSpanString, the parameters are listed as:
getRelativeTimeSpanString (long time,
long now,
long minResolution,
int flags)
However in your code,
now: the current time in milliseconds, is passed as 1st parameter instead of currentNewsItem.getTimeInMilliseconds().
To get relative timespan like X minutes ago you can use DateUtils.MINUTE_IN_MILLIS and flag like DateUtils.FORMAT_ABBREV_RELATIVE will abbreviate relative time as X mins. ago.
So, you can change your code to:
CharSequence relativeDate =
DateUtils.getRelativeTimeSpanString(currentNewsItem.getTimeInMilliseconds(),
System.currentTimeMillis(),
DateUtils.MINUTE_IN_MILLIS,
DateUtils.FORMAT_ABBREV_RELATIVE);
dateView.setText(relativeDate);
I'm trying to find the difference between 2 times using Joda Time. I've tried using some of the examples on here and can not get a value to return. Here is what I've tried: Where time is in a format like 17:23 and the to and from variables are time zones like America/Los_Angeles or Europe/London
DateTime dtFrom = DateTime.now()
DateTime dtTo = new DateTime(dtFrom.withZone(DateTimeZone.forID(to)));
//calculate time difference
Period period = new Period(dtTo, dtFrom) ;
Minutes minutes = Minutes.minutesBetween(dtFrom, dtTo);
long diffInMillis = dtFrom.getMillis() - dtTo.getMillis();
I've tried using different methods to compare these two and always get no value returned...period returns PT0S, minutes returns PT0M, and diffInMillis returns 0. What am I doing wrong here? Thanks!
You are using the method withZone() which is defined to preserve the (POSIX) instant in milliseconds since 1970-01-01T00:00Z. So dtFrom and dtTo have still the same instant meaning they describe the same global time although their local time representations are different due to different time zones.
Result: The difference between two same instants is exactly equal to zero regardless which time unit you use.
However, if you are interested into the local time difference instead please determine the zone offset difference, for example using the method getOffset().
I've just learned about the AlarmManager and tried to play around with it. As I understood the alarms are set by saying that it needs to be called after X miliseconds like in the code below:
am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + (sec * 1000), pendingIntent);
However, I'm concerned about the performance of system when it comes to long periods of time.
If I need to set alarm that will activate notification say after 10 months, what should I do? Do I need to convert needed period of time into miliseconds and pass it in the same way? Or there are other more efficient ways to work with long periods of time?
Yes, you have to pass it as milliseconds. I'm not sure why you're concerned about performance from that, there's no loss in performance form passing a large value instead of a small one. Your only real problem is that doing in X months, the length of a month isn't regular. I'd create a Calendar object for the end time and convert that to milliseconds to get it right.
Use like this
int month = 2;
alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),AlarmManager.INTERVAL_DAY * 30*month, pendingIntent);
How to set alarm for long duration has already been discusses above.
You just need to keep a check that if device gets restarted you reset your alarm because alarms gets canceled once system goes off.
link here
I have timer for a task. And all the sessions will be added up to each other.
Let's say today user spent 5 minutes
Other day he spent 1 hour, here this one hour will be added to the 5 minutes
and so on..
So it will be the total time in one value..
How can I do this ? Is it by Milliseconds or Date object ?
Date is more useful when you are dealing with actual calendar dates.
If you just want to keep track of time intervals/durations, just have a long variable and keep on adding the durations to this.
EDIT : Long.MAX_VALUE is 9,223,372,036,854,775,807. So, you don't really need to worry about the overflow either.
You can keep track of all the milliseconds using Date().getTime(), which returns the time since Epoch. So whenever you need to add/remove, just take your Date object, invoke .getTime(), and add/remove from the total. Then when you're done, you can convert the milliseconds to whatever format you need.
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.