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>

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

相关文章
|
5天前
|
测试技术 Android开发
Android App获取不到pkgInfo信息问题原因
Android App获取不到pkgInfo信息问题原因
14 0
|
29天前
|
Java Android开发
Android Studio的使用导入第三方Jar包
Android Studio的使用导入第三方Jar包
12 1
|
2月前
|
数据库 Android开发 数据库管理
【Android】使用android studio查看内置数据库信息
【Android】使用android studio查看内置数据库信息
76 0
|
2月前
|
编译器 开发工具 Android开发
|
6月前
|
存储 缓存 安全
Android14 适配之——现有 App 安装到 Android14 手机上需要注意些什么?
Android14 适配之——现有 App 安装到 Android14 手机上需要注意些什么?
269 0
|
4月前
|
传感器 物联网 Android开发
【Android App】物联网中查看手机支持的传感器及实现摇一摇功能-加速度传感器(附源码和演示 超详细)
【Android App】物联网中查看手机支持的传感器及实现摇一摇功能-加速度传感器(附源码和演示 超详细)
69 1
|
4月前
|
Android开发 网络架构
【Android App】检查手机连接WiFi信息以及扫描周围WiFi的讲解及实战(附源码和演示 超详细必看)
【Android App】检查手机连接WiFi信息以及扫描周围WiFi的讲解及实战(附源码和演示 超详细必看)
207 1
|
4月前
|
XML Java 定位技术
【Android App】定位导航GPS中开启手机定位功能讲解及实战(附源码和演示 超详细)
【Android App】定位导航GPS中开启手机定位功能讲解及实战(附源码和演示 超详细)
118 0
|
4月前
|
XML Java Android开发
Android App开发手机阅读中实现平滑翻书效果和卷曲翻书动画实战(附源码 简单易懂 可直接使用)
Android App开发手机阅读中实现平滑翻书效果和卷曲翻书动画实战(附源码 简单易懂 可直接使用)
72 0
|
4月前
|
XML Java Android开发
Android App开发手机阅读中PDF文件渲染器的讲解及使用(附源码 简单易懂)
Android App开发手机阅读中PDF文件渲染器的讲解及使用(附源码 简单易懂)
82 0