Android之读取 AndroidManifest.xml 中的数据:版本号、应用名称、自定义K-V数据(meta-data)...

AndroidManifest.xml中的定义如下:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="hpc.cai.test01"
android:versionCode="1"
android:versionName="1.0" >
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<meta-data
android:name="tel"
android:value="cdma-13366350377" >
</meta-data>
<meta-data
android:name="channel"
android:value="eben" />


<activity
android:name="hpc.cai.test.Test1Activity"
android:label="@string/app_name" >
<meta-data
android:name="name"
android:value="自强不息" >
</meta-data>
<meta-data
android:name="city"
android:value="北京" />


<intent-filter>
<action android:name="android.intent.action.MAIN" />


<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
package net.duohuo.lequ.activity.other;

import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;

/**
 * Created by ChenZhiNing on 2015/7/30.
 */
public class MetaDataHelper {
 private Activity mActivity;

    public MetaDataHelper(Activity mActivity) {
        this.mActivity = mActivity;
    }

    /**
     * activity MetaData读取
     */
    private void testActivityMetaData() {
        ActivityInfo info;
        try {
            info = mActivity.getPackageManager().getActivityInfo(mActivity.getComponentName(), PackageManager.GET_META_DATA);

            String msg = info.metaData.getString("name");
            System.out.println("name:" + msg);

            msg = info.metaData.getString("city");
            System.out.println("city:" + msg);


        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * appliction MetaData读取
     */
    private void testApplicationMetaData() {
        ApplicationInfo info;
        try {
            info = mActivity.getPackageManager().getApplicationInfo(mActivity.getPackageName(), PackageManager.GET_META_DATA);

            String msg = info.metaData.getString("tel");
            System.out.println("tel:" + msg);

            msg = info.metaData.getString("channel");
            System.out.println("channel:" + msg);


        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 测试版本信息
     */
    private void getVersion() {
        PackageInfo pkg;
        try {
            pkg = mActivity.getPackageManager().getPackageInfo(mActivity.getApplication().getPackageName(), 0);
            String appName = pkg.applicationInfo.loadLabel(mActivity.getPackageManager()).toString();
            String versionName = pkg.versionName;
            System.out.println("appName:" + appName);
            System.out.println("versionName:" + versionName);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
    }
}

 

转载于:https://www.cnblogs.com/niray/p/4688970.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值