当前位置: 首页 > news >正文

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

 

配置文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"><applicationandroid: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-dataandroid:name="api_kay"android:value="123dad*^t8"/><activityandroid: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-dataandroid:name="LiLi"android:value="hello"/></activity></application></manifest>

活动

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")@Overrideprotected 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;});}@Overridepublic 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 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"><TextViewandroid: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" /><LinearLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><Buttonandroid:id="@+id/activity_btn"android:layout_width="match_parent"android:layout_height="50dp"android:text="点击获取局部元数据"tools:ignore="MissingConstraints" /><Buttonandroid:id="@+id/application_btn"android:layout_width="match_parent"android:layout_height="50dp"android:text="点击获取全局元数据"tools:ignore="MissingConstraints" />
</LinearLayout></androidx.constraintlayout.widget.ConstraintLayout>

http://www.xdnf.cn/news/190675.html

相关文章:

  • 近地卫星网络 (Low Earth Orbit Satellite Networks)入门学习笔记
  • Transformer数学推导——Q26 推导多语言Transformer中语言间注意力共享的参数效率公式
  • C语言----操作符详解(万字详解)
  • python 线程池顺序执行
  • 二叉树的所有路径(回溯算法基础)
  • 深度学习---Pytorch概览
  • 3D模型文件格式之《DAE格式介绍》
  • [LeetCode 438/567] 找到字符串中所有字母异位词/字符串的排列(滑动窗口)
  • tsconfig.json的配置项介绍
  • 云原生周刊:Kubernetes v1.33 正式发布
  • 用JavaScript构建3D程序
  • 2025系统架构师---论微服务架构及其应用
  • Linux中的系统延时任务和定时任务与时间同步服务和构建时间同步服务器
  • 老电脑优化全知道(包括软件和硬件优化)
  • 【爬虫】一文掌握 adb 的各种指令(adb备忘清单)
  • 【Mybatis】Mybatis基础
  • 集合框架篇-java集合家族汇总
  • 【3D基础】深入解析OBJ与MTL文件格式:Blender导出模型示例及3D开发应用
  • 【KWDB 创作者计划】_企业数据管理的利刃:技术剖析与应用实践
  • CMake:设置编译C++的版本
  • 【北京】昌平区某附小v3700存储双控故障维修案例
  • 分布式链路追踪理论
  • 【Axure视频教程】手电筒效果
  • 【题解-Acwing】867. 分解质因数
  • 【蒸馏(5)】DistillBEV代码分析
  • FPGA-DDS信号发生器
  • 3D架构图软件 iCraft Editor 正式发布 @icraft/player-react 前端组件, 轻松嵌入3D架构图到您的项目
  • 数据可视化
  • 【C++教程】三目运算符
  • Day8 鼠标控制与32位模式切换