android gradle里面怎么获取manifest的meta-data值
答案:1 悬赏:0 手机版
解决时间 2021-02-11 03:23
- 提问者网友:泪痣哥哥
- 2021-02-10 20:38
android gradle里面怎么获取manifest的meta-data值
最佳答案
- 五星知识达人网友:梦中风几里
- 2021-02-10 22:09
如以下配置内容:
[html] view plain copy
<meta-data android:name="api_key" android:value="@string/api_key" />
指定的api_key值为存储在资源文件string中的api_key值 如:AIzaSyBhBFOgVQclaa8p1JJeqaZHiCo2nfiyBBo
如以下配置内容:
[html] view plain copy
<meta-data android:name="resId" android:resource="@string/res_id" />
指定的resId值则是为res_id的资源id号 而不是string中的res_id值
二、如何获取<mate-data...>元素配置的值:
1、在<application...>元素下配置<mate-data...>元素
xml代码段:
[html] view plain copy
<application...>
.....
<meta-data
android:name="api_key"
android:value="AIzaSyBhBFOgVQclaa8p1JJeqaZHiCo2nfiyBBo" />
</application>
Java代码段:
[java] view plain copy
try {
ApplicationInfo appInfo = getPackageManager().getApplicationInfo(getPackageName(),
PackageManager.GET_META_DATA);
String value = appInfo.metaData.getString("api_key");
Log.d("Tag", " app key : " + value); // Tag﹕ app key : AIzaSyBhBFOgVQclaa8p1JJeqaZHiCo2nfiyBBo
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
2、在<activity...>元素下配置<mate-data...>元素
xml代码段:
[html] view plain copy
<activity ...>
.....
<meta-data android:name="resource_id"
android:resource="@string/ice" />
</activity>
Java代码段:
[java] view plain copy
try {
ActivityInfo activityInfo = getPackageManager().getActivityInfo(getComponentName(),
PackageManager.GET_META_DATA);
// 获取到的是 @string/ice 对应的资源id值
int value = activityInfo.metaData.getInt("resource_id");
Log.d("Activity Tag", "resource_id : " + value); // Activity Tag﹕ resource_id : 2131361808
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
3、在<service...>元素下配置<mate-data...>元素
xml代码段:
[html] view plain copy
<service android:name="MetaDataService">
.....
<meta-data android:name="service_meta_data" android:value="xxxxxxx" />
</service>
Java代码段:
[java] view plain copy
try {
ComponentName cn=new ComponentName(this, MetaDataService.class);
ServiceInfo info=this.getPackageManager()
.getServiceInfo(cn, PackageManager.GET_META_DATA);
String value = info.metaData.getString("service_meta_data");
Log.d("Service TAG", " value == " + value);
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
4、在<receiver...>元素下配置<mate-data...>元素
xml代码段:
[html] view plain copy
<receiver android:name="MetaDataReceiver">
.....
<meta-data android:name="receiver_meta_data" android:value="xxxxxxx" />
</receiver>
Java代码段:
[java] view plain copy
try {
ComponentName cn=new ComponentName(this, MetaDataReceiver.class);
ActivityInfo info=context.getPackageManager()
.getReceiverInfo(cn, PackageManager.GET_META_DATA);
String value = info.metaData.getString("receiver_meta_data");
Log.d("Receiver TAG", " value == " + value);
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
[html] view plain copy
<meta-data android:name="api_key" android:value="@string/api_key" />
指定的api_key值为存储在资源文件string中的api_key值 如:AIzaSyBhBFOgVQclaa8p1JJeqaZHiCo2nfiyBBo
如以下配置内容:
[html] view plain copy
<meta-data android:name="resId" android:resource="@string/res_id" />
指定的resId值则是为res_id的资源id号 而不是string中的res_id值
二、如何获取<mate-data...>元素配置的值:
1、在<application...>元素下配置<mate-data...>元素
xml代码段:
[html] view plain copy
<application...>
.....
<meta-data
android:name="api_key"
android:value="AIzaSyBhBFOgVQclaa8p1JJeqaZHiCo2nfiyBBo" />
</application>
Java代码段:
[java] view plain copy
try {
ApplicationInfo appInfo = getPackageManager().getApplicationInfo(getPackageName(),
PackageManager.GET_META_DATA);
String value = appInfo.metaData.getString("api_key");
Log.d("Tag", " app key : " + value); // Tag﹕ app key : AIzaSyBhBFOgVQclaa8p1JJeqaZHiCo2nfiyBBo
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
2、在<activity...>元素下配置<mate-data...>元素
xml代码段:
[html] view plain copy
<activity ...>
.....
<meta-data android:name="resource_id"
android:resource="@string/ice" />
</activity>
Java代码段:
[java] view plain copy
try {
ActivityInfo activityInfo = getPackageManager().getActivityInfo(getComponentName(),
PackageManager.GET_META_DATA);
// 获取到的是 @string/ice 对应的资源id值
int value = activityInfo.metaData.getInt("resource_id");
Log.d("Activity Tag", "resource_id : " + value); // Activity Tag﹕ resource_id : 2131361808
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
3、在<service...>元素下配置<mate-data...>元素
xml代码段:
[html] view plain copy
<service android:name="MetaDataService">
.....
<meta-data android:name="service_meta_data" android:value="xxxxxxx" />
</service>
Java代码段:
[java] view plain copy
try {
ComponentName cn=new ComponentName(this, MetaDataService.class);
ServiceInfo info=this.getPackageManager()
.getServiceInfo(cn, PackageManager.GET_META_DATA);
String value = info.metaData.getString("service_meta_data");
Log.d("Service TAG", " value == " + value);
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
4、在<receiver...>元素下配置<mate-data...>元素
xml代码段:
[html] view plain copy
<receiver android:name="MetaDataReceiver">
.....
<meta-data android:name="receiver_meta_data" android:value="xxxxxxx" />
</receiver>
Java代码段:
[java] view plain copy
try {
ComponentName cn=new ComponentName(this, MetaDataReceiver.class);
ActivityInfo info=context.getPackageManager()
.getReceiverInfo(cn, PackageManager.GET_META_DATA);
String value = info.metaData.getString("receiver_meta_data");
Log.d("Receiver TAG", " value == " + value);
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