Android详细教程(基础篇):二十六、Android组件通信,Intent详解、消息机制(Message、Handler、Looper)详解、 PendingIntent详解

Android组件通信

9.1. Intent

Intent 是一个将要执行的动作的抽象的描述,一般来说是作为参数来使用,由Intent来协助完成android各个组件之间的通讯。比如说调用startActivity()来启动一个activity,或者由broadcaseIntent()来传递给所有感兴趣的BroadcaseReceiver, 再或者由startService()/bindservice()来启动一个后台的service.所以可以看出来,intent主要是用来启动其他的activity 或者service,所以可以将intent理解成activity之间的粘合剂。

9.1.1. Intent的构造函数

public class Intent extends Object implements Parcelable Cloneable

java.lang.Object

   

android.content.Intent

 

Constructors:

Intent()

Create an empty intent.

Intent(Intent o)

Copy constructor.

Intent(String action)

Create an intent with a given action.

Intent(String action, Uri uri)

Create an intent with a given action and for a given data url.

Intent(Context packageContext, Class<?> cls)

Create an intent for a specific component.

Intent(String action, Uri uri, Context packageContext, Class<?> cls)

Create an intent for a specific component with a specified action and data.

公共构造函数:

1、Intent() 空构造函数

2、Intent(Intent o) 拷贝构造函数

3、Intent(String action) 指定action类型的构造函数

4、Intent(String action, Uri uri) 指定Action类型和Uri的构造函数,URI主要是结合程序之间的数据共享ContentProvider

5、Intent(Context packageContext, Class<?> cls) 传入组件的构造函数,也就是上文提到的

6、Intent(String action, Uri uri, Context packageContext, Class<?> cls) 前两种结合体

Intent有六种构造函数,3、4、5是最常用的,并不是其他没用!

Intent(String action, Uri uri) 的action就是对应在AndroidMainfest.xml中的action节点的name属性值。在Intent类中定义了很多的Action和Category常量。

示例:

Intent intent = new Intent(Intent.ACTION_EDIT, null);

startActivity(intent);

代码是用了第四种构造函数,只是uri参数为null。执行此代码的时候,系统就会在程序主配置文件AndroidMainfest.xml中寻找

<action android:name="android.intent.action.EDIT" />对应的Activity,如果对应有多个activity具有<action android:name="android.intent.action.EDIT" />此时就会弹出一个dailog选择Activity。

9.1.2. Activity对Intent的支持

Activity程序支持的Intent操作方法。

9.1.3. Intent的构成

9.1.3.1. Action-动作

       用来指明要实施的动作是什么,比如说ACTION_VIEW, ACTION_EDIT等。具体的可以查阅android SDK-> reference中的Android.content.intent类,里面的constants中定义了所有的action。

相当于通过Activity制定要操作的类型

9.1.3.2. Data-数据

      Data 要传递的具体数据,一般由一个Uri变量来表示,下面是一些简单的例子:

ACTION_VIEW content://contacts/1 //显示identifier为1的联系人的信息。

ACTION_DIAL content://contacts/1 //给这个联系人打电话

9.1.3.3. Type-数据类型

       显式指定Intent的数据类型(MIME)。一般Intent的数据类型能够根据数据本身进行判定,但是通过设置这个属性,可以强制采用显式指定的类型而不再进行推导。

9.1.3.4. Category-类别

       这个选项指定了将要执行的这个action的其他一些额外的信息,例如 LAUNCHER_CATEGORY 表示Intent 的接受者应该在Launcher中作为顶级应用出现;而ALTERNATIVE_CATEGORY表示当前的Intent是一系列的可选动作中的一个,这些动作可以在同一块数据上执行。具体同样可以参考android SDK-> reference中的Android.content.intent类。以前我也写过一篇于category有关的文章,点击这里可以查看。

9.1.3.5. component-组件

       指定Intent的的目标组件的 类名称。通常 Android会根据Intent 中包含的其它属性的信息,比如action、data/type、category进行查找,最终找到一个与之匹配的目标组件。但是,如果 component这个属性有指定的话,将直接使用它指定的组件,而不再执行上述查找过程。指定了这个属性以后,Intent的其它所有属性都是可选的。

9.1.3.6. extras-附加信息

       是其它所有附加信息的集合。使用extras可以为组件提供扩展信息,比如,如果要执行“发送电子邮件”这个动作,可以将电子邮件的标题、正文等保存在extras里,传给电子邮件发送组件。

 

