Android学习笔记6:使用Intent1

《第一行代码——Android》郭霖著java

更多内容,请访问个人我的博客醒岛android

Intent介绍

Intent是Android程序中各组件之间进行交互的一种重要方式,它不只能够指明当前组件想要执行的动做,还能够在不一样组件之间传递数据。Intent通常可被用于启动活动、启动服务、以及发送广播等场景。浏览器

An intent is an abstract description of an operation to be performed. It can be used with startActivity to launch an Activity, broadcastIntent to send it to any interested BroadcastReceiver components, and startService(Intent) or bindService(Intent, ServiceConnection, int) to communicate with a background Service. app

An Intent provides a facility for performing late runtime binding between the code in different applications. Its most significant use is in the launching of activities, where it can be thought of as the glue between activities. It is basically a passive data structure holding an abstract description of an action to be performed.ide

显式Intent

在ActivityTest中再建立一个活动SecondActivity,咱们可使用默认的模板,new→Actyvity→Empty Activity。布局文件自动生成,为activity_second.xml。使用此方法建立Activity,系统会自动生成java文件(默认继承AppCompatActivity类,并已经配置好布局文件),xml布局文件,并在AndroidManifest.xml中为此活动进行注册。在布局中增长一个Button,代码以下:函数

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

显式Intent的使用。Intent有多个构造函数的重载,其中一个是Intent(Context packageContext, Class<?> cls)。这个构造函数接收两个参数,第一个参数Context要求提供一个启动活动的上下文,第二个参数Class则是指定想要启动的目标活动,经过这个构造函数就能够构建出Intent的“意图”。而后咱们应该怎么使用这个Intent呢?Activity类中提供了一个startActivity()方法,这个方法是专门用于启动活动的,它接收一个Intent参数,这里咱们将构建好的Intent传入startActivity()方法就能够启动目标活动了。修改FirstActivity中按钮的点击事件,代码以下所示:typecho

Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
startActivity(intent);

咱们首先构建出了一个Intent,传入FirstActivity.this做为上下文,传入SecondActivity.class做为目标活动,这样咱们的“意图”就很是明显了,即在FirstActivity这个活动的基础上打开SecondActivity这个活动。而后经过startActivity()方法来执行这个Intent。
运行结果以下,若是想从新回到上一个活动,直接按下Back便可。也能够像前文所说的那样给button2添加onClick事件,使用finish()方法。
intent try1.PNG布局

隐式Intent

相比于显式Intent,隐式Intent则含蓄了许多,它并不明确指出咱们想要启动哪个活动,而是指定了一系列更为抽象的action和category等信息,而后交由系统去分析这个Intent,并帮咱们找出合适的活动去启动。ui

什么是合适的活动呢?就是action和category等信息都与咱们的指定相匹配的活动。每一个活动只能指定一个action,可是能够指定多个category。其中,android.intent.category.DEFAULT是一种默认的category,在调用startActivity()方法的时候会自动将这个category添加到Intent中。this

在<activity>标签下配置<intent-filter>内容,指定SecondActivity的action和category信息。以下:

<activity android:name=".SecondActivity" >
    <intent-filter>
        <action android:name="com.example.activitytest.ACTION_START" />
        <category    android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</activity>

修改FirstActivity中按钮的点击事件。

Intent intent = new Intent("com.example.activitytest.ACTION_START");
startActivity(intent);

运行程序,跳转结果以下所示。
intent try1.PNG

下面咱们使用addCategory()方法给intent添加一个category。这里咱们指定一个自定义的category,值为com.example.activitytest.MY_CATEGORY。在startActivity()语句前添加以下代码:

intent.addCategory("com.example.activitytest.MY_CATEGORY");

程序运行错误!logcat中的错误信息以下:
error.PNG

这个错误表示没有活动能够响应咱们的Intent。

咱们在<intent-filter>中再添加一个category的声明,就能够解决这个问题了:

<category android:name="com.example.activitytest.MY_CATEGORY"/>

由此,能够知道,活动中的action和category信息必须与Intent中的action和category信息彻底匹配,才能被启动。

更多隐式Intent的用法

使用隐式Intent,咱们不只能够启动本身程序内的活动,还能够启动其余程序的活动,这使得Android多个应用程序之间的功能共享成为了可能。

下面尝试启动系统浏览器来打开百度首页。咱们在SecondActivity中设置button2的点击事件,而后在点击事件中添加以下代码:

Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("http://www.baidu.com"));
startActivity(intent);

从新运行程序,结果以下所示:
intent try url.PNG

这里咱们首先指定了Intent的action是Intent.ACTION_VIEW,这是一个Android系统内置的动做,其常量值为android.intent.action.VIEW。而后经过Uri.parse()方法,将一个网址字符串解析成一个Uri对象,再调用Intent的setData()方法将这个Uri对象传递进去。

与此对应,咱们还能够在<intent-filter>标签中再配置一个<data>标签,用于更精确地指定当前活动可以响应什么类型的数据。<data>标签中主要能够配置如下内容。

  1. android:scheme 用于指定数据的协议部分,如上例中的http部分。

  2. android:host 用于指定数据的主机名部分,如上例中的www.baidu.com部分。

  3. android:port 用于指定数据的端口部分,通常紧随在主机名以后。

  4. android:path 用于指定主机名和端口以后的部分,如一段网址中跟在域名以后的内容。

  5. android:mimeType 用于指定能够处理的数据类型,容许使用通配符的方式进行指定。

举个例子:
example.PNG

只有<data>标签中指定的内容和Intent中携带的Data彻底一致时,当前活动才可以响应该Intent。不过通常在<data>标签中都不会指定过多的内容,如上面浏览器示例中,其实只须要指定android:scheme为http,就能够响应全部的http协议的Intent了。

下面咱们来新建一个活动,命名为ThirdActivity,使它能响应打开网页的Intent。和新建SecondActivity的步骤相同。最后,打开AndroidManifest.xml,在ThirdActivity的<intent-filter>中配置以下:

<intent-filter>
    <action android:name="android.intent.action.VIEW" />
    <category android:name="android.intent.category.DEFAULT" />
    <data android:scheme="http" />
</intent-filter>

在<data>标签中咱们经过android:scheme指定了数据的协议必须是http协议。运行结果以下:
intent try choose.PNG

系统会自动弹出一个列表,显示了目前可以响应这个Intent的全部程序。

除了http外,咱们还能够指定其余的协议。好比geo表示地理位置、tel表示电话、smsto表示发送短信、mailto发送邮件等等。咱们甚至还能够自定义协议,下面来实践一下:

在AndroidManifest.xml中修改ThirdActivity的data字段以下:

<data android:scheme="myscheme" />

而后修改SecondActivity中button2的点击事件为

Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("myscheme://share"));
startActivity(intent);

运行程序,在SecondActivity点击按钮,成功跳转至ThirdActivity。

相关文章
相关标签/搜索