在意图之外给活动添加额外的信息,
首先可以把字符串参数放到字符串资源文件中,待App运行之时再从资源文件读取字符串值;
接着还能在AndroidManifest.xml中给指定活动配置专门的元数据,App运行时即可获取对应活动的元数据信息;
然后利用元数据的resource属性配置更复杂的XML定义,从而为App注册在长按桌面之时弹出的快捷菜单。
res\values\strings.xml可用来配置字符串形式的参数。
配置的字符串参数例子如下:
晴天 在活动页面的Java代码中,调用getString方法即可根据“R.string.参数名称”获得指定参数的字符串值。
获取代码示例如下:
// 显示字符串资源 private void showStringResource() {
// 从strings.xml获取名叫weather_str的字符串值
String value = getString(R.string.weather_str); // 在文本视图上显示文字 tv_resource.setText("来自字符串资源:今天的天气是"+value); }

元数据是一种描述其他数据的数据,它相当于描述固定活动的参数信息。
在activity节点内部添加meta-data标签,通过属性name指定元数据的名称,通过属性value指定元数据的值。示例如下:
也可引用strings.xml已定义的字符串资源,举例如下:
- package com.example.chapter04;
-
- import androidx.appcompat.app.AppCompatActivity;
-
- import android.content.pm.ActivityInfo;
- import android.content.pm.PackageManager;
- import android.os.Bundle;
- import android.widget.TextView;
-
- 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);
- try {
- PackageManager packageManager=getPackageManager();//获取应用包管理器
- //从应用宝里面获取当前活动信息
- ActivityInfo info = packageManager.getActivityInfo(getComponentName(), PackageManager.GET_META_DATA);
- Bundle bundle = info.metaData;//获取活动附加的元数据信息
- String weather = bundle.getString("weather");//从包中取出
- tv_meta.setText(weather);
- } catch (PackageManager.NameNotFoundException e) {
- e.printStackTrace();
- }
-
- }
- }


利用元数据配置快捷菜单
元数据的meta-data标签除了前面说到的name属性和value属性,还拥有resource属性,该属性可指定一个XML文件,表示元数据想要的复杂信息保存于XML数据之中。
利用元数据配置快捷菜单的步骤如下所示:
(1)在res/values/strings.xml添加各个菜单项名称的字符串配置
(2)创建res/xml/shortcuts.xml,在该文件中填入各组菜单项的快捷方式定义(每个菜单对应哪个活动页面)。
(3)给activity节点注册元数据的快捷菜单配置,举例如下:
注册了快捷方式的活动节点配置
完整的activity节点配置示例如下:
- <shortcuts xmlns:android="http://schemas.android.com/apk/res/android">
- <shortcut
- android:shortcutId="first"
- android:enabled="true"
- android:icon="@drawable/ic_back"
- android:shortcutShortLabel="@string/first_short"
- android:shortcutLongLabel="@string/first_long">
-
- <intent
- android:action="android.intent.action.VIEW"
- android:targetPackage="com.example.chapter04"
- android:targetClass="com.example.chapter04.ActStartActivity" />
- <categories android:name="android.shortcut.conversation"/>
- shortcut>
-
- <shortcut
- android:shortcutId="second"
- android:enabled="true"
- android:icon="@mipmap/ic_launcher"
- android:shortcutShortLabel="@string/second_short"
- android:shortcutLongLabel="@string/second_long">
-
- <intent
- android:action="android.intent.action.VIEW"
- android:targetPackage="com.example.chapter04"
- android:targetClass="com.example.chapter04.JumpFirstActivity" />
- <categories android:name="android.shortcut.conversation"/>
- shortcut>
-
- <shortcut
- android:shortcutId="third"
- android:enabled="true"
- android:icon="@mipmap/ic_launcher"
- android:shortcutShortLabel="@string/third_short"
- android:shortcutLongLabel="@string/third_long">
-
- <intent
- android:action="android.intent.action.VIEW"
- android:targetPackage="com.example.chapter04"
- android:targetClass="com.example.chapter04.LoginInputActivity" />
- <categories android:name="android.shortcut.conversation"/>
- shortcut>
- shortcuts>


| shortcutID | 快捷方式的编号 |
| enable | 是否启用快捷方式 true/false |
| icon | 快捷菜单左侧的图标 |
| shortcutShortLabel | 快捷菜单的短标签 |
| shortcutLongLabel | 快捷菜单的长标签 |