Example usage for android.content.pm PackageManager getPackageInfo

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

Introduction

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

Prototype

public abstract PackageInfo getPackageInfo(VersionedPackage versionedPackage, @PackageInfoFlags int flags)
        throws NameNotFoundException;

Source Link

Document

Retrieve overall information about an application package that is installed on the system.

Usage

From source file:com.armtimes.dialogs.DialogAboutUs.java

@SuppressWarnings("NullableProblems")
@Override// w  w w . ja  v  a  2 s.c  o  m
public Dialog onCreateDialog(Bundle savedInstanceState) {
    View dialogView = getActivity().getLayoutInflater().inflate(R.layout.dialog_about_us, null);
    TextView tvVersion = (TextView) dialogView.findViewById(R.id.tvAboutUsVersion);
    try {
        PackageManager manager = getActivity().getPackageManager();
        PackageInfo info = manager.getPackageInfo(getActivity().getPackageName(), 0);
        tvVersion.setText(tvVersion.getText() + " " + info.versionName);

    } catch (PackageManager.NameNotFoundException e) {
        tvVersion.setVisibility(View.GONE);
    }

    // Makes the link in the text view clickable.
    TextView t2 = (TextView) dialogView.findViewById(R.id.tvAboutUsDeveloper);
    t2.setMovementMethod(LinkMovementMethod.getInstance());

    // Creates About Us Alert dialog which extends from DialogFragment.
    return new AlertDialog.Builder(getActivity()).setTitle(R.string.about_us)
            .setNeutralButton(R.string.about_us_close, null).setView(dialogView).create();
}

From source file:Main.java

public PackageInfo getPackageInfo(Context context) {
    PackageManager packageManager = context.getPackageManager();
    PackageInfo packageInfo = null;/*from www.  ja v a  2s.  c o  m*/
    try {
        packageInfo = packageManager.getPackageInfo(context.getPackageName(), 0);
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
    }
    return packageInfo;
}

From source file:com.byhook.cordova.chromelauncher.ChromeLauncher.java

private boolean isInstalled(String uri) {
    PackageManager pm = ((Activity) cordova).getPackageManager();
    try {/* w  ww .  j a v  a 2 s .  c o  m*/
        pm.getPackageInfo(uri, PackageManager.GET_ACTIVITIES);
        return true;
    } catch (PackageManager.NameNotFoundException e) {
    }
    return false;
}

From source file:com.blork.anpod.activity.HomeActivity.java

@Override
protected void onResume() {
    super.onResume();

    SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);

    try {/*from w w w . ja  va2s.  com*/
        PackageManager pm = getPackageManager();
        pm.getPackageInfo("com.blork.anpodpro", PackageManager.GET_ACTIVITIES);

        boolean pro = settings.getBoolean("pro", false);
        if (!pro) {
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle("Thank you!");
            builder.setMessage("Thanks for buying the donate version - I really appreciate it!")
                    .setCancelable(false)
                    .setNeutralButton("No Problem!", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            dialog.cancel();
                            startActivity(new Intent(HomeActivity.this, HomeActivity.class));
                            finish();
                        }
                    });
            AlertDialog alert = builder.create();
            alert.show();
        }
        SharedPreferences.Editor pEditor = settings.edit();
        pEditor.putBoolean("pro", true);
        pEditor.commit();
    } catch (NameNotFoundException e1) {
        SharedPreferences.Editor pEditor = settings.edit();
        pEditor.putBoolean("pro", false);
        pEditor.commit();
    }
}

From source file:com.bzu.yhd.pocketcampus.main.AboutActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_about);
    mUnbinder = ButterKnife.bind(this);
    initializeToolbar();// ww w  . j  a  v  a  2s  . com
    BaseApplication.getInstance().addActivity(this);

    Glide.with(this).load(R.mipmap.flowbloom).into(mHeaderImg);
    try {
        PackageManager manager = getPackageManager();
        PackageInfo info = manager.getPackageInfo(getPackageName(), 0);
        String s = String.format(Locale.CHINA, "%sBuild %d", info.versionName, info.versionCode);
        mVersionNameText.setText(s);
    } catch (Exception e) {
        e.printStackTrace();
        mVersionNameText.setText("");
    }
    if (Colorful.getThemeDelegate().isNight()) {
        Drawable drawable = mGitHubImg.getDrawable();
        if (drawable != null) {
            DrawableCompat.setTint(drawable, Color.WHITE);
            mGitHubImg.setImageDrawable(drawable);
        }
    }
    mLicenseText.setText(Html.fromHtml(getString(R.string.license)));
}

From source file:cc.metapro.openct.utils.CrashHandler.java

