Intent的用法大致可以分为两种,显式Intent和隐式Intent,我们先来看一下显式Intent如何使用
一:显示Intent
1.创建一个布局activity_second.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/btn2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="BUTTON 2" />
</LinearLayout>
2.先建活动SecondActivity并继承Activity类
public class SecondActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//去除标题
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_second);
}
}
3.千万记住在清单文件中进行注册
4.Intent有多个构造函数的重载,其中一个是Intent(Context packageContext, Class<?> cls)。这个构造函数接收两个参数,第一个参数Context要求提供一个启动活动的上下文,第二个参数Class则是指定想要启动的目标活动,通过这个构造函数就可以构建出Intent的“意图”。
5.修改activity_first.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<Button
android:id="@+id/btn1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="BUTTON1" />
</LinearLayout>
6.修改FirstActivity类
public class FirstActivity extends Activity {
//初始化对象
private Button btn1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//去除标题
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_first);
btn1 = (Button) findViewById(R.id.btn1);
btn1.setOnClickListener(clickLis);
}
private View.OnClickListener clickLis=new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
startActivity(intent);
}
};
}
7.运行如下
二:隐式Intent
1.创建xml文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<Button
android:id="@+id/btn1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="BUTTON1" />
</LinearLayout>
2.在对应的Activity里面编写代码
public class FirstActivity extends Activity {
private Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_first);
btn = (Button) findViewById(R.id.btn1);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("http:www.baidu.com"));
startActivity(intent);
}
});
}
}
3.
这里我们首先指定了
Intent
的
action
是
Intent.ACTION_VIEW,这是一个
Android
系统内置的动作,其常量值为
android.intent.action.VIEW。然后通过
Uri.parse()
方法,将一个网址字符串解析成一个
Uri
对象,再调用
Intent
的
setData()
方法将这个
Uri
对象传递进去。
4.点击按钮后,运行如下
三:data
与此对应,我们还可以在<intent-filter>标签中再配置一个<data>标签,用于更精确地指定当前活动能够响应什么类型的数据。<data>标签中主要可以配置以下内容。
1.android:scheme
用于指定数据的协议部分,如上例中的http部分。
2.android:host
用于指定数据的主机名部分,如上例中的www.baidu.com部分。
3.android:port
用于指定数据的端口部分,一般紧随在主机名之后。
4.android:path
用于指定主机名和端口之后的部分,如一段网址中跟在域名之后的内容。
5.android:mimeType
用于指定可以处理的数据类型,允许使用通配符的方式进行指定
只有<data>标签中指定的内容和Intent中携带的Data完全一致时,当前活动才能够响应该Intent。不过一般在<data>标签中都不会指定过多的内容,如上面浏览器示例中,其实只需要指定android:scheme为http,就可以响应所有的http协议的Intent了。
除了http协议外,我们还可以指定很多其他协议,比如geo表示显示地理位置、tel表示拨打电话。下面的代码展示了如何在我们的程序中调用系统拨号界面。
Intent intent = new Intent(Intent.ACTION_DIAL);
intent.setData(Uri.parse("tel:10086"));