gson: Wrong parsing time in some Android device - android

I got wrong value when using Gson in some Android devices.
Below is json
JSON
{'Time':'1900-01-01T11:00:00.000+08:00'}
Below is my code for testing
new GsonBuilder().create().fromJson("{'Time':'1904-01-01T11:00:00.000+08:00'}", Info.class);
The result is in correct: Fri Jan 01 10:36:42 GMT+08:00 1904
new GsonBuilder().create().fromJson("{'Time':'1905-01-01T11:00:00.000+08:00'}", Info.class);
The result is correct: Sun Jan 01 11:00:00 GMT+08:00 1905
The android version is 10 (API 29), MIUI 12
May I know anybody has this issue

Related

UsageStatsManager not returning correct weekly or monthly results

Question
I want to retrieve a list of UsageStats per week or month. However, the output often has overlapping intervals, e.g. Wed Apr 29 until Mon May 04, when I'm requesting monthly data (see the latest result of the time stamps below).
So my question is: how can I cleanly get the usage stats for a particular week (e.g. Mon - Sun) or month (e.g. Apr 1 - Apr 31) using the UsageStatsManager?
Background
In my Android application, I'm using the UsageStatsManager to retrieve the total time in foreground per week/month. This post about how to use the UsageStatsManager has been very helpful already, as well as this post about the UsageStatsManager producing the wrong daily results (I'm using that method for the daily usage stats, which is working fine now).
Problem
The trouble starts when I try to retrieve the usage stats for weekly/monthly interval. The UsageStatsManager documentation states that
Usage data is aggregated into time intervals: days, weeks, months, and years.
But the output I get from querying the UsageStatsManager is very different. An example of how I'm retrieving the list of UsageStats objects for the last two months:
Calendar endCalendar = Calendar.getInstance();
long endTime = endCalendar.getTimeInMillis();
Calendar startCalendar = Calendar.getInstance();
startCalendar.add(Calendar.MONTH, -2); // since two months.
long startTime = startCalendar.getTimeInMillis();
UsageStatsManager usm = (UsageStatsManager) context.getSystemService(Context.USAGE_STATS_SERVICE);
List<UsageStats> result = usm.queryUsageStats(UsageStatsManager.INTERVAL_MONTHLY, startTime, endTime);
However, when I go over the List<UsageStats> result and log the time stamps of the packages and their foreground time, I get a bunch of time stamps, but they are not in a monthly interval at all. An example of time stamps I retrieved for the Contacts app from the result:
ForegroundTime: 4447 first=Wed Mar 04 03:53:28 GMT+01:00 2020 last=Fri Mar 06 11:46:18 GMT+01:00 2020
ForegroundTime: 176383 first=Fri Mar 06 11:46:18 GMT+01:00 2020 last=Tue Mar 17 07:53:54 GMT+01:00 2020
ForegroundTime: 14337 first=Tue Mar 17 07:53:54 GMT+01:00 2020 last=Tue Mar 17 20:03:31 GMT+01:00 2020
ForegroundTime: 0 first=Tue Mar 17 20:03:31 GMT+01:00 2020 last=Fri Mar 20 14:32:26 GMT+01:00 2020
ForegroundTime: 0 first=Fri Mar 20 14:32:26 GMT+01:00 2020 last=Mon Mar 23 00:12:11 GMT+01:00 2020
ForegroundTime: 0 first=Mon Mar 23 04:27:16 GMT+01:00 2020 last=Mon Mar 23 17:00:29 GMT+01:00 2020
ForegroundTime: 0 first=Mon Mar 23 17:00:29 GMT+01:00 2020 last=Sat Mar 28 00:52:10 GMT+01:00 2020
ForegroundTime: 0 first=Sat Mar 28 00:52:10 GMT+01:00 2020 last=Fri Apr 03 06:45:08 GMT+02:00 2020
ForegroundTime: 81481 first=Fri Apr 03 06:45:08 GMT+02:00 2020 last=Thu Apr 09 18:04:05 GMT+02:00 2020
ForegroundTime: 0 first=Thu Apr 09 18:04:05 GMT+02:00 2020 last=Thu Apr 16 10:31:23 GMT+02:00 2020
ForegroundTime: 157189 first=Thu Apr 16 10:31:23 GMT+02:00 2020 last=Sun Apr 26 02:10:31 GMT+02:00 2020
ForegroundTime: 0 first=Sun Apr 26 02:10:31 GMT+02:00 2020 last=Wed Apr 29 13:21:12 GMT+02:00 2020
ForegroundTime: 43516 first=Wed Apr 29 13:21:12 GMT+02:00 2020 last=Mon May 04 17:45:43 GMT+02:00 2020
which seems to be partitioned almost randomly into intervals.
Any links/explanation as to why UsageStatsManager has this behaviour, or an example of an open-source application that is using the UsageStatsManager for querying weeks/months/years would be very helpful. Thanks in advance.
I might not be able to solve the issue but I will suggest a way around it which is using queryEvents function for all of them - daily, weekly and monthly data.
Querying weekly data(data of last 7 days) by this function is similar to querying daily data by it. (you mentioned that you have already implemented the function for daily data)
As for monthly data, you cannot directly use the function since events are kept by system for around one week as said in documentation. You need to store usage data in a local database and query from it.

