2024年最新Android基础教程——从入门到精通(上)_生皮鞣制工艺流程,2024年最新软件项目经理面试经典问题

img
img

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化的资料的朋友,可以戳这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

intent.setComponent(component);

2. 隐式Intent:

没有明确指定所要跳转的页面,而是通过一些动作字符串来让系统自动匹配。

通常是App不想向外暴露Activity的名称,只给出一些定义好的字符串。这些字符串可以自己定义,也有系统定义的。

常见的系统动作如下:

image-20230109224228884

下面以调用系统拨号页面举例:

String phone = “12345”;
Intent intent = new Intent();
//这里表示设置意图动作为准备拨号
intent.setAction(Intent.ACTION_DIAL);
intent.setData(Uri.parse(“tel:” + phone));
startActivity(intent);

如果想要跳转到自己定义的activity:

步骤一:在AndroidManifest.xml找到该activity,添加action和category标签,同时设置exported为true,表示允许被其他activity调用。

image-20230109230403488

步骤二:调用过程和上面一样:

Intent intent = new Intent();
intent.setAction(“android.intent.action.activity2”);
intent.addCategory(Intent.CATEGORY_DEFAULT);
startActivity(intent);

(2)向下一个Activity发送消息:

Intent重载了很多putExtra方法用于传递各种类型的信息,包括整数类型,字符串等。但是显然通过调用putExtra方法会很不好管理,因为数据都是零碎传递。所以Android引入了Bundle,其内部是一个Map,使用起来也和Map一样。

image-20230109165406124

示例:

Intent intent = new Intent(this, NextActivity.class);
//通过bundle包装数据
Bundle bundle = new Bundle();
bundle.putString(“stringKey”, “stringValue”);
intent.putExtras(bundle);
startActivity(intent);

然后下一个Activity就可以通过intent获取到所想要的数据了:

Bundle bundle = getIntent().getExtras();
String stringValue = bundle.getString(“stringKey”);

(3)向上一个Activity返回消息:

上一个页面跳转到下一个页面,同时携带数据:

private ActivityResultLauncher register;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);

findViewById(R.id.bt).setOnClickListener(this);

//回调函数,返回到这个页面时所执行的程序
register = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), new ActivityResultCallback() {
//回调函数
@Override
public void onActivityResult(ActivityResult result) {
if (result != null) {
Intent intent = result.getData();
if (intent != null && result.getResultCode() == Activity.RESULT_OK) {
//获取到返回的数据
Bundle bundle = intent.getExtras();
//…
}
}
}
});
}

@Override
public void onClick(View v) {
Intent intent = new Intent(this, MainActivity3.class);
//跳转下一页面
register.launch(intent);

}

下一个页面接受到数据,处理之后返回结果给上一个页面:

Bundle bundle = getIntent().getExtras();
//…页面进行处理
//返回数据给上一个页面
Bundle bundle = new Bundle();
bundle.putString(“stringKey”, “stringValue”);
intent.putExtras(bundle);
setResult(Activity.RESULT_OK, intent);
finish();

5. Activity获取一些附加信息

(1)获取资源信息:

//获取strings.xml中的字符串资源
String text = getString(R.string.text);
//获取color.xml中的颜色资源
int black = getColor(R.color.black);

(2)获取元数据信息:

try {
//获取包管理器
PackageManager pm = getPackageManager();
//获取当前的Activity信息
ActivityInfo activityInfo = pm.getActivityInfo(getComponentName(), PackageManager.GET_META_DATA);
Bundle bundle = activityInfo.metaData;
String text2 = bundle.getString(“text2”);

} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}

四、数据存储

1. 共享参数SharedPreferences

(1)使用:

sharedPreferences是安卓的一个轻量级存储工具,采用的方式是key-value,以xml文件形式存在,文件路径为/data/data/应用包名/shared_prefs/文件名.xml。

适合场景:

  1. 简单且孤立的数据
  2. 文本数据,二进制数据则不合适
  3. 需要持久化的数据,也就是重启APP后数据仍然存在且有效。

实际开发中,sharedPreferences经常用来存储的数据有:APP的个性化配置信息,用户使用APP的行为信息等。

sharedPreferences对数据的存储和读取类似Map,提供put和set方法。

获取数据可以通过SharedPreferences对象获取:

//第一个参数表示文件名,第二个参数表示私有模式
SharedPreferences shared = getSharedPreferences(“fileName”, MODE_PRIVATE);
String name = shared.getString(“name”);

而存储数据则还需要借助Editor类:

SharedPreferences.Editor editor = shared.edit();
editor.putString(“name”, “oymn”);
editor.putInt(“age”, 20);
editor.commit();

(2)应用实例:记住密码功能
  1. 声明一个共享参数对象,并在onCreate中调用getSharedPreferences方法获取共享参数的实例。
  2. 登录成功时,如果用户勾选了“记住密码”,就使用共享参数保存手机号码与密码。

所以在登录页面的onCreat方法中添加获取共享参数的代码:

// 从share_login.xml获取共享参数对象
mShared = getSharedPreferences(“share_login”, MODE_PRIVATE);
// 获取共享参数保存的手机号码
String phone = mShared.getString(“phone”, “”);
// 获取共享参数保存的密码
String password = mShared.getString(“password”, “”);
et_phone.setText(phone); // 往手机号码编辑框填写上次保存的手机号
et_password.setText(password); // 往密码编辑框填写上次保存的密码

接着在登录成功方法中添加保存功能:

// 如果勾选了“记住密码”,就把手机号码和密码都保存到共享参数中
if (isRemember) {
SharedPreferences.Editor editor = mShared.edit(); // 获得编辑器的对象
editor.putString(“phone”, et_phone.getText().toString()); // 添加名叫phone的手机号码
editor.putString(“password”, et_password.getText().toString()); // 添加名叫password的密码
editor.commit(); // 提交编辑器中的修改
}

2. 数据库SQLite

SQLite是安卓的一种小巧的嵌入式数据库,基本使用和思路和Mysql无异。

(1)SQLiteDatabase

java代码层面借助SQLiteDatabase来对SQLite进行操作。

/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值