Android Studio App开发入门之活动Activity中为活动补充附加信息讲解及实战(附源码 超详细必看)

简介: Android Studio App开发入门之活动Activity中为活动补充附加信息讲解及实战(附源码 超详细必看)

运行有问题或需要源码请点赞关注收藏后评论区留言

利用资源文件配置字符串

利用Bundle固然能在页面跳转得时候传送数据,但这仅限于在代码中传递参数,如果要求临时修改某个参数的数值,就得去该Java代码,然而直接修改Java代码有两个弊端

1:代码文件太多 不好找到修改的地方

2:每次改动代码都得重新编译 浪费时间

因此我们可以通过res/value目录下的strings.xml是用来配置字符串形式的参数。可以通过它来配置字符串 效果如下

ReadStringActivity类代码如下

package com.example.chapter04;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class ReadStringActivity extends AppCompatActivity {
    private TextView tv_resource; // 声明一个文本视图对象
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_read_string);
        // 从布局文件中获取名叫tv_resource的文本视图
        tv_resource = findViewById(R.id.tv_resource);
        showStringResource(); // 显示字符串资源
    }
    // 显示字符串资源
    private void showStringResource() {
        String value = getString(R.string.weather_str); // 从strings.xml获取名叫weather_str的字符串值
        tv_resource.setText("来自字符串资源:今天的天气是"+value); // 在文本视图上显示文字
    }
}

activity_read_stringXML文件代码如下

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <TextView
        android:id="@+id/tv_resource"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="5dp"
        android:gravity="center"
        android:textColor="#000000"
        android:textSize="17sp" />
</LinearLayout>

利用元数据传递配置信息

尽管资源文件能够配置字符串参数,然而有时候为了安全起见,某个参数要给某个活动专用,并不希望其他活动也能获取该参数,此时就不方便用getString方法了,因此Activity提供了元数据的概念,它是一种描述其他数据的数据,它相当于描述固定活动的参数信息 效果如下

MetaDataActivity类代码如下

package com.example.chapter04;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class MetaDataActivity extends AppCompatActivity {
    private TextView tv_meta; // 声明一个文本视图对象
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_meta_data);
        // 从布局文件中获取名叫tv_meta的文本视图
        tv_meta = findViewById(R.id.tv_meta);
        showMetaData(); // 显示配置的元数据
    }
    // 显示配置的元数据
    private void showMetaData() {
        try {
            PackageManager pm = getPackageManager(); // 获取应用包管理器
            // 从应用包管理器中获取当前的活动信息
            ActivityInfo act = pm.getActivityInfo(getComponentName(), PackageManager.GET_META_DATA);
            Bundle bundle = act.metaData; // 获取活动附加的元数据信息
            String value = bundle.getString("weather"); // 从包裹中取出名叫weather的字符串
            tv_meta.setText("来自元数据信息:今天的天气是"+value); // 在文本视图上显示文字
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

activity_meta_dataXML文件代码如下

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <TextView
        android:id="@+id/tv_meta"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="5dp"
        android:gravity="center"
        android:textColor="#000000"
        android:textSize="17sp" />
</LinearLayout>

给应用页面注册快捷方式

快捷方式即长按应用图标后会显示应用的一些功能,就可以直接点击使用,不用再点击应用里面去寻找,十分方便

在res目录下创建名为xml的文件夹,并在该文件夹下创建shortcuts.xml 代码如下

<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">
        <!-- targetClass指定了点击该项菜单后要打开哪个活动页面 -->
        <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">
        <!-- targetClass指定了点击该项菜单后要打开哪个活动页面 -->
        <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">
        <!-- targetClass指定了点击该项菜单后要打开哪个活动页面 -->
        <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>

MainActivity类代码如下

package com.example.chapter04;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViewById(R.id.btn_act_open).setOnClickListener(this);
        findViewById(R.id.btn_act_life).setOnClickListener(this);
        findViewById(R.id.btn_act_jump).setOnClickListener(this);
        findViewById(R.id.btn_act_login).setOnClickListener(this);
        findViewById(R.id.btn_action_uri).setOnClickListener(this);
        findViewById(R.id.btn_send_receive).setOnClickListener(this);
        findViewById(R.id.btn_request_response).setOnClickListener(this);
        findViewById(R.id.btn_read_string).setOnClickListener(this);
        findViewById(R.id.btn_meta_data).setOnClickListener(this);
    }
    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.btn_act_open) {
            Intent intent = new Intent(this, ActStartActivity.class);
            startActivity(intent);
        } else if (v.getId() == R.id.btn_act_life) {
            Intent intent = new Intent(this, ActLifeActivity.class);
            startActivity(intent);
        } else if (v.getId() == R.id.btn_act_jump) {
            Intent intent = new Intent(this, JumpFirstActivity.class);
            startActivity(intent);
        } else if (v.getId() == R.id.btn_act_login) {
            Intent intent = new Intent(this, LoginInputActivity.class);
            startActivity(intent);
        } else if (v.getId() == R.id.btn_action_uri) {
            Intent intent = new Intent(this, ActionUriActivity.class);
            startActivity(intent);
        } else if (v.getId() == R.id.btn_send_receive) {
            Intent intent = new Intent(this, ActSendActivity.class);
            startActivity(intent);
        } else if (v.getId() == R.id.btn_request_response) {
            Intent intent = new Intent(this, ActRequestActivity.class);
            startActivity(intent);
        } else if (v.getId() == R.id.btn_read_string) {
            Intent intent = new Intent(this, ReadStringActivity.class);
            startActivity(intent);
        } else if (v.getId() == R.id.btn_meta_data) {
            Intent intent = new Intent(this, MetaDataActivity.class);
            startActivity(intent);
        }
    }
}

