Android Api Guid 之App Components 笔记

1 每个Android应用程序会被分给一个 linux帐号 usrer ID
2 每个Android应用程序被分配一个 VM
3 Application Component
  共四个组件:
  Activity : 参照博客有关 Activity生命周期的文章(<activity></activity>)
  Service : 参照博客有关Service的文章(<service></service>)
  ContentProvider : 参照博客有关ContentProvider的文章, 难点为 URI及MIME (<provider></provider>)
  BroadcastReceiver : 实现 onReceiver 方法(<receiver></receiver>)
 
  其中 Activity/Service/BraodcastReiver 都可以通过 Intent 激活,相关方法为
  Context.startActivity()/Activity.startActivityForResult()
  Context.startService()/Context.bindService()
  Context.sendBroadcast()/sendOrderedBroadcast()/sendStickyBroadcast()
 
  另外 ContentProvider通过 Context.getContentResolver() 激活
  一个BroadcastReceiver可以不在manifest.xml中注册,而是通过 Context.registerReceiver() 完成.
 
4 一些常识
manifest.xml中可以用的元素有:
<support-screens>声明自己应用程序支持的屏幕尺寸
<uses-configuratio> 声明 input configuration
<uses-feature> 声明 a camera, a light sensor, bluetooth, a certain version of OpenGL, or the fidelity of the touchscreen
<uses-sdk> 声明 支持的API等级

你的 animations, menus, styles, colors, and the layout of activity user interfaces 都应该在xml文件中定义
5 Intent及Intent Filter
 一个Intent对象可以设置的字段:
  1> Component: 如果给ntent指定一个明确的Component,可以用 setComponent,setClass,setClassName,getComponent来设置和获取组件.
  2> Action: 一个Action就是一个字符串,Intent内部定义了一些Action常量即字符串常量.Action能够决定Data和Extra,就像函数名指
     定了函数参数和返回值一样. 所以定义自己的Action字符串时最好也想好需要传递的Data和Extra. 可通过setAction/getAction设置.
  3> Data: 即一个URI,如一个ACTION_EDIT需要一个URI来指定需要编辑的文档. URI从一定程度上能够指定MIME Type.
     可以用 setData/setType/getData/getType操作.
  4> Category: A string containing additional information about the kind of component that should handle the intent.
     用来标识一个组件的种类,注意种类二字 addCaterogy/removeCategory/getCategories操作
  5> Extras: 一个Action可以跟一个Data,同时一个Action也可以和一个Extra配合,如
     an ACTION_TIMEZONE_CHANGED intent has a "time-zone" extra that identifies the new time zone,
     and ACTION_HEADSET_PLUG has a "state" extra indicating whether the headset is now plugged in or unplugged,
     as well as a "name" extra for the type of headset. If you were to invent a SHOW_COLOR action, the color value
     would be set in an extra key-value pair.
     可以用 put.../set...来设置(其实是设置时Bundle里),也可以用putExtras/getExtras 直接传递Bundle对象.
  6> Flag: Flags of various sorts. Many instruct the Android system how to launch an activity (for example, which task the activity should belong to)
     and how to treat it after it's launched (for example, whether it belongs in the list of recent activities).
     All these flags are defined in the Intent class.
  解发google开发的应用程序(browser,dailer,google map等),可以参考file:///D:/Android/android-sdk/docs/guide/appendix/g-app-intents.html
 
  Intent解析:
  //一个没有filter的组件只能接收explicit intent.
  If a component does not have any intent filters, it can receive only explicit intents.
  A component with filters can receive both explicit and implicit intents.
  在进行filter匹配时,仅仅Intent三个字段参加,Extra和Flag不参加:
  action
  data (both URI and data type)
  category
  一个组件可以有多组 <intent-filter> 只要Intent能通过其中一组即可. 一个explicit intent能够触发一个组件,
  不管这个组件有多少<intent-filter>.
 
  <intent-filter> 标签对应 IntentFilter 类, IntentFilter类主要用于 Context.registerReceiver() 方法,因为
  一个BroadcastReceiver可能没有在manifest.xml中注册.
 
  在测试 intent-filter 必须对 action,data,category都进行测试,有一个通不过则此 intent-filter即不能通过.
  1> 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,则所有的implicit intent都不能通过;
  相反如一个 Intent对象如果没有声明action,则可以通过<intent-filter>的 action 这一项(filter至少包含一个action).

  2> category测试
  一般情况下一个 Intent只有一个action,但却可以有多个category,所以category的测试通过规则略有不同.
  一个Intent中所含的所有category都通过<category>才算通过,当然<category>可以包含更多的category,但决不能
  少于 Intent 中所含的.
  一个例外情况是: Cont
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值