当前位置: 首页 > 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>

相关文章:

  • 【lammps】后处理 log.lammps
  • 如何在idea 中写spark程序
  • Linux学习笔记(一):Linux下的基本指令
  • 详解RabbitMQ工作模式之简单模式
  • 天猫TP代运营服务商-品融电商:助力品牌破局增长的专业推手
  • 智慧健康养老实训室建设方案:科技引领养老健康服务人才培养
  • 技术与文化双轮驱动:数字化转型的核心要素
  • 运维实施27-Linux权限管理
  • OpenGL进阶系列21 - OpenGL SuperBible - blendmatrix 例子学习
  • Pytorch深度学习框架60天进阶学习计划 - 第53天:自监督学习范式(一)
  • C++?模板!!!
  • 深入浅出JavaScript常见设计模式:从原理到实战(2)
  • TMI投稿指南(二):投稿文章注意事项
  • 维安WAYON推出32位MCU:WY32F1030系列
  • Ajax 提交表单与文件上传
  • 快乐数(双指针解法)
  • Spring框架allow-bean-definition-overriding详细解释
  • 永磁同步电机控制算法-转速环电流环SMC控制器
  • 微信jdk 前端vue获取流程1、
  • 基于【低代码+AI智能体】开发智能考试系统
  • 俄罗斯延长非法滞留外国人限期离境时间至9月
  • 幸福航空五一前三天航班取消:客服称目前是锁舱状态,无法确认何时恢复
  • 早睡1小时,变化有多惊人?第一个就没想到
  • 借助AI应用,自闭症人群开始有可能真正“读懂他人”
  • 上海首个航空前置货站落户松江综合保税区,通关效率可提升30%
  • “一对一讨论诸多事宜”,泽连斯基披露此次特泽会更多细节