android学习笔记(二)

一、Intent(意图):最简单的应用是调用另一个Activity以及传递一些附加信息,除此之外还可以调用Android提供的各种组件比如:Service、BroadCast Receiver和Content Provider等

Intent可以分为显式Intent和隐式Intent:

显式Intent:明确指出了目标组件的Intent

Intent intent=new Intent();
intent.setClass(MainActivity.this,MyActivity.class);

// 调用Intent对象的setClassName方法,参数为<包名>,<完整类名>  
//intent.setClassName("com.onecm.intent", "com.onecm.intent.MyActivity");  
startActivity(intent);

注意:使用以上的基本方法至少要在Manifest文件中注册Activity名称,否则系统将无法找到它。

<activity android:name="com.onecm.hello.MyActivity"><
/activity>
<!--如果是跳转到别的应用程序,则需要获得权限android:permission=""-->
<activity android:name="com.onecm.hello.MyActivity" android:exported="true">
</activity>


隐式Intent:

Intent intent=new Intent();
intent.setAction("com.onecm.hello");
intent.addCategory(Intent.CATEGORY_DEFAULT);
startActivity(intent);
/* 也可以利用manifest文件中定义好的action名称调用activity,
如果有多个activity的action相同则系统弹出菜单让用户选择,通常action名字为<包名><intent.action><类名>,
也可以自定义  
String actionName = "<包名>.com.onecm.hello";  
Intent intent = new Intent(actionName);  */
在Manifest中加入activity便签,相当于一个白名单


<activity android:name="com.example.hello.MyActivity"> 
<intent-filter> 
 <action android:name="com.onecm.hello"></action><!-- category.DEFAULT 这个必须有种类一般为默认,
否则找不到,只有一个这个活动的activity会报错,如果多个则是没有category属性的被忽略不可见 --> 
<category andorid:name="android.intent.category.DEFAULT">
 </category> 

</intent-filter>
</activity>

二、inter-filter(意图过滤器)

IntentFilter对象负责过滤掉组件无法响应和处理的Intent,只将自己关心的Intent接收进来进行处理。 IntentFilter实行“白名单”管理,即只列出组件乐意接受的Intent,但IntentFilter只会过滤隐式Intent,显式的Intent会直接传送到目标组件。 Android组件可以有一个或多个IntentFilter,每个IntentFilter之间相互独立,只需要其中一个验证通过则可。除了用于过滤广播的IntentFilter可以在代码中创建外其他的IntentFilter必须在AndroidManifest.xml文件中进行声明。

1.动作测试

<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>的行为测试。

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请求。

3.数据测试

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

<intent-filter> 
<data android:type="video/mpeg" android:scheme="http"/>
 <data android:type="audio/mpeg" android:scheme="http"/> 
 </intent-filter>

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





你可能感兴趣的:(intent,intent-filter)