Android-->AndroidManifest.xml 文件 使用方法

官方介绍: http://android.xsoftlab.net/guide/topics/manifest/meta-data-element.html

使用方法:
1:首先,需要在AndroidManifest.xml文件中声明,如下:





    //注意此处
    
        //注意此处
        
            
            
        
    

    
         //注意此处
    

    
        //注意此处
    




2:在代码中获取对应的值

private void init() {
    TextView textView = (TextView) findViewById(R.id.text);
    StringBuilder stringBuilder = new StringBuilder();
    stringBuilder.append("app data:");
    stringBuilder.append(getAppMeta());
    stringBuilder.append("\n");

    stringBuilder.append("act data:");
    stringBuilder.append(getActMeta());
    stringBuilder.append("\n");

    stringBuilder.append("ser data:");
    stringBuilder.append(getSerMeta());
    stringBuilder.append("\n");

    stringBuilder.append("rec data:");
    stringBuilder.append(getRecMeta());
    stringBuilder.append("\n");

    textView.setText(stringBuilder.toString());
}

private String getAppMeta() {//app 组件中的meta-data
    try {
        ApplicationInfo info = this.getPackageManager().getApplicationInfo(this.getPackageName(), PackageManager.GET_META_DATA);
        return getString(info.metaData.getInt(KEY_DATA));//如果是通过 resource 设置的值,需要通过id访问数据
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
    }
    return "";
}


private String getSerMeta() {//service 组件中的meta-data
    try {
        ServiceInfo info = this.getPackageManager().getServiceInfo(new ComponentName(this, TestService.class), PackageManager.GET_META_DATA);
        return info.metaData.getString(KEY_DATA);
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
    }
    return "";
}


private String getRecMeta() {//receiver 组件中的meta-data
    try {
        ActivityInfo info = this.getPackageManager().getReceiverInfo(new ComponentName(this, TestReceiver.class), PackageManager.GET_META_DATA);
        return getString(info.metaData.getInt(KEY_DATA));
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
    }
    return "";
}


private String getActMeta() {//activity 组件中的meta-data
    try {
        ActivityInfo info = this.getPackageManager().getActivityInfo(new ComponentName(this, MainActivity.class), PackageManager.GET_META_DATA);
        return info.metaData.getString(KEY_DATA);
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
    }
    return "";
}

项目地址:https://github.com/angcyo/MetaDataDemo


至此: 文章就结束了,如有疑问: QQ群:274306954 欢迎您的加入.

你可能感兴趣的:(Android-->AndroidManifest.xml 文件 使用方法)