安卓基础之《(6)—Activity组件(3)》

==========为活动补充附加信息==========

八、利用资源文件配置字符串

1、Activity从strings.xml获取字符串,显示到TextView中

上下文Context类里有getString方法,就是从strings.xml中获取字符串的值。而Activity就是继承自Context

所以可以直接getString(id)

2、为什么把字符串放到strings.xml中

strings.xml是不需要编译的,而Java代码需要编译。配置文件可以随时改

3、例子

strings.xml

XML 复制代码
<resources>
    <string name="app_name">chapter04</string>
    <string name="weather_str">晴天</string>
</resources>

ReadStringActivity.java

java 复制代码
package com.example.chapter04;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.TextView;

public class ReadStringActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_read_string);

        TextView tv_resource = findViewById(R.id.tv_resource);
        // 从strings.xml获取名叫weather_str的字符串值
        String value = getString(R.string.weather_str);
        tv_resource.setText(value);
    }
}

九、利用元数据传递配置信息

1、有的时候Activity配置信息不是放在strings.xml中,它会放到清单文件中

比如调用第三方的SDK

高德地图

友盟

微信登录

一般是把要验证的token值放在meta-data中

2、元数据是一种描述其它数据的数据,它相当于描述固定活动的参数信息

3、在Activity节点内部添加meta-data标签,通过属性name指定元数据的名称,通过属性value指定元数据的值

4、在代码中获取元数据

在Java代码中,获取元数据信息的步骤分为下列三步:

(1)调用getPackageManager方法获得当前应用的包管理器

(2)调用包管理器的getActivityInfo方法获得当前Activity的信息对象

(3)Activity信息对象的metaData是Bundle包裹类型,调用包裹对象的getString即可获得指定名称的参数值

5、例子

activity标签

XML 复制代码
        <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="晴天"/>

        </activity>

MetaDataActivity.java

java 复制代码
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);
        // 获取应用包管理器
        PackageManager packageManager = getPackageManager();
        try {
            // 从应用包管理器中获取当前的活动信息
            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) {
            throw new RuntimeException(e);
        }

    }
}

十、给应用页面注册快捷方式

1、元数据不仅能传递简单的字符串参数,还能传送更复杂的资源数据,比如App的快捷方式菜单

2、添加文件,在res下新建xml文件夹,然后建立shortcuts.xml文件

XML 复制代码
<?xml version="1.0" encoding="utf-8"?>
<shortcuts xmlns:android="http://schemas.android.com/apk/res/android">

    <shortcut
        android:shortcutId="first"
        android:enabled="true"
        android:icon="@mipmap/ic_launcher"
        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>
</shortcuts>

给App添加两个快捷菜单,一个跳转到ActStartActivity,另一个跳转到JumpFirstActivity

3、在清单文件activity标签添加快捷方式元数据关联

<meta-data android:name="android.app.shortcuts" android:resource="@xml/shortcuts"/>

XML 复制代码
        <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="晴天"/>
            <meta-data android:name="android.app.shortcuts" android:resource="@xml/shortcuts"/>

        </activity>

4、例子

5、元数据的meta-data标签除了前面说到的name属性和value属性,还拥有resource属性,该属性可指定一个XML文件,表示元数据想要的复杂信息保存于XML数据之中

6、利用元数据配置快捷菜单的步骤如下

(1)在res/values/strings.xml添加各个菜单项名称的字符串配置

(2)创建res/xml/shortcuts.xml,在该文件中填入各组菜单项的快捷方式定义(每个菜单对应哪个活动页面)

(3)给activity节点注册元数据的快捷菜单配置

相关推荐
千里马学框架5 天前
一起学 Android 14:ShellTransition 屏幕旋转过程深度剖析
android·智能手机·性能优化·framework·性能·屏幕旋转·rotation
美狐美颜SDK开放平台5 天前
开发直播APP时如何接入视频美颜SDK?开发流程与注意事项
android·人工智能·计算机视觉·音视频·直播美颜sdk
AFinalStone5 天前
Android7 SystemUI源码解析(七)Keyguard锁屏模块深度解析
android·systemui
致远ccc5 天前
Google Play 上架前如何测试 App?多国家 Android 环境测试
android·app测试·googleplay·多国家应用测试
ttyyttemo5 天前
Kotlin 协程中的 Job 结构化并发与取消
android
sun0077005 天前
tbox 4g/5g切换,导致wan ip 改变,导致车机旧网络不可用。需要重启车机才行
android
其实防守也摸鱼5 天前
内网穿透与反向代理:原理、工具与实战指南
android·大数据·运维·安全·网络安全·自动化·渗透
AFinalStone5 天前
Android7 SystemUI 源码解析(四)NavigationBar 导航栏与 SystemBars
android·systemui
JMchen5 天前
属性动画原理与高级动画实现
android·kotlin·canvas
AFinalStone5 天前
Android7 SystemUI 源码解析(二)启动流程深度解析
android·systemui