activity_mainXML文件代码如下

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingBottom="10dp"
            android:orientation="vertical">
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="20dp"
                android:gravity="center"
                android:text="4.1 启停活动" />
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">
                <Button
                    android:id="@+id/btn_act_open"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="打开新页面" />
                <Button
                    android:id="@+id/btn_act_life"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="活动的生命周期" />
            </LinearLayout>
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">
                <Button
                    android:id="@+id/btn_act_jump"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="两个页面来回跳转" />
                <Button
                    android:id="@+id/btn_act_login"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="登录成功不再返回" />
            </LinearLayout>
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="20dp"
                android:gravity="center"
                android:text="4.2 传递参数" />
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">
                <Button
                    android:id="@+id/btn_action_uri"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="跳转到Uri" />
                <Button
                    android:id="@+id/btn_send_receive"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="传递请求数据" />
                <Button
                    android:id="@+id/btn_request_response"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="返回应答数据" />
            </LinearLayout>
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="20dp"
                android:gravity="center"
                android:text="4.3 附加信息" />
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">
                <Button
                    android:id="@+id/btn_read_string"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="读取字符串资源" />
                <Button
                    android:id="@+id/btn_meta_data"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="读取元数据配置" />
            </LinearLayout>
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">
                <Button
                    android:id="@+id/btn_shortcuts"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="快捷方式请回到桌面长按应用图标" />
            </LinearLayout>
        </LinearLayout>
    </ScrollView>
</LinearLayout>

创作不易 觉得有帮助请点赞关注收藏~~~

