Android Utililty Methods Context Check

List of utility methods to do Context Check

Description

The list of methods to do Context Check are organized into topic(s).

Method

booleanisAppInstalled(Context context)
is App Installed
PackageManager manager = context.getPackageManager();
try {
    manager.getPackageInfo(PACKAGE_NAME, 0);
    return true;
} catch (NameNotFoundException e) {
    return false;
booleanisAppInstalled(Context context, String uri)
Check if package installed
PackageManager pm = context.getPackageManager();
boolean appInstalled;
try {
    assert pm != null;
    pm.getPackageInfo(uri, PackageManager.GET_ACTIVITIES);
    appInstalled = true;
} catch (PackageManager.NameNotFoundException e) {
    appInstalled = false;
...
booleanisAppOnForeground(Context context)
is App On Foreground
ActivityManager activityManager = (ActivityManager) context
        .getSystemService(Context.ACTIVITY_SERVICE);
List<RunningAppProcessInfo> appProcesses = activityManager
        .getRunningAppProcesses();
if (appProcesses == null)
    return false;
for (RunningAppProcessInfo appProcess : appProcesses) {
    if (appProcess.processName.equals(context.getPackageName())
...
booleanisApplicationBroughtToBackground( final Context context)
Checks if the application is in the background (i.e behind another application's Activity).
if (context
        .checkCallingOrSelfPermission(Manifest.permission.GET_TASKS) == PackageManager.PERMISSION_DENIED)
    return false;
ActivityManager am = (ActivityManager) context
        .getSystemService(Context.ACTIVITY_SERVICE);
List<RunningTaskInfo> tasks = am.getRunningTasks(1);
if (!tasks.isEmpty()) {
    ComponentName topActivity = tasks.get(0).topActivity;
...
booleanisApplicationBroughtToBackground( final Context context)
Checks if the application is in the background (i.e behind another application's Activity).
ActivityManager am = (ActivityManager) context
        .getSystemService(Context.ACTIVITY_SERVICE);
List<RunningTaskInfo> tasks = am.getRunningTasks(1);
if (!tasks.isEmpty()) {
    ComponentName topActivity = tasks.get(0).topActivity;
    if (!topActivity.getPackageName().equals(
            context.getPackageName())) {
        return true;
...
booleanisCallable(Context context, String url)
is Callable
String mimeTypeExtension = URLConnection
        .guessContentTypeFromName(url);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse(url), mimeTypeExtension);
List<ResolveInfo> list = context.getPackageManager()
        .queryIntentActivities(intent,
                PackageManager.MATCH_DEFAULT_ONLY);
return list.size() > 0;
...
booleanisConnected(Context connext)
is Connected
ConnectivityManager connectivityManager = (ConnectivityManager) connext
        .getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo info = connectivityManager.getActiveNetworkInfo();
if (info != null && info.isConnected()) {
    return true;
return false;
booleanisConnected(Context context)
is Connected
ConnectivityManager conn = (ConnectivityManager) context
        .getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo info = conn.getActiveNetworkInfo();
return (info != null && info.isConnected());
booleanisDebugCertificateCheck(final Context context)
is Debug Certificate Check
final X500Principal DEBUG_CERTIFICATE_DN = new X500Principal(
        "CN=Android Debug,O=Android,C=US");
boolean debuggable = false;
try {
    Signature[] signatures = getSignatures(context);
    for (int i = 0; i < signatures.length; i++) {
        X509Certificate certificate = generateX509CertificateFromSignature(signatures[i]);
        debuggable = certificate.getSubjectX500Principal().equals(
...
booleanisDebuggable(final Context context, final boolean includeDefaultDebugCertificateCheck)
Check whether the application is debuggable.
boolean debuggable = (0 != (context.getApplicationInfo().flags &= ApplicationInfo.FLAG_DEBUGGABLE));
if (!debuggable && includeDefaultDebugCertificateCheck) {
    debuggable = isDebugCertificateCheck(context);
return debuggable;