Posted on

Android JSON Parsers Comparison

https://medium.com/@IlyaEremin/android-json-parsers-comparison-2017-8b5221721e31

List of parsers to compare: gson 2.8.0, jackson 2.0.1, moshi 1.4.0, ason 1.1.0.

conclusion

jackson:
pros: fasters, triggers GC least, most customazible
cons: 6.7k methods (can be reduced by Proguard), API not so friendly

gson:
pros: fast enough, most popular (10k stars on github), pretty API, small
cons: relatively slow, trigger GC relatively more

There is no reasons for me to use moshi or ason based on this comparation: they are slower almost in each test.

Posted on

Is android app in foreground

https://stackoverflow.com/questions/8489993/check-android-application-is-in-foreground-or-not

[code language=”java”]
class ForegroundCheckTask extends AsyncTask<Context, Void, Boolean> {

@Override
protected Boolean doInBackground(Context… params) {
final Context context = params[0].getApplicationContext();
return isAppOnForeground(context);
}

private boolean isAppOnForeground(Context context) {
ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
List<RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses();
if (appProcesses == null) {
return false;
}
final String packageName = context.getPackageName();
for (RunningAppProcessInfo appProcess : appProcesses) {
if (appProcess.importance == RunningAppProcessInfo.IMPORTANCE_FOREGROUND && appProcess.processName.equals(packageName)) {
return true;
}
}
return false;
}
}

// Use like this:
boolean foregroud = new ForegroundCheckTask().execute(context).get();
[/code]