private void dumpPhoneInfo(PrintWriter pw) throws PackageManager.NameNotFoundException {
    PackageManager pm = mContext.getPackageManager();
    PackageInfo pi = pm.getPackageInfo(mContext.getPackageName(), PackageManager.GET_ACTIVITIES);
    pw.print("OpenCT Version: ");
    pw.print(pi.versionName);/* w  ww  . j a v  a2s.co m*/
    pw.print("_");
    pw.println(pi.versionCode);

    pw.print("OS Version: ");
    pw.print(Build.VERSION.RELEASE);
    pw.print("_");
    pw.println(Build.VERSION.SDK_INT);

    pw.print("Vendor: ");
    pw.println(Build.MANUFACTURER);

    pw.print("Model: ");
    pw.println(Build.MODEL);
}

From source file:com.aqnote.app.wifianalyzer.about.AboutActivity.java

private void setVersionNumber() {
    MainContext mainContext = MainContext.INSTANCE;
    try {//from w  w  w. ja  va2 s.  c  o  m
        PackageManager packageManager = getPackageManager();
        PackageInfo packageInfo = packageManager.getPackageInfo(getPackageName(), 0);
        String versionInfo = packageInfo.versionName;
        Configuration configuration = mainContext.getConfiguration();
        if (configuration != null && configuration.isDevelopmentMode()) {
            versionInfo += " - " + packageInfo.versionCode + " SDK:" + Build.VERSION.SDK_INT;
        }
        ((TextView) findViewById(R.id.about_version_info)).setText(versionInfo);
    } catch (PackageManager.NameNotFoundException e) {
        mainContext.getLogger().error(this, "Version Information", e);
    }
}

From source file:com.atwal.wakeup.battery.util.Utilities.java

public static boolean isPackageInstalled(Context context, String packageName) {
    PackageManager pm = context.getPackageManager();
    try {//w ww.  j  a  v a  2 s.  c  o  m
        pm.getPackageInfo(packageName, PackageManager.GET_ACTIVITIES);
        return true;
    } catch (NameNotFoundException e) {
        return false;
    }
}

From source file:br.com.hotforms.FacebookHash.java

/**
 * Executes the request and returns PluginResult.
 *
 * @param action            The action to execute.
 * @param args              JSONArry of arguments for the plugin.
 * @param callbackContext   The callback id used when calling back into JavaScript.
 * @return                  True if the action was valid, false otherwise.
 */// ww w .  ja  va 2  s.c  o  m
@Override
public boolean execute(String action, CordovaArgs args, final CallbackContext callbackContext)
        throws JSONException {
    Log.v(TAG, "Executing action: " + action);
    final Activity activity = this.cordova.getActivity();
    final Window window = activity.getWindow();

    if ("getHash".equals(action)) {
        try {
            String packageName = activity.getClass().getPackage().getName();
            PackageManager packageManager = activity.getPackageManager();
            PackageInfo info = packageManager.getPackageInfo(packageName, PackageManager.GET_SIGNATURES);
            for (Signature signature : info.signatures) {
                MessageDigest md = MessageDigest.getInstance("SHA");
                md.update(signature.toByteArray());
                String hash = Base64.encodeToString(md.digest(), Base64.DEFAULT);

                String result = String.format("{ FacebookHash : \"%s\", PackageName : \"%s\"}", hash.trim(),
                        packageName);
                callbackContext.success(result);
            }
        } catch (NameNotFoundException e) {
            callbackContext.error(e.getMessage());
        } catch (NoSuchAlgorithmException e) {
            callbackContext.error(e.getMessage());
        }
        return true;
    }

    return false;
}

From source file:com.amazonaws.mobileconnectors.pinpoint.internal.core.system.AndroidAppDetails.java

public AndroidAppDetails(Context context, String appId) {
    this.applicationContext = context.getApplicationContext();
    try {//ww w  .j  a  va2  s .  c o m
        PackageManager packageManager = this.applicationContext.getPackageManager();
        PackageInfo packageInfo = packageManager.getPackageInfo(this.applicationContext.getPackageName(), 0);
        ApplicationInfo appInfo = packageManager.getApplicationInfo(packageInfo.packageName, 0);

        appTitle = (String) packageManager.getApplicationLabel(appInfo);
        packageName = packageInfo.packageName;
        versionCode = String.valueOf(packageInfo.versionCode);
        versionName = packageInfo.versionName;
        this.appId = appId;
    } catch (NameNotFoundException e) {
        log.warn("Unable to get details for package " + this.applicationContext.getPackageName());
        appTitle = "Unknown";
        packageName = "Unknown";
        versionCode = "Unknown";
        versionName = "Unknown";
    }
}