Android Utililty Methods Context Get

List of utility methods to do Context Get

Description

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

Method

voidreadRaw(Context ctx, int res_id)
Method to read in a text file placed in the res/raw directory of the application.
InputStream is = ctx.getResources().openRawResource(res_id);
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr, 8192); 
try {
    String test;
    while (true) {
        test = br.readLine();
        if (test == null)
...
DrawableloadImageFromAsset(Context context, String id)
load Image From Asset
try {
    Resources resources = context.getResources();
    AssetManager am = resources.getAssets();
    InputStream ims = am.open(id + ".png");
    Drawable d = Drawable.createFromStream(ims, null);
    return d;
} catch (IOException ex) {
    return null;
...
booleanhasTelephony(@Nonnull Context context)
Checks if the device has the PackageManager#FEATURE_TELEPHONY .
final PackageManager manager = context.getPackageManager();
return manager.hasSystemFeature(PackageManager.FEATURE_TELEPHONY);
StringgetDataColumn(Context context, Uri uri, String selection, String[] selectionArgs)
Get the value of the data column for this Uri.
Cursor cursor = null;
final String column = "_data";
final String[] projection = { column };
try {
    cursor = context.getContentResolver().query(uri, projection,
            selection, selectionArgs, null);
    if (cursor != null && cursor.moveToFirst()) {
        final int column_index = cursor
...
StringgetPath(final Context context, final Uri uri)
Get a file path from a Uri.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
    if (DocumentsContract.isDocumentUri(context, uri)) {
        if (isExternalStorageDocument(uri)) {
            final String docId = DocumentsContract
                    .getDocumentId(uri);
            final String[] split = docId.split(":");
            final String type = split[0];
            if ("primary".equalsIgnoreCase(type)) {
...
StringcachedJson(Context context, URI uri)
cached Json
try {
    StringBuffer json = new StringBuffer();
    BufferedReader in = new BufferedReader(new InputStreamReader(
            context.openFileInput(String.format("%d.json",
                    uri.hashCode()))));
    String line = null;
    while ((line = in.readLine()) != null) {
        json.append(line);
...
voidcacheJson(Context context, URI uri, String jsonSource)
cache Json
try {
    PrintWriter out = new PrintWriter(context.openFileOutput(
            String.format("%d.json", uri.hashCode()),
            Context.MODE_PRIVATE));
    out.write(jsonSource);
    out.close();
} catch (IOException ex) {
    Log.e(TAG, String.format("error caching %s", uri));
...
SharedPreferencesgetSharedPreferences(Context context)
get Shared Preferences
return PreferenceManager.getDefaultSharedPreferences(context);
FilegetAndroidDBDir(Context context)
get Android DB Dir
return new File(Environment.getDataDirectory(), "//data//"
        + context.getPackageName() + "//databases//");
intgetAnimId(Context paramContext, String paramString)
get Anim Id
return paramContext.getResources().getIdentifier(paramString,
        "anim", paramContext.getPackageName());