Android Intent概览

一、概念

Intent直译过来的意思为意图,它可以理解为一个信使,用来进行组件间的通信。它的主要用途为以下三个:

1.启动Activity

通过 startActivity()启动一个Activity,并且Intent中可以附带需要的数据给目标Activity,如果期待从Acitivty中获取返回值,需要通过startActivityForResult()启动Activity,在onActivityResult()中获取回调的值;

2.启动服务

通过Context.startService()启动一个服务,或者通过Context.bindService()和后台服务交互;

3.发送广播

通过广播方法Context.sendBroadcasts() / Context.sendOrderedBroadcast() / Context.sendStickyBroadcast()发给Broadcast Receivers;

二、类型

1.显式Intent

通过提供指定目标应用的包名或者组件名的Intent为显式Intent。通常在自已的应用内部使用显式Intent,因为你可以明确知道自已要跳转的Activity或者服务。显式Intent常见定义如下:

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

Intent intent = new Intent();    
intent.setClass(this, SecondActivity.class);  
//intent.setClassName(this, "com.example.app.SecondActivity");  
//intent.setClassName(this.getPackageName(),"com.example.app.SecondActivity");            
startActivity(intent);  

2.隐示Intent

隐式Intent不指定特定的组件,而是通过设置Intent的Action、Data、Category,然后把Intent交给系统,让系统根据每个组件的intent-filter规则来筛选出匹配的组件。

三、Intent组成

Intent主要属性有ComponentName、Action、Data、Category、Extra和Flag,其中前四个是用来确定跳转什么组件,Extra是用来携带数据的,Flag用来指定启动组件的一些行为。

1. ComponentName

要启动的组件名称,显式组件的标志,如果不指定的话则为一个隐式组件。可以使用目标组件的完全限定类名指定此对象,其中包括应用的软件包名称。例如,com.example.ExampleActivity。可以使用 setComponent()、setClass()、setClassName(),或 Intent 构造函数设置组件名称。

2. Action

Action为一个字符串,表示要执行的动作。系统默认定义了一些Action,如文章最后的例子中几乎都是使用系统定义的action。我们也可以自己定义Action,指定Action代码如下:

Intent intent = new Intent()
intent.setAction("myapp.action.test1")
//或者
Intent intent = new Intent("myapp.action.test1");

Action的匹配规则如下:

  1. 如果<intent-filter>标签中有多个<action/>,那么Intent请求的Action,只要匹配其中的一条<action/>就可以通过了这条<intent-filter>的动作测试。
  2. 如果<intent-filter>中没有包含任何<action/>,那么无论什么Intent请求都无法和这条<intent-filter>匹配,也就是说<intent-filter>必须有一个action才有意义。
  3. 如果Intent请求中没有设定Action(动作),那么这个Intent请求就将顺利地通过<intent-filter>的动作测试(前提是<intent-filter>中必须包含有<action/>,否则与第二条冲突)。
3. Category

Intent中可以附加的额外信息,为一个字符串,一个Intent可以添加多个Category,也可以不添加。当一个Intent的Action可以匹配多个Intent-Filter时,会根据Category继续去匹配。
注意:Android 会自动将 CATEGORY_DEFAULT 类别应用于传递给 startActivity()和 startActivityForResult() 的所有隐式 Intent。如需 Activity 接收隐式 Intent,则必须将 "android.intent.category.DEFAULT" 的类别包括在其 Intent 过滤器中。
Category匹配规则如下:
Intent中的所有的Category都能在<intent-filter>中匹配时,才会通过匹配测试,<intent-filter>中多于的category不会影响匹配。如下面的Intent可以和GuideActivity匹配,但是如果<intent-filter>中去掉除了cate3的任何一条,都不会通过匹配测试。

      var intent = Intent("com.action")
      intent.addCategory("cate1")
      intent.addCategory("cate2")
      startActivity(intent)

      <activity
            android:name=".view.splash.GuideActivity"
            android:label="引导页"
            android:screenOrientation="portrait" >
            <intent-filter>
                <action android:name="com.action" />
                <category android:name="cate1" />
                <category android:name="cate2" />
                <category android:name="cate3" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>

4. Extra (数据大小)

可以携带的数据。可以通过putExtra(key, value)的形式添加数据,其实数据是放到一个Bundle对象中的,支持基础数据类型、String、Serializable、Parcelable和对应的数组类型,还支持ArrayList和对应的String、Parcelable、Integer和CharSequence四种泛型。
因为Intent支持跨进程通信,所以Extra可支持的最大数据量也就依赖于跨进程通信数据的大小。Android跨进程通信主要使用Binder,它支持的大小为将近1M, 源码 定义大小为#define BINDER_VM_SIZE ((1*1024*1024) - (4096 *2)),但是这个内存是共享的,官方文档中也说了The Binder transaction buffer has a limited fixed size, currently 1MB, which is shared by all transactions in progress for the process. 我自己测试和看网上其他人的分析,一般不建议超过500K,当然这个值没有科学依据,数据比较大的话可以考虑使用ContentProvider。下面盗张图解释Binder数据传输,更多详细可以看关于Binder的文章。

进程间数据传输

常见用法

拨打电话

Uri uri = Uri.parse("tel:10010");
Intent intent = new Intent(Intent.ACTION_DIAL, uri);
startActivity(intent);

发送短信

Uri uri = Uri.parse("smsto:10010");
Intent intent = new Intent(Intent.ACTION_SENDTO, uri);
intent.putExtra("sms_body", "Hello");
startActivity(intent);

通过浏览器打开网页

Uri uri = Uri.parse("https://www.baidu.com");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);

发送电子邮件

Uri uri = Uri.parse("mailto:someone@domain.com");
Intent intent = new Intent(Intent.ACTION_SENDTO, uri);
startActivity(intent);

显示地图

Uri uri = Uri.parse("geo:39.9,116.3");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);

播放媒体

Intent intent = new Intent(Intent.ACTION_VIEW);
Uri uri = Uri.parse("file:///sdcard/foo.mp3");
intent.setDataAndType(uri, "audio/mp3");
startActivity(intent);

Uri uri = Uri.withAppendedPath(MediaStore.Audio.Media.INTERNAL_CONTENT_URI, "1");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);

选择图片

Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
startActivityForResult(intent, 2);

打开摄像头拍照

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, 1);

安装程序

String fileName = Environment.getExternalStorageDirectory() + "/myApp.apk";   
Intent intent = new Intent(Intent.ACTION_VIEW);   
intent.setDataAndType(Uri.fromFile(new File(fileName)),
"application/vnd.android.package-archive");   
startActivity(intent);  

卸载程序

Uri uri = Uri.parse("package:" + packageName);   
Intent intent = new Intent(Intent.ACTION_DELETE, uri);
startActivity(intent);

跳转设置页面

Intent intent = new Intent(android.provider.Settings.ACTION_SETTINGS);
startActivity(intent);

参考文档:

https://developer.android.com/guide/components/intents-filters
https://blog.csdn.net/andie_guo/article/details/9271973
//www.greatytc.com/p/67d99a82509b
https://blog.csdn.net/wenzhi20102321/article/details/52876648

http://androidxref.com/7.0.0_r1/xref/frameworks/native/libs/binder/ProcessState.cpp

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容