只有一个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.java
和 AndroidManifest.xml
不动就好。
一、啥是Intent
Intent
是Android
程序中各组件进行交互的一种方式,它指明了将执行的动作,还可以在不同组件间传递数据。通常用来启动活动、启动服务、发送广播等。
通常分为显式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
并未明确指出启动哪个活动,而是通过action
和catagory
等信息,让系统分析这个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
中指定的action
和category
时,该活动才能响应该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);
}
- 倒数第四行,指定
action
是Intent.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);
}
- 倒数第四行:指定
Intent
的action
是Intent.Action_DIAL
,也是内置动作 - 倒数第三行:指定协议是
tel
,号码是10086
,经过Uri.parse()
解析后传入setData()
,该Intent
就记录了此意图 - 倒数第二行:启动该
Intent
(5)运行实例
点击按钮后自动跳转到拨号界面,并显示号码
五、附:个人理解
个人理解未经过任何实践,只是为了以后自己看笔记写的,仅作参考
- 显示Intent多用于直接打开指定活动,最常做一些应用内跳转的功能
- 隐式Intent用来响应支持的活动,如果只有一个活动支持,就直接打开,如果有多个活动,那么操作系统就会询问了。这个功能可以做最常用的“通过某app打开文章”或者“分享到”的功能。