Example usage for android.content.pm PackageManager GET_META_DATA

List of usage examples for android.content.pm PackageManager GET_META_DATA

Introduction

In this page you can find the example usage for android.content.pm PackageManager GET_META_DATA.

Prototype

int GET_META_DATA

To view the source code for android.content.pm PackageManager GET_META_DATA.

Click Source Link

Document

ComponentInfo flag: return the ComponentInfo#metaData data android.os.Bundle s that are associated with a component.

Usage

From source file:Main.java

public static String getServiceMetaData(Context context, Class<?> serviceClass, String key) {
    String data = null;//  ww w .  java2  s.  com
    ServiceInfo info = null;
    try {
        info = context.getPackageManager().getServiceInfo(new ComponentName(context, serviceClass),
                PackageManager.GET_META_DATA);
    } catch (NameNotFoundException e) {
        e.printStackTrace();
    }
    if (info != null) {
        data = info.metaData.get(key).toString();
    }
    return data;
}

From source file:Main.java

public static void send(Context context, String path) {

    Intent intent = new Intent();
    intent.setAction(Intent.ACTION_SEND);
    PackageManager pm = context.getPackageManager();
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    intent.setType("*/*");
    intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File(path)));
    List<ResolveInfo> list = pm.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
    boolean flag = false;
    for (ResolveInfo info : list) {
        if (info.activityInfo.packageName.toLowerCase().contains("bluetooth")
                || info.activityInfo.name.toLowerCase().contains("bluetooth")) {
            ApplicationInfo appInfo = null;
            try {
                appInfo = pm.getApplicationInfo(info.activityInfo.packageName, PackageManager.GET_META_DATA);
            } catch (PackageManager.NameNotFoundException e) {

            }/*  w  ww.  ja va2s .c  o m*/
            if (appInfo != null && (appInfo.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) == 0
                    && (appInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
                intent.setClassName(info.activityInfo.packageName, info.activityInfo.name);
                flag = true;
                break;
            }
        }
    }
    if (!flag) {
        return;
    }
    context.startActivity(intent);
}

From source file:Main.java

public static String getReceiverMetaData(Context context, Class<?> receiverClass, String key) {
    String data = null;/*  www.  ja v a 2s  .  com*/
    ActivityInfo info = null;
    try {
        info = context.getPackageManager().getReceiverInfo(new ComponentName(context, receiverClass),
                PackageManager.GET_META_DATA);
    } catch (NameNotFoundException e) {
        e.printStackTrace();
    }
    if (info != null) {
        data = info.metaData.get(key).toString();
    }
    return data;
}

From source file:Main.java

public static Bitmap packageNameToBitmap(Context context, PackageManager packageManager, String packageName,
        int resId) {
    try {/*w  w w  .  j a v a  2 s  .  c  o m*/
        ApplicationInfo applicationInfo = packageManager.getApplicationInfo(packageName,
                PackageManager.GET_META_DATA);
        Resources resources = packageManager.getResourcesForApplication(applicationInfo);
        Bitmap bitmap = resIdToBitmap(resources, resId);
        if (bitmap == null) {
            Drawable drawable = packageManager.getApplicationIcon(packageName);
            if (drawable != null) {
                bitmap = drawableToBitmap(drawable);
            }
        }
        return bitmap;
    } catch (PackageManager.NameNotFoundException | Resources.NotFoundException e) {
        e.printStackTrace();
        return null;
    }
}

From source file:Main.java

/**
 * Check for the Llama app./*w w w  .  j ava 2 s  . c om*/
 * @param c
 * @return
 */
public static boolean isLlamaInstalled(Context c) {
    boolean res = false;
    PackageManager pm = c.getPackageManager();
    for (ApplicationInfo ai : pm.getInstalledApplications(PackageManager.GET_META_DATA)) {
        if (ai.packageName.contains("com.kebab.Llama")) {
            res = true;
            break;
        }
    }
    return res;
}

From source file:Main.java

/**
 * Get application meta-data of a package name.
 * @param context application context./* w  ww  .  java  2 s.  com*/
 * @param packageName package name to get meta-data.
 * @return meta-data, may be empty but never null.
 */
