运行有问题或需要源码请点赞关注收藏后评论区留言
利用资源文件配置字符串
利用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>
创作不易 觉得有帮助请点赞关注收藏~~~