8.Android(通过Manifest配置文件传递数据(meta-data))

配置文件

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

    <application
        android:allowBackup="true"
        android:dataExtractionRules="@xml/data_extraction_rules"
        android:fullBackupContent="@xml/backup_rules"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.MyApplication"
        tools:targetApi="31">
        <!--   1.application标签下是全局元元素-->
        <meta-data
            android:name="api_kay"
            android:value="123dad*^t8"/>

        
        <activity
            android:name=".MainActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>

            <!--2.activity局部元元素-->
            <meta-data
                android:name="LiLi"
                android:value="hello"/>
        </activity>



    </application>

</manifest>

活动

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

import static android.content.ContentValues.TAG;

import android.annotation.SuppressLint;
import android.content.pm.ActivityInfo;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import java.text.BreakIterator;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_main);
        findViewById(R.id.activity_btn).setOnClickListener(this);
        findViewById(R.id.application_btn).setOnClickListener(this);

        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });
    }

    @Override
    public void onClick(View v) {
        TextView text = findViewById(R.id.get_api);
        if(v.getId() == R.id.application_btn){
            //        1.全局元数据的获取方式
            try {
                ApplicationInfo info = getPackageManager().getApplicationInfo(
                        getPackageName(), PackageManager.GET_META_DATA);
                String api_kay = info.metaData.getString("api_kay");
                Log.d(TAG, "_________________" + api_kay);
                // 使用配置值(如显示到TextView)
                text.setText(api_kay);
            } catch (PackageManager.NameNotFoundException e) {
                throw new RuntimeException(e);
            }
        }


        if(v.getId() == R.id.activity_btn){
            //        2.局部元数据的获取方式
            try {
                ActivityInfo info = getPackageManager().getActivityInfo(
                        getComponentName(), PackageManager.GET_META_DATA);
                String LiLi = info.metaData.getString("LiLi");
                Log.d(TAG, "_________________" + LiLi);
                // 使用配置值(如显示到TextView)
                text.setText(LiLi);
            } catch (PackageManager.NameNotFoundException e) {
                throw new RuntimeException(e);
            }
        }
    }
}

布局

XML 复制代码
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/get_api"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <Button
        android:id="@+id/activity_btn"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:text="点击获取局部元数据"
        tools:ignore="MissingConstraints" />
    <Button
        android:id="@+id/application_btn"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:text="点击获取全局元数据"
        tools:ignore="MissingConstraints" />
</LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>
相关推荐
Kapaseker31 分钟前
酷炫的文字效果 — Compose 文本着色
android·kotlin
努力进修1 小时前
【JavaEE初阶】 多线程编程核心:解锁线程创建、方法与状态的创新实践密码
android·java·java-ee
生莫甲鲁浪戴1 小时前
Android Studio新手开发第二十八天
android·ide·android studio
zhaoyufei1331 小时前
Android触屏TP驱动事件上报以及多点触摸
android
杨筱毅2 小时前
【Android】详细讲解ViewDragHelper的实现原理(不含代码版)
android
代码s贝多芬的音符9 小时前
ios android 小程序 蓝牙 CRC16_MODBUS
android·ios·小程序
2501_9159184111 小时前
iOS 混淆实战 多工具组合完成 IPA 混淆、加固与工程化落地(iOS混淆|IPA加固|无源码混淆|Ipa Guard|Swift Shield)
android·ios·小程序·https·uni-app·iphone·webview
雨白11 小时前
让协程更健壮:全面的异常处理策略
android·kotlin
Jeled12 小时前
AI: 生成Android自我学习路线规划与实战
android·学习·面试·kotlin
游戏开发爱好者813 小时前
如何系统化掌握 iOS 26 App 耗电管理,多工具协作
android·macos·ios·小程序·uni-app·cocoa·iphone