Several useful packageutils gadgets in Android

Source: Internet
Author: User

1. Get the Meta-data value of the specified key in application

    publicstaticgetApplicationMetadata(Context context,String metaDataKey) {        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();        }        returnnull;    }

Application scenario: You need to set a different channel ID for each APK when the program is packaged online to a different application market, and check that the log information is correct after packaging.
For example:

  <meta-data            android:name="UMENG_CHANNEL"            android:value="Offline" />

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

  publicstaticgetPackageName(Context context){        return context.getPackageName();    }
3、获取版本名称
     publicstaticgetVersionName(Context context){        try {            PackageManager manager = context.getPackageManager();            0);            String version = info.versionName;            return version;        catch (Exception e) {            e.printStackTrace();        }        return"1.0";    }
4、得到版本号
        publicstaticintgetVersionCode(Context context){        try {            PackageManager manager = context.getPackageManager();            0);            int version = info.versionCode;            return version;        catch (Exception e) {            e.printStackTrace();        }        return1;    }
这些信息只是为了检查程序中的设置是否真确不是每次都需要的,所以通常情况下把log信息在进入应用的引导动画所在的activity中打印出来就好了。

Several useful packageutils gadgets in Android

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.