9.1.4. Android中页面跳转以及传值的几种方式!

1、页面跳转及传值:

Activity跳转与传值,主要是通过Intent类来连接多个Activity,通过Builder类来传递数据。

最常见最一般的页面跳转代码,很简单,如下:

Intent intent = new Intent(A.this, B.class); 

startActivity(intent); 

 

也可以这样写:

Intent intent = new Intent(); 

intent.setClass(A.this, B.class); 

startActivity(intent); 

 

只要这两句,就可以实现从A页面跳转到B页面了。  (A、B均继承自Activity)

有的时候,在跳转页面时还需要传递数据,这个时候如何做呢?

如果数据比较少,比如只要传一个名字,那么只要加一句"intent.putExtra("Name", "feng88724");"即可,代码如下:

Intent intent = new Intent(); 

intent.setClass(A.this, B.class); 

intent.putExtra("Name", "feng88724"); 

startActivity(intent); 

 

如果数据比较多,就需要使用 Bunder类了,代码如下: (说明直接看注释)

Intent intent = new Intent(A.this, B.class); 

/* 通过Builder对象存储需要传递的数据 */ 

Builder builder = new Builder (); 

/*字符、字符串、布尔、字节数组、浮点数等等,都可以传*/ 

builder.putString("Name", "feng88724"); 

builder.putBoolean("Ismale", true); 

/*builder对象加给Intent*/ 

intent.putExtras(builder); 

startActivity(intent); 

 

以上我们讲的都是如何进行页面跳转及数据传递,那么在另一个页面B上,应该如何接收数据呢?

9.1.5. 接受Intent传来的值:

在A页面上是以Builder封装了对象,自然在B页面也是以Builder的方式来解开封装的数据。主要通过

"getIntent().getExtras()"方法来获取Builder对象,然后再从Builder中获取数据。 也可以通过

" this.getIntent().getStringExtra("Name");"方法直接从Intent中获取数据。

从Builder获取数据的代码

