網(wǎng)站首頁 編程語言 正文
前序
格式
<meta-data android:name="weather" android:value="xxx"/>
什么場景需要使用?
使用第三方SDK,需要在APP應(yīng)用內(nèi)使用別的APP的整合包,如使用微信登錄、某某地圖等。
一、在代碼中獲取元數(shù)據(jù)
在java代碼中,獲取元數(shù)據(jù)信息的步驟分為下列三步:
- 調(diào)用getPackageManager方法獲得當(dāng)前應(yīng)用的包管理器;
- 調(diào)用包管理器的getActivityInfo方法獲得當(dāng)前活動(dòng)的信息對(duì)象;
- 活動(dòng)信息對(duì)象的metaData是Bundle包裹類型,調(diào)用包裹對(duì)象的getString即可獲得指定名稱的參數(shù)值。
例:從清單文件中獲取元數(shù)據(jù)并顯示到屏幕上
清單文件
<activity android:name=".MetaDataActivity" android:exported="true"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> <meta-data android:name="weather" android:value="xxx"/> </activity>
xml文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:id="@+id/tv_meta" android:layout_width="match_parent" android:layout_height="wrap_content"/> </LinearLayout>
java類
public class MetaDataActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_meta_data);
TextView tv_meta = findViewById(R.id.tv_meta);
//獲取應(yīng)用包管理器
PackageManager pm = getPackageManager();
try {
//從應(yīng)用包管理器中獲取當(dāng)前的活動(dòng)信息
ActivityInfo info = pm.getActivityInfo(getComponentName(), PackageManager.GET_META_DATA);
//獲取活動(dòng)附加的元數(shù)據(jù)信息
Bundle bundle = info.metaData;
String weather = bundle.getString("weather");
tv_meta.setText(weather);
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
}
}
運(yùn)行結(jié)果
二、給應(yīng)用頁面注冊(cè)快捷方式
元數(shù)據(jù)不僅能傳遞簡單的字符串參數(shù),還能傳送更復(fù)雜的資源數(shù)據(jù),如支付寶的快捷式菜單。
利用元數(shù)據(jù)配置快捷菜單
元數(shù)據(jù)的meta-data標(biāo)簽除了前面的name屬性和value屬性,還擁有resource屬性,該屬性可指定一個(gè)XML文件,表示元數(shù)據(jù)想要的復(fù)雜信息保存于XML數(shù)據(jù)之中。
利用元數(shù)據(jù)配置快捷菜單的步驟如下:
- 在res/values/strings.xml添加各個(gè)菜單項(xiàng)名稱的字符串配置
- 創(chuàng)建res/xml/shortcuts.xml,在該文件中填入各組菜單項(xiàng)的快捷方式定義。
- 給activity節(jié)點(diǎn)注冊(cè)元數(shù)據(jù)的快捷菜單配置。
例:長按應(yīng)用出現(xiàn)快捷菜單
清單文件AndroidManifest.xml
<activity android:name=".ActStartActivity" android:exported="true"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> <meta-data android:name="android.app.shortcuts" android:resource="@xml/shortcuts"/> </activity>
新建shortcuts.xml文件用于配置快捷菜單
<resources xmlns:android="http://schemas.android.com/apk/res/android"> <shortcut android:shortcutId="first" android:enabled="true" android:icon="@mipmap/ic_launcher" android:shortcutLongLabel="@string/first_long" android:shortcutShortLabel="@string/first_short"> <!--文字太長則顯示shotLabel ↑--> <!--點(diǎn)擊選項(xiàng)跳轉(zhuǎn)到的頁面 ↓--> <intent android:action="android.intent.action.VIEW" android:targetPackage="com.example.chapter2" android:targetClass="com.example.chapter2.ActStartActivity"/> <categories android:name="android.shortcut.conversation"/> </shortcut> </resources>
運(yùn)行結(jié)果:長按出現(xiàn)快捷菜單
原文鏈接:https://blog.csdn.net/Tir_zhang/article/details/126956205
相關(guān)推薦
- 2023-05-24 Pytorch:Conv2d卷積前后尺寸詳解_python
- 2022-11-11 利用Python程序讀取Excel創(chuàng)建折線圖_python
- 2022-05-15 Web?API中使用Autofac實(shí)現(xiàn)依賴注入_實(shí)用技巧
- 2021-12-08 Linux之操作文件的系統(tǒng)調(diào)用_Linux
- 2022-10-31 Kotlin中Object關(guān)鍵字的使用示例介紹_Android
- 2022-02-27 Required String paramter ‘username‘ is not present
- 2022-06-06 PowerShell yarn : 無法加載文件 C:\Users\Admin\AppData\Ro
- 2022-10-12 Python數(shù)據(jù)分析之使用scikit-learn構(gòu)建模型_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支