Example usage for android.content Context getApplicationContext

List of usage examples for android.content Context getApplicationContext

Introduction

In this page you can find the example usage for android.content Context getApplicationContext.

Prototype

public abstract Context getApplicationContext();

Source Link

Document

Return the context of the single, global Application object of the current process.

Usage

From source file:Main.java

/**
 * @param context/*from   w w w.ja  v  a  2s  .  c o  m*/
 * @return KeyHash
 * follow facebook developers link to get release key hash
 * https://developers.facebook.com/docs/android/getting-started#release-key-hash
 */
public static String getKeyHash(Context context) {
    PackageInfo packageInfo;
    String key = null;
    try {
        packageInfo = context.getPackageManager().getPackageInfo(
                context.getApplicationContext().getPackageName(), PackageManager.GET_SIGNATURES);
        for (Signature signature : packageInfo.signatures) {
            MessageDigest md = MessageDigest.getInstance("SHA");
            md.update(signature.toByteArray());
            Log.d("KeyHash:", Base64.encodeToString(md.digest(), Base64.DEFAULT));
        }
    } catch (PackageManager.NameNotFoundException e1) {
    } catch (NoSuchAlgorithmException e) {
    } catch (Exception e) {
    }
    return key;
}

From source file:Main.java

public static void unRegisterBroadcastReceiver(Context context, BroadcastReceiver broadcastReceiver) {
    if (context != null && broadcastReceiver != null) {
        if (mLocalBroadcastManager == null) {
            getLocalBroadcastManager(context.getApplicationContext());
        }/*from   w ww .j a v  a 2  s.c o  m*/
        mLocalBroadcastManager.unregisterReceiver(broadcastReceiver);
    }
}

From source file:Main.java

/**
 * Removes all cookies for this application.
 *//*  w  w w.j  a v  a 2 s . co  m*/
public static void removeAllCookies(Context context) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        removeAllCookiesV21();
    } else {
        removeAllCookiesV14(context.getApplicationContext());
    }
}

From source file:ee.ria.DigiDoc.util.FileUtils.java

private static File getCacheDir(Context context) {
    return context.getApplicationContext().getCacheDir();
}

From source file:ee.ria.DigiDoc.util.FileUtils.java

private static File getFilesDir(Context context) {
    return context.getApplicationContext().getFilesDir();
}

From source file:Main.java

/**
 * Return file size from Uri//from w  w  w .  j ava  2s . c o  m
 *
 * @param uri file URI
 * @return return file size
 */
public static long getFileSizeFromUri(Context context, Uri uri) {
    long size = 0;
    if (uri.getScheme().toString().compareTo("content") == 0) {
        Cursor cursor = null;
        try {
            cursor = context.getApplicationContext().getContentResolver().query(uri, null, null, null, null);
            if (cursor != null && cursor.moveToFirst()) {
                final int column_index = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.SIZE);
                size = cursor.getInt(column_index);
            }
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
    } else if (uri.getScheme().toString().compareTo("file") == 0) {
        final File file = new File(uri.getPath());
        size = file.length();
    }
    return size;
}

From source file:Main.java

private static void startActivityByResolve(Context context, ResolveInfo r, Intent intent) {
    Intent intent2 = new Intent(intent);
    intent2.setClassName(r.activityInfo.packageName, r.activityInfo.name);
    intent2.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.getApplicationContext().startActivity(intent2);
}

From source file:Main.java

public static boolean areNotificationsEnabled(Context context) {
    AppOpsManager appOps = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);
    ApplicationInfo appInfo = context.getApplicationInfo();
    String pkg = context.getApplicationContext().getPackageName();
    int uid = appInfo.uid;
    try {// w  ww .j ava2 s  .  c  o  m
        Class<?> appOpsClass = Class.forName(AppOpsManager.class.getName());
        Method checkOpNoThrowMethod = appOpsClass.getMethod(CHECK_OP_NO_THROW, Integer.TYPE, Integer.TYPE,
                String.class);
        Field opPostNotificationValue = appOpsClass.getDeclaredField(OP_POST_NOTIFICATION);
        int value = (Integer) opPostNotificationValue.get(Integer.class);
        return ((Integer) checkOpNoThrowMethod.invoke(appOps, value, uid, pkg) == AppOpsManager.MODE_ALLOWED);
    } catch (ClassNotFoundException | NoSuchMethodException | NoSuchFieldException | InvocationTargetException
            | IllegalAccessException | RuntimeException e) {
        return true;
    }
}

From source file:com.pursuer.reader.easyrss.Utils.java

public static void initManagers(final Context context) {
    if (DataMgr.getInstance() == null) {
        DataMgr.init(context.getApplicationContext());
    }/*from   ww  w.j a v a  2  s .c  om*/
    if (NetworkMgr.getInstance() == null) {
        NetworkMgr.init(context.getApplicationContext());
    }
    if (ReaderAccountMgr.getInstance() == null) {
        ReaderAccountMgr.init(context.getApplicationContext());
    }
    if (NotificationMgr.getInstance() == null) {
        NotificationMgr.init(context.getApplicationContext());
    }
    if (GoogleAnalyticsMgr.getInstance() == null) {
        GoogleAnalyticsMgr.init(context.getApplicationContext());
        GoogleAnalyticsMgr.getInstance().startTracking();
    }
}

From source file:Main.java

/**
 * Return file display name from Uri//from www . ja v a  2s .c om
 *
 * @param context Context
 * @param uri URI of the file
 * @return return file display name
 */
public static String getFileDisplayNameFromUri(Context context, Uri uri) {
    String displayName = "";
    if (uri.getScheme().toString().compareTo("content") == 0) {
        Cursor cursor = null;
        try {
            cursor = context.getApplicationContext().getContentResolver().query(uri, null, null, null, null);
            if (cursor != null && cursor.moveToFirst()) {
                final int column_index = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DISPLAY_NAME);
                displayName = cursor.getString(column_index);
            }
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
    } else if (uri.getScheme().toString().compareTo("file") == 0) {
        final File file = new File(uri.getPath());
        displayName = file.getName();
    }
    return displayName;
}