Android学习笔记6:使用Intent1

版权声明:本文为博主原创文章,未经博主允许不得转载。个人主页:http://lhy9.com https://blog.csdn.net/u011134961/article/details/51427677

《第一行代码——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.

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.

显式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

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,并帮我们找出合适的活动去启动。

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

在标签下配置内容,指定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。

我们在中再添加一个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对象传递进去。

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

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

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

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

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

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

举个例子:
example.PNG

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

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

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

在标签中我们通过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。

猜你喜欢

转载自blog.csdn.net/u011134961/article/details/51427677
今日推荐