Android基本的设计理念是鼓励减少组件间的耦合,因此Android提供了Intent,Intent提供了一种通用的消息系统,它允许在你的应用程序与其它的应用程序间传递Intent来执行动作和产生事件。使用Intent可以激活Android应用的三个核心组件:活动、服务和广播接收器.即Android的四大组件中活动、服务和广播接收器、内容提供者中只有内容提供者不是被Intent激活的.Intent可以划分成显式意图和隐式意图。显示意图适合用于应用的内部,速度较快.隐式意图有对应的查找规则,所以速度会相对慢点.更适合应用于应用之间激活组件!!显式意图:显式意图明确指定了Intent应该传递给哪个组件如调用Intent.setComponent(),Intent.setClassName(),Intent.setClass()方法明确指定了组件隐式意图:没有明确指定组件名的Intent为隐式意图.Android系统会根据隐式意图中设置的动作(action),类别(category),数据(URI(包含scheme,host,path,port等)和数据类型(mimeType))找到最合适的组件来处理这个意图.Android系统数据是通过Uri体现的如果Activity的意图过滤器和想激活此Activity的意图对象都没有设置数据的情况下,意图对象中的动作(action)名称和类别必须与Activity的过滤器的动作(action)名称和类别相匹配.所以问题来了:某Activity中的意图过滤器只定义了action<activity android:name=".OtherActivity">  <intent-filter><action android:name="cn.sohu.laoli"/>  </intent-filter></activity>于是我们常常这么做去激活此Activity:Intent intent = new Intent();intent.setAction("cn.sohu.laoli");startActivity(intent);但是这么做是错误的,系统会提示错误:找不到对应组件因为当我们的Intent只设置动作,没有设置类别的时候,在执行intent.setAction("cn.sohu.laoli");时这个方法内部会为Intent设置一个默认的Intent类别即为android.intent.category.DEFAULT.于是这个Intent实际上就设置了动作(action)和类别(category).于是无法匹配此处Activity的意图过滤器.为了匹配需要给此处Activity的意图过滤器加上一个Intent默认的类别.<activity android:name=".OtherActivity">  <intent-filter><action android:name="cn.sohu.laoli"/>        <category android:name="android.intent.category.DEFAULT"/>  </intent-filter></activity>总结:在Intent和Activity中的意图过滤器同样没有设置数据的情况下,只要Intent对象中的动作(action)和类别(category)是Activity中的意图过滤器的动作(action)和类别(category)的子集即可.因为有的情况下Activity中的意图过滤器会定义多个动作(action)和类别(category)意图过滤器的设置:<activity android:name=".OtherActivity">    <intent-filter><action android:name="cn.sohu.laoli"/><category android:name="android.intent.category.laozhang"/><data android:scheme="sh" android:host="www.sohu.cn" android:path="/person"/><data android:mimeType="image/gif"/>   </intent-filter></activity>在此意图过滤器中设置了数据的两个方面:URI(包含scheme,host,path,port等)和数据类型(mimeType)于是这样设置Intent来匹配意图过滤器:Intent intent = new Intent();intent.setAction("cn.sohu.laoli");intent.setDataAndType(Uri.parse("sh://www.sohu.cn/person"), "image/gif");注意:1 不能把数据和类型分开设置.即setType(),setDate()!!!!!!!因为setType()会清除掉setDate()所进行的设置!!!!!  而要同时设置数据和类型方法即setDataAndType()2 如果Intent和Activity中的意图过滤器中的一方设置了数据而另一方没有设置,那么此时是不可能匹配的


更多相关文章

  1. android 手动设置对话框能否消失
  2. Android提高十八篇之自定义Menu(TabMenu)[转]
  3. android:设置背景图片、背景颜色透明
  4. cocos2d-x android 字体的设置
  5. AndroidManifest.xml文件讲解
  6. Android中MaterialSearchView(搜索框)的简单实用
  7. Android(安卓)华为手机音频设置播放倍速mMediaPlayer.getPlaybac
  8. WebView!!!!日记~~
  9. Android手机控制流量那几招

随机推荐

  1. android 时区转换
  2. Android layout layout_weight属性理解
  3. Android 更改 Toast 的默认位置及自定义T
  4. Android Device Monitor无法查看data目录
  5. Android根据屏幕宽度缩放图片
  6. 代码混淆
  7. Android(安卓)Studio gradle 打包之非渠
  8. Android(安卓)手势检测实战 打造支持缩放
  9. Android(安卓)USB-HID controlTransfer()
  10. android 中 unable to start service 错