Intent

Android 4大组件之间需要交互,这个时候就有Intent来交互。

Intent是具有相关数据负载的操作。

1.URL

intent.setData(Uri uri);

intent.getData(Uri uri);

图片选择程序, onPageResult返回的intent带回的就是图片ID的uri。

2.Intent过滤器

Intent intent = new Intent(Intent.ACTION_VIEW);

intent.setdata(Uri.prase("http://www.google.com"));

activity.startActivity(intent);

一般情况下,会跳出一个列表,然你选择一个app来执行该段程序,问题是

系统是怎么知道需要哪些程序的呢?

在定义activity或者其他组件的时候,会定义<intent-filter>, 该字段就是用来筛选activity的。

host

mimetype

path

port

pathParent

pathPerfix

scheme

   其中URL由四部分组成:它有四个属性scheme、host、port、path对应于URI的每个部分。

       例如:content://com.wjr.example1:121/files

       scheme部分:content

       host部分:com.wjr.example1

       port部分:121

       path部分:files

intent.settype("video/*"); == mimetype

 <intent-filter>
  <action android:name="android.intent.action.VIEW" />
  <category android:name="android.intent.category.BROWSABLE" />
  <category android:name="android.intent.category.DEFAULT" />
  <data android:scheme="http" />
  <data android:scheme="https" />
  <data android:mimeType="text/html" />
  <data android:mimeType="text/plain" />
  <data android:mimeType="application/xhtml+xml" />
  <data android:mimeType="application/vnd.wap.xhtml+xml" />
  </intent-filter>

3.extra

put:

Intent.putextra(key,value);

...

get:

Bundle bundle = intent.getextras();

4.category

CATEGORY_LAUNCH  该项目可以再启动项中显示

CATEGORY_HOME   该项目可以随系统启动而启动,一般launcher就设为这个

5.ACTION_PICKER

当我们使用ACTION_PICKER来启动activity时,它会根据intent设置的URI来筛选,

当在新activity中选择某个item后,会在原来activity中的OnActivityResult中的intent带回URI。

6.ACTION_GET_CONTENT

 ACTION_GET_CONTENT与ACTION_PICKER类似,只是后者使用URL来过滤,而前者使用mimetype来筛选。

7.PendingIntent

作用就是在activity上下文销毁后,也可以使用intent,最常用的地方就是notification。

原文地址:https://www.cnblogs.com/deman/p/3979645.html