Different Android dates in emulator and real device

I'm experiencing a very strange problem with Date objects in Android,
when I run the following code I get different dates in the emulator and in the real device. (str1 and str2 come from the following json result
json file)
String str1="1450967458";
String str2="1450980000";
Date dstart=new Date((long)Long.parseLong(str1)*1000);
Date dend=new Date((long)Long.parseLong(str2)*1000);
Log.i("VILLANUEVA","START:"+dstart.toString());
Log.i("VILLANUEVA","END:"+dend.toString());
Dates in real device:
... I/VILLANUEVA: START:Thu Dec 24 15:30:58 CET 2015
... I/VILLANUEVA: END:Thu Dec 24 19:00:00 CET 2015
Dates in emulator(These are the correct dates)
...I/VILLANUEVA: START:Thu Dec 24 09:30:58 EST 2015
...I/VILLANUEVA: END:Thu Dec 24 13:00:00 EST 2015
It seems to be a difference of 6 hours between the device and the emulator. I've tested with Calendar class and I get the same difference. Any help would be appreciated. Thanks in advance.

getting "androidApplicationContext must be not null!" crashes from crash reporting service

after supporing linkedin social network, an app i'm working on has caused many crashes that the only thing that is written there is "androidApplicationContext must be not null!" .
i've searched the internet , and found only the next links as clues:
http://pastebin.com/QeV9D8XH
http://logs.nslu2-linux.org/livelogs/android-dev/android-dev.20120829.txt
it says :
Aug 28 17:15:23 <S3nsat10n> has anyone run across a stacktrace like this? http://pastebin.com/QeV9D8XH
Aug 28 17:15:33 <S3nsat10n> has to do with a null app context inside the apache httpclient
Aug 28 17:15:38 <Hodapp> wongk: yeah, I'm just really confused about why it's doing this...
Aug 28 17:15:47 <S3nsat10n> highly modified httpclient it would seem, judging my the naf.gba package stuff
Aug 28 17:15:51 <S3nsat10n> Seems to be happening only on Samsung Galaxy S devices, but I don't know if that's always the case.
Aug 28 17:16:48 <wongk> S3nsat10n: never see that
Aug 28 17:16:50 <wongk> seen
Aug 28 17:17:17 <S3nsat10n> yeah :/
Aug 28 17:17:22 <S3nsat10n> neither has most of the internet...
Aug 28 17:17:23 <S3nsat10n> heh
has anyone else got this weird and super rare error?
if so how did you fix it?
Two solutions :
Use HttpURLConnection instead of HttpClient
Create an instance of HttpClient in the main thread of your application, as soon as possible, for instance in its onCreate method.
More details here.

plotting real time csv data in android

By using arduino, python serial I am able to log data from a temp sensor from dev/ttyACM0 into a file timetemp.csv
which looks like
Wed Jun 12 12:59:45 2013,27
Wed Jun 12 12:59:46 2013,27
Wed Jun 12 12:59:47 2013,27
Wed Jun 12 12:59:48 2013,27
Wed Jun 12 12:59:49 2013,27
Wed Jun 12 12:59:50 2013,26
Next thing I want is to show a realtime plot of time-sensordata preferably in android client by setting up the system(where the csv file resides) as a server...Will this be possible using the androidplot or any other plot utility like Rgraph...What are your suggestions
Yes you can use androidplot for it. It should be possible. Also you can do it by yourself.
You can look into this discussion for reference code.
Easiest way to plot array of floats

change TimeZone display type from new java.util.Date() in android

When I print the new java.util.Date() it prints this in Android
Thu Feb 22 05:30:00 Asia/Calcutta 2007
I need this to be print as Thu Feb 22 05:30:00 IST 2007
please help
Alright guys I got my problem solved. Here is the solution what I adapted
I had the date in format in dd-mm-yy which I need to convert like Thu Feb 22 05:30:00 IST 2007
so I just created the object of the SimpleDateFormat class with the desire format suiting the format Thu Feb 22 05:30:00 IST 2007 and finally called format() method on SimpleDateFormat instance

Categories

Resources