@Override 

 public void onCreate(Bundle savedInstanceState) { 

        super.onCreate(savedInstanceState); 

        /*加载页面*/ 

     setContentView(R.layout.main); 

        /*获取Intent中的Builder对象*/ 

        Builder builder = this.getIntent().getExtras(); 

        /*获取Builder中的数据,注意类型和key*/ 

        String name = bunder.getString("Name"); 

        boolean ismale = builder.getBoolean("Ismale"); 

9.1.6. 通过Intent跳转到另一页面之后,再返回到之前的页面: 

有时,在页面跳转之后,需要返回到之前的页面,同时要保留用户之前输入的信息,这个时候该怎么办呢?

在页面跳转后,前一个Activity已经被destroy了。如果要返回并显示数据,就必须将前一个Activity再次唤醒,

同时调用某个方法来获取并显示数据。

要实现这个效果,需要做以下几步:

1. 首先,从A页面跳转到B页面时,不可以使用"startActivity()"方法,而要使用"startActivityForResult"方法。

2. 在A页面的Activity中,需要重写"onActivityResult"方法

 /*这里的requestCode用来标识某一个调用,一般由我们定义一个常量。

resultCode是返回代码,同样用来标识一个返回类型,而data则是它要返回的参数。*/

@Override 

protected void onActivityResult(int resultCode,Intent data){ 

    switch(requestCode){ 

    case RESULT_OK: 

        /*取得来自B页面的数据,并显示到画面*/ 

       Builder builder = data.getExtras(); 

         

        /*获取Builder中的数据,注意类型和key*/ 

        String name = bunder.getString("Name"); 

        boolean ismale = bunder.getBoolean("Ismale"); 

    } 

 

  1. 在B页面上加一个返回按钮,并在事件写如下代码:

 /*给上一个Activity返回结果*/ 

B.this.setResult(RESULT_OK,intent); 

/*结束本Activity*/ 

B.this.finish(); 

 

 

9.1.7. Intent的解析

 应用程序的组件为了告诉Android自己能响应、处理哪些隐式Intent请求,可以声明一个甚至多个Intent Filter。每个Intent Filter描述该组件所能响应Intent请求的能力——组件希望接收什么类型的请求行为,什么类型的请求数据。比如之前请求网页浏览器这个例子中,网页浏览器程序的Intent Filter就应该声明它所希望接收的Intent Action是WEB_SEARCH_ACTION,以及与之相关的请求数据是网页地址URI格式。如何为组件声明自己的Intent Filter? 常见的方法是在AndroidManifest.xml文件中用属性< Intent-Filter>描述组件的Intent Filter。

  前面我们提到,隐式Intent(Explicit Intents)和Intent Filter(Implicit Intents)进行比较时的三要素是Intent的动作、数据以及类别。实际上,一个隐式Intent请求要能够传递给目标组件,必要通过这三个方面的检查。如果任何一方面不匹配,Android都不会将该隐式Intent传递给目标组件。接下来我们讲解这三方面检查的具体规则。

9.1.7.1. 动作匹配-action

 <intent-filter>元素中可以包括子元素< action>,比如:

<intent-filter>

       <action android:name=”com.example.project.SHOW_CURRENT” />

       <action android:name=”com.example.project.SHOW_RECENT” />

       <action android:name=”com.example.project.SHOW_PENDING” />

</intent-filter>

    一条< intent-filter>元素至少应该包含一个<action>,否则任何Intent请求都不能和该<intent-filter>匹配。如果Intent请求的Action和< intent-filter>中某一条<action>匹配,那么该Intent就通过了这条<intent-filter>的动作匹配。如果Intent请求或<intent-filter>中没有说明具体的Action类型,那么会出现下面两种情况。

(1) 如果<intent-filter>中没有包含任何Action类型,那么无论什么Intent请求都无法和这条<intent- filter>匹配;

(2) 反之,如果Intent请求中没有设定Action类型,那么只要< intent-filter>中包含有Action类型,这个 Intent请求就将顺利地通过< intent-filter>的行为测试。

9.1.7.2. 类别匹配

  <intent-filter>元素可以包含< category>子元素,比如:

<intent-filter>

      ....

      <category android:name=”android.Intent.Category.DEFAULT” />

      <category android:name=”android.Intent.Category.BROWSABLE” />

</intent-filter>

 

   

  只有当Intent请求中所有的Category与组件中某一个IntentFilter的<category>完全匹配时,才会让该 Intent请求通过匹配,IntentFilter中多余的<category>声明并不会导致匹配失败。一个没有指定任何类别的 IntentFilter仅仅只会匹配没有设置类别的Intent请求。

9.1.7.3. 数据匹配

    数据在< intent-filter>中的描述如下:

<intent-filter>

     ...

     <data android:type=”video/mpeg” android:scheme=”http” . . . />

     <data android:type=”audio/mpeg” android:scheme=”http” . . . />

</intent-filter>

  元素指定了希望接受的Intent请求的数据URI和数据类型,URI被分成三部分来进行匹配:scheme、 authority和path。其中,用setData()设定的Inteat请求的URI数据类型和scheme必须与IntentFilter中所指定的一致。若IntentFilter中还指定了authority或path,它们也需要相匹配才会通过测试。

9.1.7.4. 简单例子说明

  讲解完Intent基本概念之后,接下来我们就使用Intent激活Android自带的电话拨号程序,通过这个实例你会发现,使用Intent并不像其概念描述得那样难。最终创建Intent的代码如下所示。

Intent i = new Intent(Intent.ACTION_DIAL,Uri.parse(”tel://13800138000″));

创建好Intent之后,你就可以通过它告诉Android希望启动新的Activity了。startActivity(i);

9.1.8. 实例:

    1、调用web浏览器

Java代码

Uri myBlogUri = Uri.parse("http://kuikui.javaeye.com");

  returnIt = new Intent(Intent.ACTION_VIEW, myBlogUri);

 

  2、地图

Java代码

Uri mapUri = Uri.parse("geo:38.899533,-77.036476");

returnIt = new Intent(Intent.ACTION_VIEW, mapUri);

 

  3、调拨打电话界面

Java代码

Uri telUri = Uri.parse("tel:100861");

returnIt = new Intent(Intent.ACTION_DIAL, telUri)

  4、直接拨打电话

Java代码

Uri callUri = Uri.parse("tel:100861");

returnIt = new Intent(Intent.ACTION_CALL, callUri);

  5、卸载

Java代码

Uri uninstallUri = Uri.fromParts("package", "xxx", null);

returnIt = new Intent(Intent.ACTION_DELETE, uninstallUri);

   6、安装

Java代码

Uri installUri = Uri.fromParts("package", "xxx", null);

returnIt = new Intent(Intent.ACTION_PACKA

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值