Posts

Showing posts from May, 2016

How to check android application in background or not?

Check if application in background or not snippet public static boolean isApplicationInBackground(Context context) { ActivityManager am = (ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE); List taskList = am.getRunningTasks(1); if (taskList != null && !taskList.isEmpty()) { ComponentName topActivity = taskList.get(0).topActivity; if (topActivity != null && !topActivity.getPackageName().equals(context.getPackageName())) { return true; } } return false; } whether the application is in the background we need to define permission android.permission.GET_TASKS in Manifest.xml This function will return true if the application in the background otherwise it will return false.

Android status bar and navigation bar height

Android NavigationBar Height Snippet public static int getNavigationBarHeight() { int result = 0; int resourceId = mApplicationContent.getResources().getIdentifier("navigation_bar_height", "dimen", "android"); if (resourceId > 0) { result = mApplicationContent.getResources().getDimensionPixelSize(resourceId); } return result; } Android Phone Statusbar height snippet public static int getStatusBarHeight() { int result = 0; int resourceId = mApplicationContent.getResources().getIdentifier("status_bar_height", "dimen", "android"); if (resourceId > 0) { result = mApplicationContent.getResources().getDimensionPixelSize(resourceId); } return result; }

How to check internet connection is available or not in android

Declare Permission in manifest file <uses-permission android:name="android.permission.INTERNET"> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE">     Now Check using this function is network available or not public static boolean isNetWorkAvilable() { ConnectivityManager connectivityManager = (ConnectivityManager) mApplicationContent .getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo(); if (activeNetInfo == null || !activeNetInfo.isAvailable()) { return false; } else { return true; } }