Android <meta-data/>详解

本文详细介绍了Android中如何使用meta-data元素为组件提供额外数据。包括如何在application、activity、service和receiver中配置meta-data,并提供了获取这些数据的具体代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、android meta-data 使用详解

<meta-data android:name="string"
           android:resource="resourcespecification"
           android:value="string" />

这是该元素的基本结构。在AndroidManifest.xml中,meta-data元素可以作为子元素,被包含在activity、application、service、receiver元素中,但不同的父元素,在应用时读取的方法也不同。
这个名字值是额外的任意的可以提供给父组件的数据。一个组件元素能够包含任意数量的meta-data子元素。它们所有的值都会被收集在Bundle对象中并且使其可以作为组件的 PackageItemInfo.metaData 字段。
一般的值可以通过value属性来指定,但是如果要指定一个资源id作为一个值,那么就要用resource属性来代替。例如:下面的代码就是指定存储在@string/kangaroo资源中的zoo名字。

<meta-data android:name="zoo"android:value="@string/kangaroo" />

另一方面,利用resource属性将指定zoo的资源id号,并不是存储在资源中的资源值。

<meta-data android:name="zoo" android:resource="@string/kangaroo"/>

当要给组件提供多个复杂的数据时,在这里并不推荐使用多重meta-data元素,推荐你存储这些数据在一个资源文件中并且利用resource属性来通知它的id给组件
android:name
元数据项的名字,为了保证这个名字是唯一的,采用java风格的命名规范。例如com.example.project.activity.fred
android:resource
资源的一个引用,指定给这个项的值是该资源的id。该id可以通过方法Bundle.getInt()来从meta-data中找到。
android:value
指定给这一项的值。可以作为值来指定的数据类型并且组件用来找回那些值的Bundle方法列在了下面的表中。
获得a meta-data 的值:

<meta-data 
        android:name="foo"
        android:value="@string/app_name"/>
ActivityInfo activityInfo=activity.getPackageManager().getActivityInfo(componentName, PackageManager.GET_META_DATA);   
String foo =ai.metaData.getString("foo");
Toast.makeText(this, "meta:"+foo,1).show();  

二、android meta-data在不用元素中使用
在AndroidManifest.xml中,meta-data元素可以作为子元素,被包含在activity、application、service和receiver元素中,但不同的父元素,在应用时读取的方法也不同。
1:在application中。

<application>
    <meta-data android:value="hellomy application" 
               android:name="myMsg">
    </meta-data>
</application>
ApplicationInfo appInfo = getPackageManager().getApplicationInfo(getPackageName(),PackageManager.GET_META_DATA);
String msg=appInfo.metaData.getString("myMsg");
System.out.println("myMsg:"+msg);

2 :在Activity中

<activity>
    <meta-data android:name="myMsg" 
               android:value="hellomy activity">
    </meta-data>
</activity>
ActivityInfo info=this.getPackageManager().getActivityInfo(getComponentName(),PackageManager.GET_META_DATA);
String msg=info.metaData.getString("myMsg");
System.out.println("myMsg:"+msg);

3:在service的中

<service android:name="MetaDataService">
    <meta-data android:value="hellomy service" 
               android:name="myMsg">
    </meta-data>
</service>
ComponentName cn=new ComponentName(this, MetaDataService.class);
ServiceInfo info=this.getPackageManager().getServiceInfo(cn, PackageManager.GET_META_DATA);
String msg=info.metaData.getString("myMsg");
System.out.println("myMsg:"+msg);

4: 在receiver中

<receiver android:name="MetaDataReceiver">
    <meta-data android:value="hellomy receiver" 
               android:name="myMsg">
    </meta-data>
    <intent-filter>
        <action android:name="android.intent.action.PHONE_STATE"></action>
    </intent-filter>
</receiver>
ComponentName cn=new ComponentName(context, MetaDataReceiver.class);
ActivityInfo info=context.getPackageManager().getReceiverInfo(cn, PackageManager.GET_META_DATA);
String msg=info.metaData.getString("myMsg");
System.out.println("myMsg:"+msg);

转载自http://blog.youkuaiyun.com/kakaxi1o1/article/details/37595145

