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:it.telecomitalia.my.base_struct_apps.VersionUpdate.java

public VersionUpdate(Activity activity) {
    super();/*from   w  w w .  jav a  2  s . c  om*/
    /* http://developer.android.com/tools/publishing/versioning.html
     * si comincia dal capire che versione sto utilizzando. Nella documentazione sopra
     * elencata, leggersi tutto e fare attenzione alla differenza tra Version Name e
     * Version Code. Con questo metodo mi leggo il versionName e lo salvo per
     * ulteriori elaborazioni. In caso qualcosa non funzionasse ritorno null */
    try {
        PackageManager manager = activity.getPackageManager();
        PackageInfo info = manager.getPackageInfo(activity.getPackageName(), 0);
        runningVersion = info.versionName;
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
        runningVersion = null;
    }
}

From source file:com.rareloop.cordova.appversion.RareloopAppVersion.java

/**
 * Executes the request and returns PluginResult
 *
 * @param  action          //from  w w w .  j ava  2 s . c  o  m
 * @param  args            
 * @param  callbackContext 
 * @return boolean                
 * @throws JSONException   
 */
@Override
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {

    /**
     * appVersion
     */
    if (action.equals("getAppVersion")) {

        try {
            PackageManager packageManager = this.cordova.getActivity().getPackageManager();

            JSONObject r = new JSONObject();
            r.put("version",
                    packageManager.getPackageInfo(this.cordova.getActivity().getPackageName(), 0).versionName);
            r.put("build",
                    packageManager.getPackageInfo(this.cordova.getActivity().getPackageName(), 0).versionCode);

            callbackContext.success(r);
        } catch (NameNotFoundException e) {
            callbackContext.error("Exception thrown");
        }

        return true;
    }

    // Default response to say the action hasn't been handled
    return false;
}

From source file:io.github.pwlin.cordova.plugins.fileopener2.FileOpener2.java

private boolean _appIsInstalled(String packageId) {
    PackageManager pm = cordova.getActivity().getPackageManager();
    boolean appInstalled = false;
    try {//w  w  w  .  j av  a  2  s  .c  om
        pm.getPackageInfo(packageId, PackageManager.GET_ACTIVITIES);
        appInstalled = true;
    } catch (PackageManager.NameNotFoundException e) {
        appInstalled = false;
    }
    return appInstalled;
}

From source file:cheng.app.cnbeta.AboutFragment.java

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    PackageManager pm = getActivity().getPackageManager();
    String packageName = getActivity().getPackageName();
    String versionName;//from w  w  w . j  a  va2 s.com
    try {
        PackageInfo info = pm.getPackageInfo(packageName, 0);
        versionName = info.versionName;
    } catch (NameNotFoundException e) {
        versionName = VERSION_NA;
    }

    LayoutInflater layoutInflater = LayoutInflater.from(getActivity());
    View v = layoutInflater.inflate(R.layout.dialog_about, null);
    TextView versionText = (TextView) v.findViewById(R.id.dialog_about_version);
    LinearLayout author = (LinearLayout) v.findViewById(R.id.author);
    versionText.setText(versionName);
    author.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent i = new Intent(Intent.ACTION_VIEW,
                    Uri.parse("https://plus.google.com/u/0/114474183775310756510"));
            startActivity(i);
        }
    });
    TextView github = (TextView) v.findViewById(R.id.github);
    github.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse("https://github.com/cr1944/CBReader2"));
            startActivity(i);
        }
    });
    TextView opensource = (TextView) v.findViewById(R.id.opensource);
    opensource.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            HelpUtils.showOpenSourceLicenses(getActivity());
        }
    });

    return new AlertDialog.Builder(getActivity()).setView(v)
            .setPositiveButton(android.R.string.cancel, new OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();

                }
            }).create();
}

From source file:com.lillicoder.newsblurry.net.BaseTask.java

/**
 * Gets the user agent string for use with tasks that make web requests.
 * By default this returns the application name and version code.
 * @return User agent string for use with web requests.
 *//*from   www .  jav a  2s  .  co  m*/
protected String getUserAgent() {
    Context context = this.getContext();
    Assert.assertTrue(context != null);

    String versionName = context.getString(R.string.default_versionName);
    try {
        PackageManager manager = context.getPackageManager();
        PackageInfo info = manager.getPackageInfo(context.getPackageName(), 0);
        versionName = info.versionName;
    } catch (NameNotFoundException e) {
        Log.w(TAG, String.format(WARNING_FAILED_TO_GET_PACKAGE_INFO, context.getPackageName()));
    }

    String userAgentFormat = context.getString(R.string.format_userAgent);
    String appName = context.getString(R.string.app_name);
    return String.format(userAgentFormat, appName, versionName);
}

