Android开发——intent在活动间穿梭

只有一个activity的活动太简单了,怎么从主活动跳转到其他活动呢?
按照上文《关于活动——Toast 、Menu、以及活动的销毁》继续写代码

零、准备

再建一个空活动:
在这里插入图片描述
不过这次自动生成的layout文件还看不懂,于是修改一下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id='@+id/button_2'
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Button 2" />

</LinearLayout>

自动生成的SecendActivity.javaAndroidManifest.xml不动就好。

一、啥是Intent

IntentAndroid程序中各组件进行交互的一种方式,它指明了将执行的动作,还可以在不同组件间传递数据。通常用来启动活动、启动服务、发送广播等。
通常分为显式Intent隐式Intent

二、显式Intent

Intent有多个构造函数的重载,其中一个是Intent(Context packageContext, Class<?> cls),其中两个参数:

  • Context:启动活动的上下文
  • Class:要启动的目标活动

使用这个就能返回Intent,然后将这个实例传入startActivity()即可启动。
修改按钮监听器:

public void onClick(View v) {
	// Toast.makeText(FirstActivity.this, "我被点击啦!", LENGTH_SHORT).show();
	// finish();
    Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
    startActivity(intent);
}

重新运行:
在这里插入图片描述

三、隐式Intent

隐式Intent并未明确指出启动哪个活动,而是通过actioncatagory等信息,让系统分析这个Intent去启动。

1、修改配置文件

这次修改AndroidManifest.xml配置文件,在SecondActivity中添加<intent-filter>的两行内容:

<activity android:name=".SecondActivity">
    <intent-filter>
        <action android:name="com.example.day01_empty.ACTION_START"/>
        <category android:name="android.intent.category.DEFAULT"/>
    </intent-filter>
</activity>
  • <action/>标签指明了当前活动可以响应的action:"com.example.day01_empty.ACTION_START"
  • <category/>包含了附加信息,指明当前活动能响应的Intent可能带有category
  • 只有<action/><category>中的内容同时匹配Intent中指定的actioncategory时,该活动才能响应该Intent

2、修改监听器

public void onClick(View v) {
	// Toast.makeText(FirstActivity.this, "我被点击啦!", LENGTH_SHORT).show();
	// finish();
	// Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
	Intent intent = new Intent("com.example.day01_empty.ACTION_START");
	startActivity(intent);
}
  • 这次又使用了Intent的一个构造函数,直接传递了action的字符串,表明我们想启动能响应"com.example.day01_empty.ACTION_START"这个action的活动
  • 无需传递category的原因是,这里的category是默认的,会自动传

3、运行

正常启动了SecondActivity
在这里插入图片描述

3、action 和 category

每个Intent只能指定一个action,但却能指定多个category,再添加一个,修改按钮监听器:

public void onClick(View v) {
	//  Toast.makeText(FirstActivity.this, "我被点击啦!", LENGTH_SHORT).show();
	// finish();
	// Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
    Intent intent = new Intent("com.example.day01_empty.ACTION_START");
    intent.addCategory("com.example.day01_empty.my_category");
    startActivity(intent);
}

通过addCategory()方法来添加category,这里我自定义了一个:com.example.day01_empty.my_category

4、运行奔溃

这次运行程序直接崩溃:
在这里插入图片描述
我们打开logcat查看日志:
在这里插入图片描述
错误信息中提示没有活动响应Intent,所以我们要再添加一个category声明:

<intent-filter>
    <action android:name="com.example.day01_empty.ACTION_START"/>
    <category android:name="android.intent.category.DEFAULT"/>
    <category android:name="com.example.day01_empty.my_category"/>
</intent-filter>

5、重新运行

这次就莫得问题了
在这里插入图片描述

四、隐式Intent的更多玩法

隐式Intent除了打开自己程序的活动,还能启动其他程序的活动,这就使得app间的功能可以共享。

1、用系统浏览器打开URL

(1)代码示例

修改按钮监听器:

public void onClick(View v) {
	// Toast.makeText(FirstActivity.this, "我被点击啦!", LENGTH_SHORT).show();
	// finish();
	// Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
	// Intent intent = new Intent("com.example.day01_empty.ACTION_START");
	// intent.addCategory("com.example.day01_empty.my_category");
	Intent intent = new Intent(Intent.ACTION_VIEW);
	intent.setData(Uri.parse("http://bilibili.com"));
	startActivity(intent);
}
  • 倒数第四行,指定actionIntent.ACTION_VIEW,这是Android的一个内置动作。对应配置文件的常量值是android.intent.action.VIEW
  • 然后通过Intent.setData()方法将Uri.parse()解析的Uri对象传递进去
(2)运行实例

点击按钮即可打开B站
在这里插入图片描述

(3)进阶

可以在<intetent-filter>标签中再添加一个<data>标签,用来精确指定当前活动能响应的数据类型。配置下列内容:

  • android:scheme:指定数据协议
  • android:host:指定主机名
  • android:port:指定端口号
  • android:path:指定之后的内容
  • android:mimeType:指定处理的数据类型,允许通配符

如果只指定某一项,那么就可以响应所有的支持的Intent了。

(4)代码示例

再建一个ThirdActivity,并配置布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id='@+id/button_3'
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Button 3" />

</LinearLayout>

修改配置信息:

<activity android:name=".ThirdActivity">
    <intent-filter tools:ignore="AppLinkUrlError">
        <action android:name="android.intent.action.VIEW"/>
        <category android:name="android.intent.category.DEFAULT"/>
        <data android:scheme="http"/>
    </intent-filter>
</activity>
(5)运行实例

配置好以后,就可以看到有两个应用都支持该响应:
在这里插入图片描述
选择自己的应用:
在这里插入图片描述
可以正常打开新建的ThirdActivity

2、用系统键盘拨号

(1)代码示例

修改监听器:

public void onClick(View v) {
	// Toast.makeText(FirstActivity.this, "我被点击啦!", LENGTH_SHORT).show();
	// finish();
	// Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
	// Intent intent = new Intent("com.example.day01_empty.ACTION_START");
	// intent.addCategory("com.example.day01_empty.my_category");
	// Intent intent = new Intent(Intent.ACTION_VIEW);
	// intent.setData(Uri.parse("http://bilibili.com"));
	Intent intent = new Intent(Intent.ACTION_DIAL);
	intent.setData(Uri.parse("tel:10086"));
	startActivity(intent);
}
  • 倒数第四行:指定IntentactionIntent.Action_DIAL,也是内置动作
  • 倒数第三行:指定协议是tel,号码是10086,经过Uri.parse()解析后传入setData(),该Intent就记录了此意图
  • 倒数第二行:启动该Intent
(5)运行实例

点击按钮后自动跳转到拨号界面,并显示号码
在这里插入图片描述

五、附:个人理解

个人理解未经过任何实践,只是为了以后自己看笔记写的,仅作参考

  • 显示Intent多用于直接打开指定活动,最常做一些应用内跳转的功能
  • 隐式Intent用来响应支持的活动,如果只有一个活动支持,就直接打开,如果有多个活动,那么操作系统就会询问了。这个功能可以做最常用的“通过某app打开文章”或者“分享到”的功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值