<template> <view class="container"> <!-- 头部区域 --> <cover-view class="cover-header"> <cover-view class="cover-title">预览界面</cover-view> </cover-view> <!-- web-view 区域 --> <web-view :src="previewUrl"></web-view> <!-- 底部按钮区域 --> <cover-view class="bottom-btn-container"> <cover-view class="btn" @click="handleGiveUp">放弃</cover-view> <cover-view class="btn" @click="handleSolidify">固化</cover-view> </cover-view> </view> </template> <script> export default { data() { return { previewUrl: '', pdfPath: '', savedPdfUrl: '', base64PdfData: '' }; }, onLoad(options) { const pdfPath = decodeURIComponent(options.url); this.previewUrl = `/static/pdf-preview.html?pdfUrl=${encodeURIComponent(pdfPath)}`; this.pdfPath = pdfPath; }, methods: { handleGiveUp() { uni.navigateBack({ delta: 1 }); }, handleSolidify() { if (!this.pdfPath) { uni.showToast({ title: 'PDF路径为空', icon: 'none' }); return; } uni.showLoading({ title: '固化中' }); uni.downloadFile({ url: this.pdfPath, success: (downloadRes) => { if (downloadRes.statusCode === 200) { const tempFilePath = downloadRes.tempFilePath; console.log('文件下载成功:', tempFilePath); console.log('开始上传到: http://192.168.1.80:159:1592/api/upload'); uni.uploadFile({ url: 'http://192.168.1.80:1592/api/upload', // url: 'http://192.168.0.113:1592/api/upload', filePath: tempFilePath, name: 'file', formData: { timestamp: new Date().getTime() }, success: (uploadRes) => { console.log('上传响应状态:', uploadRes.statusCode); console.log('响应头:', uploadRes.header); console.log('原始响应数据:', uploadRes.data); let response; try { // 兼容多平台 response = typeof uploadRes.data === 'string' ? JSON.parse(uploadRes.data) : uploadRes.data; } catch (e) { console.error('解析错误:', e); uni.showToast({ title: '解析响应失败,请检查服务器返回格式', icon: 'none', duration: 3000 }); return; } // 检查是否为有效对象 if (typeof response !== 'object' || response === null) { console.warn('无效的响应格式:', response); uni.showToast({ title: '服务器返回了无效数据格式', icon: 'none', duration: 3000 }); return; } // 处理有效响应 if (response.code == 200) { const base64PdfData = response.data; if (!base64PdfData) { console.error('无效的PDF地址:', base64PdfData); uni.showToast({ title: '服务器未返回PDF地址', icon: 'none' }); console.error('服务器响应:', response); return; } console.log('服务器返回的PDF URL:', base64PdfData); uni.navigateTo({ url: '/pages/home/home' }); uni.base64ToPath({ base64Data: base64PdfData, // 纯Base64字符串(不含data:前缀) fileType: 'pdf' // 指定文件类型为PDF }) .then((res) => { const tempPdfPath = res.tempFilePath; // 转换后的本地临时路径 console.log('Base64转PDF成功:', tempPdfPath); // 保存证据信息到本地缓存(供evidence页面使用) const evidenceInfo = { uploadTime: new Date().getTime(), // 记录上传时间 pdfUrl: tempPdfPath }; uni.setStorageSync('currentEvidence', evidenceInfo); // 显示成功弹窗并跳转 uni.showModal({ title: '固化成功!', content: '您的证据原件保存在“我的证据”中,导出方法详见《使用说明》', confirmText: '查看证据', cancelText: '继续取证', success: (res) => { if (res.confirm) { // 携带本地PDF路径跳转至evidence页面 uni.navigateTo({ url: `/pages/home/components/evidence?pdfUrl=${encodeURIComponent(tempPdfPath)}` }); } } }); }) .catch((err) => { console.error('Base64转换失败:', err); uni.showToast({ title: '证书转换失败', icon: 'none' }); }); } else { uni.showToast({ title: PdfResult.message || '固化失败', icon: 'none', duration: 3000 }); } }, fail: (err) => { console.error('上传失败:', err); uni.showToast({ title: `上传失败: ${err.errMsg || '未知错误'}`, icon: 'none', duration: 3000 }); }, complete: () => { uni.hideLoading(); } }); } else { uni.showToast({ title: `下载失败: ${downloadRes.statusCode}`, icon: 'none' }); } }, fail: (err) => { console.error('下载失败:', err); uni.showToast({ title: `下载失败: ${err.errMsg || '未知错误'}`, icon: 'none', duration: 3000 }); uni.hideLoading(); } }); } } }; </script> <style> .container { width: 100%; height: 100vh; position: relative; overflow: hidden; } .cover-header { position: fixed; top: 0; left: 0; right: 0; background-color: #2e8bff; height: 130rpx; display: flex; justify-content: center; align-items: center; z-index: 999; padding-bottom: 10rpx; } .cover-title { color: #ffffff; font-size: 32rpx; font-weight: bold; } web-view { position: absolute; top: 44px; bottom: 64px; width: 100%; } .bottom-btn-container { position: fixed; bottom: 0; left: 0; right: 0; display: flex; flex-direction: row; justify-content: space-around; align-items: center; background-color: #2e8bff; /* background-color: rgba(0, 0, 0, 0.3); */ z-index: 999; } .btn { width: 45%; height: 80rpx; line-height: 80rpx; text-align: center; background-color: #2e8bff; color: #fff; font-size: 32rpx; border-radius: 10rpx; margin: 20rpx 0; } </style> 这是webview组件 固化成功后 跳转到 首页 弹窗查看证据和继续取证 点击查看证据跳到evidence组件 跳转时将后端获取的内容转换为base64字节数组 展示在evidence组件里 完整修改
最新发布
08-19
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值