From source file:io.ionic.links.IonicDeeplink.java

/**
 * Check if we can open an app with a given URI scheme.
 *
 * Thanks to https://github.com/ohh2ahh/AppAvailability/blob/master/src/android/AppAvailability.java
 */// ww  w. ja  va 2 s .c  o m
private void canOpenApp(String uri, final CallbackContext callbackContext) {
    Context ctx = this.cordova.getActivity().getApplicationContext();
    final PackageManager pm = ctx.getPackageManager();

    try {
        pm.getPackageInfo(uri, PackageManager.GET_ACTIVITIES);
        callbackContext.success();
    } catch (PackageManager.NameNotFoundException e) {
    }

    callbackContext.error("");
}

From source file:org.aschyiel.nextboat.Downloader.java

/**
  * Make sure the user-agent represents us as an android device.
  *//*w ww  .  j a  v a 2 s  .  com*/
  private void _prepareUserAgent(Context context) {
      try {
          // Read the package name and version number from the manifest.
          PackageManager manager = context.getPackageManager();
          PackageInfo info = manager.getPackageInfo(context.getPackageName(), 0);
          sUserAgent = String.format(context.getString(R.string.template_user_agent), info.packageName,
                  info.versionName);
      } catch (NameNotFoundException e) {
          Log.e(TAG, "Couldn't find package information in PackageManager", e);
      }
  }

From source file:com.facebook.stetho.inspector.ChromeDiscoveryHandler.java

private String getAppLabelAndVersion() {
    StringBuilder b = new StringBuilder();
    PackageManager pm = mContext.getPackageManager();
    b.append(getAppLabel());/*from  ww  w.j  av a2  s.c  o  m*/
    b.append('/');
    try {
        PackageInfo info = pm.getPackageInfo(mContext.getPackageName(), 0 /* flags */);
        b.append(info.versionName);
    } catch (PackageManager.NameNotFoundException e) {
        throw new RuntimeException(e);
    }
    return b.toString();
}

From source file:com.andrada.sitracker.ui.fragment.AboutDialog.java

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    PackageManager pm = getActivity().getPackageManager();
    String packageName = getActivity().getPackageName();
    String versionName;//from  www . ja  v a 2 s .  c o  m
    try {
        PackageInfo info = pm.getPackageInfo(packageName, 0);
        versionName = info.versionName;
    } catch (PackageManager.NameNotFoundException e) {
        versionName = VERSION_UNAVAILABLE;
    }

    SpannableStringBuilder aboutBody = new SpannableStringBuilder();
    SpannableString licensesLink = new SpannableString(getString(R.string.about_licenses));
    licensesLink.setSpan(new ClickableSpan() {
        @Override
        public void onClick(View view) {
            showOpenSourceLicenses(getActivity());
        }
    }, 0, licensesLink.length(), 0);
    SpannableString whatsNewLink = new SpannableString(getString(R.string.whats_new));
    whatsNewLink.setSpan(new ClickableSpan() {
        @Override
        public void onClick(View view) {
            showWhatsNew(getActivity());
        }
    }, 0, whatsNewLink.length(), 0);
    aboutBody.append(licensesLink);
    aboutBody.append("\n\n");
    aboutBody.append(whatsNewLink);

    AboutDialogView aboutBodyView = AboutDialogView_.build(getActivity());
    aboutBodyView.bindData(getString(R.string.app_version_format, versionName), aboutBody);

    return new AlertDialog.Builder(getActivity()).setTitle(R.string.action_about).setView(aboutBodyView)
            .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                    dialog.dismiss();
                }
            }).create();
}

From source file:com.tdispatch.passenger.core.TDApplication.java

protected void initPackageInfo() {

    try {/*from  ww  w . j a v  a2  s  .  co  m*/
        // read current version information about this package
        PackageManager manager = mAppContext.getPackageManager();
        mPackageInfo = manager.getPackageInfo(mAppContext.getPackageName(), 0);

        mAppVersion = "v" + mPackageInfo.versionName;
        mAppVersionCode = mPackageInfo.versionCode;
        mAppName = mPackageInfo.applicationInfo.name;
    } catch (Exception e) {
        WebnetLog.e("Failed to get packageInfo");
    }
}