相关文章
|
移动开发 前端开发 Android开发
【02】建立各项目录和页面标准化产品-vue+vite开发实战-做一个非常漂亮的APP下载落地页-支持PC和H5自适应提供安卓苹果鸿蒙下载和网页端访问-优雅草卓伊凡
【02】建立各项目录和页面标准化产品-vue+vite开发实战-做一个非常漂亮的APP下载落地页-支持PC和H5自适应提供安卓苹果鸿蒙下载和网页端访问-优雅草卓伊凡
2099 12
【02】建立各项目录和页面标准化产品-vue+vite开发实战-做一个非常漂亮的APP下载落地页-支持PC和H5自适应提供安卓苹果鸿蒙下载和网页端访问-优雅草卓伊凡
|
移动开发 JavaScript 应用服务中间件
【06】优化完善落地页样式内容-精度优化-vue加vite开发实战-做一个非常漂亮的APP下载落地页-支持PC和H5自适应提供安卓苹果鸿蒙下载和网页端访问-优雅草卓伊凡
【06】优化完善落地页样式内容-精度优化-vue加vite开发实战-做一个非常漂亮的APP下载落地页-支持PC和H5自适应提供安卓苹果鸿蒙下载和网页端访问-优雅草卓伊凡
1334 5
【06】优化完善落地页样式内容-精度优化-vue加vite开发实战-做一个非常漂亮的APP下载落地页-支持PC和H5自适应提供安卓苹果鸿蒙下载和网页端访问-优雅草卓伊凡
|
移动开发 Rust JavaScript
【01】首页建立-vue+vite开发实战-做一个非常漂亮的APP下载落地页-支持PC和H5自适应提供安卓苹果鸿蒙下载和网页端访问-优雅草卓伊凡
【01】首页建立-vue+vite开发实战-做一个非常漂亮的APP下载落地页-支持PC和H5自适应提供安卓苹果鸿蒙下载和网页端访问-优雅草卓伊凡
1388 4
【01】首页建立-vue+vite开发实战-做一个非常漂亮的APP下载落地页-支持PC和H5自适应提供安卓苹果鸿蒙下载和网页端访问-优雅草卓伊凡
|
开发工具 Android开发
X Android SDK file not found: adb.安卓开发常见问题-Android SDK 缺少 `adb`(Android Debug Bridge)-优雅草卓伊凡
X Android SDK file not found: adb.安卓开发常见问题-Android SDK 缺少 `adb`(Android Debug Bridge)-优雅草卓伊凡
1170 11
X Android SDK file not found: adb.安卓开发常见问题-Android SDK 缺少 `adb`(Android Debug Bridge)-优雅草卓伊凡
|
移动开发 Android开发
【03】建立隐私关于等相关页面和内容-vue+vite开发实战-做一个非常漂亮的APP下载落地页-支持PC和H5自适应提供安卓苹果鸿蒙下载和网页端访问-优雅草卓伊凡
【03】建立隐私关于等相关页面和内容-vue+vite开发实战-做一个非常漂亮的APP下载落地页-支持PC和H5自适应提供安卓苹果鸿蒙下载和网页端访问-优雅草卓伊凡
647 0
|
Java 开发工具 Maven
【01】完整的安卓二次商业实战-详细的初级步骤同步项目和gradle配置以及开发思路-优雅草伊凡
【01】完整的安卓二次商业实战-详细的初级步骤同步项目和gradle配置以及开发思路-优雅草伊凡
1659 6
|
安全 数据库 Android开发
在Android开发中实现两个Intent跳转及数据交换的方法
总结上述内容,在Android开发中,Intent不仅是活动跳转的桥梁,也是两个活动之间进行数据交换的媒介。运用Intent传递数据时需注意数据类型、传输大小限制以及安全性问题的处理,以确保应用的健壯性和安全性。
845 11
|
缓存 小程序 视频直播
基于uni-app+vite5+vue3实战短视频+直播+聊天app应用
基于uniapp+vue3+vite5从0-1实战搭建仿抖音/微信直播带货商城。集短视频+聊天+直播功能于一体。实现全屏沉浸式切换短视频/直播,支持编译运行到h5+小程序端+app端。
756 4
|
移动开发 Java 编译器
Kotlin与Jetpack Compose:Android开发生态的演进与架构思考
本文从资深Android工程师视角深入分析Kotlin与Jetpack Compose在Android系统中的技术定位。Kotlin通过空安全、协程等特性解决了Java在移动开发中的痛点,成为Android官方首选语言。Jetpack Compose则引入声明式UI范式,通过重组机制实现高效UI更新。两者结合不仅提升开发效率,更为跨平台战略和现代架构模式提供技术基础,代表了Android开发生态的根本性演进。
712 0
|
存储 移动开发 监控
App Trace功能实战:一键拉起、快速安装与免提写邀请码的应用实践
App Trace系统通过一键拉起、快速安装和免提写邀请码三大功能,显著提升用户转化率、安装成功率和邀请注册率。结合深度技术实现与优化,助力公司用户增长,成为移动端核心基础设施。

热门文章

最新文章