Example usage for android.content Context getPackageManager

List of usage examples for android.content Context getPackageManager

Introduction

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

Prototype

public abstract PackageManager getPackageManager();

Source Link

Document

Return PackageManager instance to find global package information.

Usage

From source file:Main.java

/**
 * Utility method to check if a shortcut can be added to the homescreen.
 * @param context Context used to get the package manager.
 * @return if a shortcut can be added to the homescreen under the current profile.
 *//* w ww. ja  va  2  s.  c o m*/
public static boolean isAddToHomeIntentSupported(Context context) {
    PackageManager pm = context.getPackageManager();
    Intent i = new Intent(INSTALL_SHORTCUT);
    List<ResolveInfo> receivers = pm.queryBroadcastReceivers(i, PackageManager.GET_INTENT_FILTERS);
    return !receivers.isEmpty();
}

From source file:Main.java

public static int getAppVersionCode(Context context) {
    PackageManager pm = context.getPackageManager();
    PackageInfo pi;/*from   w ww .j  a v a  2  s. com*/
    try {
        pi = pm.getPackageInfo(context.getPackageName(), 0);
        return pi.versionCode;
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
    }
    return -1;
}

From source file:Main.java

/**
 * Restart the app./*w  w  w  . j a  v  a 2s .  c o  m*/
 * @param context
 */
public static void restartApplication(Context context) {
    Intent i = context.getPackageManager().getLaunchIntentForPackage(context.getPackageName());
    i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

    PendingIntent p = PendingIntent.getActivity(context, 0, i, PendingIntent.FLAG_CANCEL_CURRENT);
    AlarmManager mgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 500, p);

    //kill the application
    System.exit(0);
}

From source file:Main.java

public static void launchApplication(Context context, String pkgname) {
    Intent intent = context.getPackageManager().getLaunchIntentForPackage(pkgname);
    intent.setFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT | Intent.FLAG_ACTIVITY_NEW_TASK);

    context.startActivity(intent);/*ww w  .ja va  2s .  co  m*/
}

From source file:Main.java

public static boolean checkPackage(Context context, String p) {
    try {//from  ww w  . jav a2  s  .  c o  m
        context.getPackageManager().getApplicationInfo(p, PackageManager.GET_UNINSTALLED_PACKAGES);
        return true;
    } catch (PackageManager.NameNotFoundException e) {
        return false;
    }
}

From source file:Main.java

public static void DEBUG(Context c) {
    try {/*from  w w  w  .j  a  v a2s .c  om*/
        debug = (c.getPackageManager().getApplicationInfo(c.getPackageName(), 0).flags
                & ApplicationInfo.FLAG_DEBUGGABLE) != 0;
    } catch (NameNotFoundException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static String getPackageName(Context contex) {
    PackageManager manager = contex.getPackageManager();
    try {/*from   w ww . j av a  2  s . c  om*/
        PackageInfo info = manager.getPackageInfo(contex.getPackageName(), 0);
        return info.packageName;
    } catch (NameNotFoundException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

protected static boolean isAvailable(Intent intent, Context context) {
    List<ResolveInfo> infos = context.getPackageManager().queryIntentActivities(intent,
            PackageManager.MATCH_DEFAULT_ONLY);
    if (infos.size() > 0)
        return true;
    else//  www  .j  a va2 s  .co m
        return false;
}

From source file:Main.java

/**
 * Returns version name of OpenSudoku.//from   ww  w.  j a  va 2s .c  o  m
 *
 * @return
 */
public static String getAppVersionName(Context context) {
    try {
        return context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionName;
    } catch (NameNotFoundException e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

/**
 * Returns version code of OpenSudoku.// w  ww .java  2s.  c o  m
 *
 * @return
 */
public static int getAppVersionCode(Context context) {
    try {
        return context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionCode;
    } catch (NameNotFoundException e) {
        throw new RuntimeException(e);
    }
}