Android 开发文档 程序基础——Activating components: intents
来源:互联网 发布:广西财经学院网络 编辑:程序博客网 时间:2024/06/10 04:36
前面说的Activities,Services,Broadcast receivers,Content providers都属于程序组件Application Components。
而intents则属于激活组件Activating components。
Content providers在接受到来自ContentResolver的请求时被激活,而其他三个组件则是被异步消息intents激活。Intent是一个持有消息内容的Intent对象。作用于activities 和 services时,intent命名被请求的动作,指定数据的URI以便操作。例如传达一个请求给activity来呈现一个图片或者编辑文字。作用于broadcast receivers时,它命名被广播的动作,例如告诉相关的程序照相键被按了。
针对不同的组件有不同的方法来激活:
activity通过传递Intent对象到Context.startActivity()或者Activity.startActivityForResult()来运行或者执行一些新内容。如果要看正在反应中的activity是由什么intent引起的,可以调用它的getIntent()。android系统中调用activity的onNewIntent()来传递给之后的 intents。
一个activity经常会导致另一个的运行。如果希望被其运行的activity返回一个结果,那么就必须调用startActivityForResult()而不是startActivity()。
service的运行或者接受新指令是通过传递intent给Context.startService()。android调用service的onStart()并传递一个intent对象给它。
类似的,intent可以被传递给Context.bindService()来确立调用中的组件和目标service之间的联系。service通过onBind()方法中接受intent对象。如果service没有启动,bindService()可以选择启动它。
程序可以通过传递Intent对象给例如Context.sendBroadcast(), Context.sendOrderedBroadcast(), Context.sendStickyBroadcast()来初始化一个broadcast。android会把intent传递给所有相关的broadcast receivers,通过调用它们的onReceive()。
原文
Activating components: intents
Content providers are activated when they’re targeted by a request from a ContentResolver. The other three components — activities, services, and broadcast receivers — are activated by asynchronous messages called intents. An intent is an Intent object that holds the content of the message. For activities and services, it names the action being requested and specifies the URI of the data to act on, among other things. For example, it might convey a request for an activity to present an image to the user or let the user edit some text. For broadcast receivers, the Intent object names the action being announced. For example, it might announce to interested parties that the camera button has been pressed.
There are separate methods for activating each type of component:
- An activity is launched (or given something new to do) by passing an Intent object to Context.startActivity() or Activity.startActivityForResult(). The responding activity can look at the initial intent that caused it to be launched by calling its getIntent() method. Android calls the activity’s onNewIntent() method to pass it any subsequent intents.One activity often starts the next one. If it expects a result back from the activity it’s starting, it calls startActivityForResult() instead of startActivity(). For example, if it starts an activity that lets the user pick a photo, it might expect to be returned the chosen photo. The result is returned in an Intent object that’s passed to the calling activity’s onActivityResult() method.
- A service is started (or new instructions are given to an ongoing service) by passing an Intent object to Context.startService(). Android calls the service’s onStart() method and passes it the Intent object.Similarly, an intent can be passed to Context.bindService() to establish an ongoing connection between the calling component and a target service. The service receives the Intent object in an onBind() call. (If the service is not already running, bindService() can optionally start it.) For example, an activity might establish a connection with the music playback service mentioned earlier so that it can provide the user with the means (a user interface) for controlling the playback. The activity would call bindService() to set up that connection, and then call methods defined by the service to affect the playback.
A later section, Remote procedure calls, has more details about binding to a service.
- An application can initiate a broadcast by passing an Intent object to methods like Context.sendBroadcast(), Context.sendOrderedBroadcast(), and Context.sendStickyBroadcast() in any of their variations. Android delivers the intent to all interested broadcast receivers by calling their onReceive() methods.
For more on intent messages, see the separate article, Intents and Intent Filters.
- Android 开发文档 程序基础——Activating components: intents
- Activating components: intents 待续
- Android 开发文档 程序基础——Shutting down components
- Android SDK Document 框架导读的翻译和注解[6]——Activating components: intents【用Intent激活组件】
- Android官方文档之App Components(Common Intents)
- Android官方文档之App Components(Intents and Intent Filters)
- Android官方文档之App Components(Common Intents)(转载)
- Android 开发文档 程序基础 ——activity组件
- Android 开发文档 程序基础 ——Services组件
- Android 开发文档 程序基础 ——Broadcast receivers组件
- Android 开发文档 程序基础 ——Content providers组件
- Android 开发文档 程序基础——The manifest file
- Android 开发文档 程序基础——Intent filters
- Android 开发文档 程序基础——Activities and Tasks
- android Intents和Intent Filters - 开发文档翻译 - 1
- Android 开发文档 程序基础——Affinities and new tasks
- Android API Guides 之 App Components(2) - Intents and Intent Filters - Common Intents
- Android 程式开发:(二)使用Intents链接Activities —— 2.1基本操作
- Oracle学习笔记(4) 在PLSQL中更改数据和管理事务
- Android 开发文档 程序基础 ——Content providers组件
- Valgrind 相关
- Oracle学习笔记(5) 在PLSQL中使用游标获取数据
- JQuery学习日志三(横向纵向菜单)
- Android 开发文档 程序基础——Activating components: intents
- Android 开发文档 程序基础——Shutting down components
- Oracle学习笔记(6) 动态SQL
- Android 开发文档 程序基础——The manifest file
- Android 开发文档 程序基础——Intent filters
- ListCtrl设置可写
- asp.net跳转页面的三种方法比较
- 网上看到的一篇DNS正向解析失败的问题解决方法
- 强烈推荐:240多个jQuery插件