public static Bundle getMetaData(Context context, String packageName) {
    Bundle config;
    try {
        config = context.getPackageManager().getApplicationInfo(packageName,
                PackageManager.GET_META_DATA).metaData;
        if (config == null)
            config = new Bundle();
    } catch (Exception e) {
        /*
         * NameNotFoundException or in some rare scenario an undocumented "RuntimeException: Package
         * manager has died.", probably caused by a system app process crash.
         */
        config = new Bundle();
    }
    return config;
}

From source file:Main.java

/**
 * check to see if the Serval Mesh software is installed
 * //from  www .j ava2  s .com
 * @param context a context object used to gain access to system resources
 * @return true if the Serval Mesh software is installed
 */
public static boolean isServalMeshInstalled(Context context) {

    try {
        context.getPackageManager().getApplicationInfo(SERVAL_MESH_PACKAGE_NAME, PackageManager.GET_META_DATA);
    } catch (NameNotFoundException e) {
        return false;
    }

    return true;
}

From source file:Main.java

/**
 * Get resource ID from extra or from metadata.
 * /*w  w  w . j av  a 2 s.c om*/
 * @param context
 * @param packagename
 * @param intent
 * @param extra
 * @param metadata
 * @return
 */
public static int getResourceIdExtraOrMetadata(final Context context, final String packagename,
        final Intent intent, final String extra, final String metadata) {
    if (intent.hasExtra(extra) && intent.getStringExtra(extra) != null) {

        int id = 0;
        try {
            String resourcename = intent.getStringExtra(extra);
            Resources resources = context.getPackageManager().getResourcesForApplication(packagename);
            Log.i(TAG, "Looking up resource Id for " + resourcename);
            id = resources.getIdentifier(resourcename, "", packagename);
        } catch (NameNotFoundException e) {
            Log.e(TAG, "Package name not found", e);
        }
        return id;
    } else {
        //Try meta data of package
        Bundle md = null;
        try {
            md = context.getPackageManager().getApplicationInfo(packagename,
                    PackageManager.GET_META_DATA).metaData;
        } catch (NameNotFoundException e) {
            Log.e(TAG, "Package name not found", e);
        }

        if (md != null) {
            // Obtain resource ID and convert to resource name:
            int id = md.getInt(metadata);

            return id;
        } else {
            return 0;
        }

    }
}

From source file:Main.java

/**
 * Get application meta-data of the current package name.
 * @param context application context.//from   ww  w .  ja  va2 s  .  c o  m
 * @return meta-data, may be empty but never null.
 */
public static Bundle getMetaData(Context context) {
    Bundle config;
    try {
        config = context.getPackageManager().getApplicationInfo(context.getPackageName(),
                PackageManager.GET_META_DATA).metaData;
        if (config == null)
            config = new Bundle();
    } catch (Exception e) {
        /*
         * NameNotFoundException or in some rare scenario an undocumented "RuntimeException: Package
         * manager has died.", probably caused by a system app process crash.
         */
        config = new Bundle();
    }
    return config;
}

From source file:Main.java

public static Drawable getIconFromPackageName(String packageName, Context context) {
    PackageManager pm = context.getPackageManager();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1) {
        try {/*from w w  w  .j ava  2  s  .c  o m*/
            PackageInfo pi = pm.getPackageInfo(packageName, 0);
            Context otherAppCtx = context.createPackageContext(packageName, Context.CONTEXT_IGNORE_SECURITY);

            int displayMetrics[] = { DisplayMetrics.DENSITY_XXXHIGH, DisplayMetrics.DENSITY_XXHIGH,
                    DisplayMetrics.DENSITY_XHIGH, DisplayMetrics.DENSITY_HIGH, DisplayMetrics.DENSITY_MEDIUM,
                    DisplayMetrics.DENSITY_TV };

            for (int displayMetric : displayMetrics) {
                try {
                    Drawable d = otherAppCtx.getResources().getDrawableForDensity(pi.applicationInfo.icon,
                            displayMetric);
                    if (d != null) {

                        return d;
                    }
                } catch (Resources.NotFoundException e) {
                }
            }

        } catch (Exception e) {
            // Handle Error here
        }
    }

    ApplicationInfo appInfo = null;
    try {
        appInfo = pm.getApplicationInfo(packageName, PackageManager.GET_META_DATA);
    } catch (PackageManager.NameNotFoundException e) {
        return null;
    }

    return appInfo.loadIcon(pm);
}