Some practical PackageUtils gadgets in Android

Source: Internet
Author: User

Some practical PackageUtils gadgets in Android

1. Obtain the meta-data value of the specified key in the application.

    public static String getApplicationMetadata(Context context,String metaDataKey) {        ApplicationInfo info = null;        try {            PackageManager pm = context.getPackageManager();            info = pm.getApplicationInfo(context.getPackageName(), PackageManager.GET_META_DATA);            return String.valueOf(info.metaData.get(metaDataKey));        } catch (Exception e) {            e.printStackTrace();        }        return null;    }

Application Scenario: when the program is packaged and launched to different application markets, you need to set different channel IDs for each apk, and check whether the log information is correct after packaging.
For example:

  
  

Call method:PackageUtils.getApplicationMetadata(App.getInstance()
.getContext(), "UMENG_CHANNEL")
The obtained result is Offline.
2. Get the package name

  public static String getPackageName(Context context){        return context.getPackageName();    }
3. Get the version name
     public static String getVersionName(Context context){        try {            PackageManager manager = context.getPackageManager();            PackageInfo info = manager.getPackageInfo(context.getPackageName(), 0);            String version = info.versionName;            return version;        } catch (Exception e) {            e.printStackTrace();        }        return "1.0";    }
4. Get the version number.
        public static int getVersionCode(Context context){        try {            PackageManager manager = context.getPackageManager();            PackageInfo info = manager.getPackageInfo(context.getPackageName(), 0);            int version = info.versionCode;            return version;        } catch (Exception e) {            e.printStackTrace();        }        return 1;    }
This information is only required to check whether the settings in the program are really not required every time. Therefore, you can print the log information in the activity where the guiding animation of the application is located.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.