- 浏览: 92198 次
- 性别:
- 来自: 天津
文章分类
最新评论
-
yangmeng_3331:
该用户名已存在 写道如果是字符串,是按照 parse 方法中的 ...
javascript时间戳与日期转换 -
该用户名已存在:
如果是字符串,是按照 parse 方法中的规则进行解析。var ...
javascript时间戳与日期转换 -
yangmeng_3331:
shiyiyue513 写道文中有诸多不妥之处。我是初学者,一 ...
Java中的栈,堆和常量池 -
shiyiyue513:
文中有诸多不妥之处。
Java中的栈,堆和常量池
Android中发送Intent的时候有很多的标志位可以使用。是在做各种各样UI跳转时,熟悉这些标志的作用会给开发过程带来很大的遍历,这几天找时间把这些Flag都看了一遍,顺便翻译了一下,为便于理解,在翻译的时候也加了一些说明性的东西,供自己以后参考用,顺便分享出来与大家一起学习讨论。
可能有些地方会有错误,欢迎大家指正,讨论。
原创翻译,如需转载,请标明出处。
http://blog.csdn.net/javensun/article/details/8700265
其中有几处地方还是存疑状态,有问号和红色标明,这几天有空搞搞清楚。
FLAG_ACTIVITY_BROUGHT_TO_FRONT 默认标志
This flag is not normally set by application code, but set for you by the system as described in the launchMode documentation for the singleTask mode.
通常在应用代码中不需要设置这个FLAG,当launchMode为singleTask时系统会默认设置这个标志。
FLAG_ACTIVITY_CLEAR_TASK 清空任务标志
If set in an Intent passed to Context.startActivity(), this flag will cause any existing task that would be associated with the activity to be cleared before the activity is started. That is, the activity becomes the new root of an otherwise empty task, and any old activities are finished. This can only be used in conjunction with FLAG_ACTIVITY_NEW_TASK.
如果Intent中设置了这个标志,会导致含有待启动Activity的Task在Activity被启动前清空。也就是说,这个Activity会成为一个新的root,并且所有旧的activity都被finish掉。这个标志只能与FLAG_ACTIVITY_NEW_TASK 一起使用。
FLAG_ACTIVITY_CLEAR_TOP 清空任务中在其之上的Activity
If set, and the activity being launched is already running in the current task, then instead of launching a new instance of that activity, all of the other activities on top of it will be closed and this Intent will be delivered to the (now on top) old activity as a new Intent.
For example, consider a task consisting of the activities: A, B, C, D. If D calls startActivity() with an Intent that resolves to the component of activity B, then C and D will be finished and B receive the given Intent, resulting in the stack now being: A, B.
The currently running instance of activity B in the above example will either receive the new intent you are starting here in its onNewIntent() method, or be itself finished and restarted with the new intent. If it has declared its launch mode to be "multiple" (the default) and you have not set FLAG_ACTIVITY_SINGLE_TOP in the same intent, then it will be finished and re-created; for all other launch modes or if FLAG_ACTIVITY_SINGLE_TOP is set then this Intent will be delivered to the current instance's onNewIntent().
This launch mode can also be used to good effect in conjunction with FLAG_ACTIVITY_NEW_TASK: if used to start the root activity of a task, it will bring any currently running instance of that task to the foreground, and then clear it to its root state. This is especially useful, for example, when launching an activity from the notification manager.
See Tasks and Back Stack for more information about tasks.
如果设置了这个标志,并且待启动的Activity已经存在于当前的task中,那就不会再给这个activity新起一个实例,而是将task中在它之上的其它activity全部关闭,然后把Intent作为一个新的Intent传给这个Activity(当前已在栈顶)。
例如,一个task中存在A,B,C,D四个Activity。如果D调用startActivity() 启动B,那么C和D会被finish掉并且B收到这个Intent,最后栈中只有A,B。
上面例子中运行的B activity既可以在onNewIntent()中接收新的Intent,也可以将自己finish掉然后使用新的Intent重启。如果在它的launch mode中设置了"multiple"(默认),并且intent中没有设置 FLAG_ACTIVITY_SINGLE_TOP 标志,那它就会被finish掉然后重新创建。如果是其它的launchMode或者是设置了FLAG_ACTIVITY_SINGLE_TOP 属性,那就会使用现有的实例的OnNewIntent()方法来接受Intent。
这种启动模式也可以与 FLAG_ACTIVITY_NEW_TASK 一起使用:如果用来启动一个任务的root activity,它会将这个任务中现在运行的实例调到前台,然后将任务清空至只有根Activity的状态。这很有用,例如要从通知中心里启动一个Activity时。
FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET 任务重置时将任务中在此标记之后的Activity清空
If set, this marks a point in the task's activity stack that should be cleared when the task is reset. That is, the next time the task is brought to the foreground with FLAG_ACTIVITY_RESET_TASK_IF_NEEDED (typically as a result of the user re-launching it from home), this activity and all on top of it will be finished so that the user does not return to them, but instead returns to whatever activity preceeded it.
This is useful for cases where you have a logical break in your application. For example, an e-mail application may have a command to view an attachment, which launches an image view activity to display it. This activity should be part of the e-mail application's task, since it is a part of the task the user is involved in. However, if the user leaves that task, and later selects the e-mail app from home, we may like them to return to the conversation they were viewing, not the picture attachment, since that is confusing. By setting this flag when launching the image viewer, that viewer and any activities it starts will be removed the next time the user returns to mail.
设置这个标志意味着在activity栈中做一个标记,在Task重置的时候栈就把从标记往上的activity都清除。也就是说,下次这个Task被通过FLAG_ACTIVITY_RESET_TASK_IF_NEEDED调到前台时(通常是由于用户从桌面重新启动),这个activity和它之上的activity都会被finish掉,这样用户就不会再回到他们,而是直接回到在它们之前的activity。
这在应用切换时非常有用。比如,Email应用会需要查看附件,就要调用查看图片的Activity来显示,那这个查看图片的Activity就会成为Email应用任务里的一部分。但是,如果用户离开了Email的任务,过了一会儿由通过Home来选择Email应用,我们会希望它回到查看邮件会话的页面,而不是浏览图片附件的页面,不然就感觉太诡异了。如果在启动查看图片Activity时设置了这个标志,那这个Activity及由它启动的Activity在下一次用户返回邮件时都会被清除。
FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS 不显示在近期任务中
If set, the new activity is not kept in the list of recently launched activities.
如果设置这个标志,这个Activity就不会在近期任务中显示。
FLAG_ACTIVITY_FORWARD_RESULT 转发结果
If set and this intent is being used to launch a new activity from an existing one, then the reply target of the existing activity will be transfered to the new activity. This way the new activity can call setResult(int) and have that result sent back to the reply target of the original activity.
如果Activity A 在启动 Activity B时设置了这个标志,那A的答复目标目标会传递给B,这样一来B就可以通过调用setResult(int) 将返回结果返回给A的答复目标。
简单如下:
O ----startActivityForResult()----> A ----FLAG_ACTIVITY_FORWARD_RESULT----> B
A的答复目标是O,如果A在启动B时使用了这个标志,A就会把答复目标O的信息传递给B,以便B将O作为它的答复目标。此时B调用setResult()时的结果信息都会传递给O,而不会给A。并且此时在A中调用setResult()的内容不会生效。我还没发现使A中setResult()生效的方法。
注意:这个标志不能与startActivityForResult()一起使用。
FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY 从近期任务中启动的标志
This flag is not normally set by application code, but set for you by the system if this activity is being launched from history (longpress home key).
这个标志通常情况下不会通过应用的代码来设置,而是在通过最近任务启动activity时由系统设置的。
FLAG_ACTIVITY_MULTIPLE_TASK Activity可在多任务运行的标志
Do not use this flag unless you are implementing your own top-level application launcher. Used in conjunction with FLAG_ACTIVITY_NEW_TASK to disable the behavior of bringing an existing task to the foreground. When set, a new task is always started to host the Activity for the Intent, regardless of whether there is already an existing task running the same thing.
Because the default system does not include graphical task management, you should not use this flag unless you provide some way for a user to return back to the tasks you have launched.
This flag is ignored if FLAG_ACTIVITY_NEW_TASK is not set.
See Tasks and Back Stack for more information about tasks.
除非你实现了自己的顶级应用启动器,否则不要使用这个标志。与 FLAG_ACTIVITY_NEW_TASK 一起使用可以不再把已存在的任务唤起到前台。 当被设置时,系统总会为Intent的Activity启动一个新的task,而不管是否已经有已存在的任务在做同样的事情。
因为默认系统不包含图形化的任务管理功能,所以除非你给用户提供了返回到已启动任务的方法,否则就不要用这个标志。
如果FLAG_ACTIVITY_NEW_TASK没有设置,则这个标志也被忽略。
FLAG_ACTIVITY_NEW_TASK 尝试在新任务中启动Activity的标志(并不一定就会在新的任务中)
If set, this activity will become the start of a new task on this history stack. A task (from the activity that started it to the next task activity) defines an atomic group of activities that the user can move to. Tasks can be moved to the foreground and background; all of the activities inside of a particular task always remain in the same order. See Tasks and Back Stack for more information about tasks.
This flag is generally used by activities that want to present a "launcher" style behavior: they give the user a list of separate things that can be done, which otherwise run completely independently of the activity launching them.
When using this flag, if a task is already running for the activity you are now starting, then a new activity will not be started; instead, the current task will simply be brought to the front of the screen with the state it was last in. See FLAG_ACTIVITY_MULTIPLE_TASK for a flag to disable this behavior.
This flag can not be used when the caller is requesting a result from the activity being launched.
设置这个标志可以为待启动的Activity创建一个新的任务。一个任务(从启动它的Activity到任务中的下一个Activity)就是用户可以跳转到的Activity的原子群。任务可以在前台与后台之间切换;在某一特定任务之中的所有Activity一直会保持同样的顺序。
这个标志通常被用来呈现一种"laucher"类型的行为:为用户提供一个可单独解决的事情列表,完全独立于启动他们的Activity之外运行。
使用这个标志时,如果有一个任务已经运行了你要启动的Activity,那就不会在创建新的Activity,而是将现有的任务保持之前的状态直接唤到前台。参见FLAG_ACTIVITY_MULTIPLE_TASK这个标志,可以禁用掉这个行为。
这个标志不能在调用者向待启动Activity请求返回结果时使用。
注意:假设A启动B,如果要让B在新的task中创建,要求这两个Activity的taskAffinity不同。也就是说,设置了这个标志后,新启动的activity并非就一定在新的task中创建,如果A和B在属于同一个package,而且都是使用默认的taskAffinity,那B还是会在A的task中被创建。 所以,只有A和B的taskAffinity不同时,设置了这个标志才会使B被创建到新的task。
FLAG_ACTIVITY_NO_ANIMATION 禁用切换动画
If set in an Intent passed to Context.startActivity(), this flag will prevent the system from applying an activity transition animation to go to the next activity state. This doesn't mean an animation will never run -- if another activity change happens that doesn't specify this flag before the activity started here is displayed, then that transition will be used. This flag can be put to good use when you are going to do a series of activity operations but the animation seen by the user shouldn't be driven by the first activity change but rather a later one.
禁用掉系统默认的Activity切换动画。
FLAG_ACTIVITY_NO_HISTORY 不保存Activity的历史状态
If set, the new activity is not kept in the history stack. As soon as the user navigates away from it, the activity is finished. This may also be set with the noHistory attribute.
如果设置这个标志,新的Activity就不会在历史栈中保存。用户一旦离开,这个Activity就会finish掉。也可以使用noHistory属性设置。
FLAG_ACTIVITY_NO_USER_ACTION 不响应onUserLeaveHint方法
If set, this flag will prevent the normal onUserLeaveHint() callback from occurring on the current frontmost activity before it is paused as the newly-started activity is brought to the front.
Typically, an activity can rely on that callback to indicate that an explicit user action has caused their activity to be moved out of the foreground. The callback marks an appropriate point in the activity's lifecycle for it to dismiss any notifications that it intends to display "until the user has seen them," such as a blinking LED.
If an activity is ever started via any non-user-driven events such as phone-call receipt or an alarm handler, this flag should be passed to Context.startActivity, ensuring that the pausing activity does not think the user has acknowledged its notification.
如果设置了这个标志,可以在避免用户离开当前Activity时回调到 onUserLeaveHint(). 通常,Activity可以通过这个回调表明有明确的用户行为将当前activity切出前台。 这个回调标记了activity生命周期中的一个恰当的点,可以用来“在用户看过通知之后”将它们清除,如闪烁LED灯。
如果Activity是由非用户驱动的事件(如电话呼入或闹钟响铃)启动的,那这个标志就应该被传入Context.startActivity,以确保被打断的activity不会认为用户已经看过了通知。
FLAG_ACTIVITY_PREVIOUS_IS_TOP
If set and this intent is being used to launch a new activity from an existing one, the current activity will not be counted as the top activity for deciding whether the new intent should be delivered to the top instead of starting a new one. The previous activity will be used as the top, with the assumption being that the current activity will finish itself immediately.
如果启动Activity时设置了这个标志,那当前这个 Activity 不会被当作顶部的 Activity 来判断是否之后新Intent应该被传给栈顶Activity而不是启动一个新的Activity。之前一个的Activity会被当作栈顶,假定当前的Acitvity会立即自己finish掉。
即 A---> B --->C,若B启动C时用了这个标志位,那在启动时B并不会被当作栈顶的Activity,而是用A做栈顶来启动C。此过程中B充当一个跳转页面。
典型的场景是在应用选择页面,如果在文本中点击一个网址要跳转到浏览器,而系统中又装了不止一个浏览器应用,此时会弹出应用选择页面。在应用选择页面选择某一款浏览器启动时,就会用到这个Flag。然后应用选择页面将自己finish,以保证从浏览器返回时不会在回到选择页面。
经常与FLAG_ACTIVITY_FORWARD_RESULT 一起使用。
FLAG_ACTIVITY_REORDER_TO_FRONT 任务中的Activity顺序重排
If set in an Intent passed to Context.startActivity(), this flag will cause the launched activity to be brought to the front of its task's history stack if it is already running.
For example, consider a task consisting of four activities: A, B, C, D. If D calls startActivity() with an Intent that resolves to the component of activity B, then B will be brought to the front of the history stack, with this resulting order: A, C, D, B. This flag will be ignored if FLAG_ACTIVITY_CLEAR_TOP is also specified.
如果设置了这个标志,而且被启动的Activity如果已经在运行,那这个Activity会被调到栈顶。
比如,一个任务中有4个Activity:A,B,C,D。如果D调用了startActivity() 来启动B时使用了这个标志,那B就会被调到历史栈的栈顶,结果顺序:A,C,D,B,否则顺序会是:A,B,C,D,B。 如果使用了标志 FLAG_ACTIVITY_CLEAR_TOP,那这个FLAG_ACTIVITY_REORDER_TO_FRONT标志会被忽略。
FLAG_ACTIVITY_SINGLE_TOP
If set, the activity will not be launched if it is already running at the top of the history stack.
设置这个标志之后,如果被启动的Activity已经在栈顶,那它就不会被再次启动。
FLAG_ACTIVITY_TASK_ON_HOME 直接返回桌面
If set in an Intent passed to Context.startActivity(), this flag will cause a newly launching task to be placed on top of the current home activity task (if there is one). That is, pressing back from the task will always return the user to home even if that was not the last activity they saw. This can only be used in conjunction with FLAG_ACTIVITY_NEW_TASK.
这个标志可以将一个新启动的任务置于当前的home任务(home activity task)之上(如果有的话)。也就是说,在任务中按back键总是会回到home界面,而不是回到他们之前看到的activity。这个标志只能与FLAG_ACTIVITY_NEW_TASK标志一起用。
比如,A->B->C->D,如果在C启动D的时候设置了这个标志,那在D中按Back键则是直接回到桌面,而不是C。
注意:
只有D是在新的task中被创建时(也就是D的launchMode是singleInstance时,或者是给D指定了与C不同的taskAffinity并且加了FLAG_ACTIVITY_NEW_TASK标志时),使用 FLAG_ACTIVITY_TASK_ON_HOME标志才会生效。
感觉实际使用效果和用 FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK 的效果一样。
FLAG_DEBUG_LOG_RESOLUTION 打开Activity解析的Log开关
A flag you can enable for debugging: when set, log messages will be printed during the resolution of this intent to show you what has been found to create the final resolved list.
可以用来启用调试的标志:设置以后,在intent的处理过程中log信息会打印出来,可以看到都找到了些什么来创建最终的解析列表。
Log输出如下:
04-15 11:19:09.446: V/IntentResolver(398): Resolving type null scheme null of intent Intent { act=android.intent.action.B_Activity flg=0x8 }
04-15 11:19:09.446: V/IntentResolver(398): Action list: [ActivityIntentInfo{42354338 com.example.icsorigintest.B_Activity}]
04-15 11:19:09.446: V/IntentResolver(398): Matching against filter ActivityIntentInfo{42354338 com.example.icsorigintest.B_Activity}
04-15 11:19:09.446: V/IntentResolver(398): Filter matched! match=0x108000
04-15 11:19:09.446: V/IntentResolver(398): Final result list:
04-15 11:19:09.446: V/IntentResolver(398): ResolveInfo{41ee95d0 com.example.icsorigintest.B_Activity p=0 o=0 m=0x108000}
FLAG_EXCLUDE_STOPPED_PACKAGES 排除已停止的包
If set, this intent will not match any components in packages that are currently stopped. If this is not set, then the default behavior is to include such applications in the result.
设置之后,Intent就不会再匹配那些当前被停止的包里的组件。如果没有设置,默认的匹配行为会包含这些被停止的包。
FLAG_FROM_BACKGROUND 后台启动Activity
Can be set by the caller to indicate that this Intent is coming from a background operation, not from direct user interaction.
可以给调用者用来标识这个Intent是来自后台操作,而不是用户的交互行为。
FLAG_GRANT_READ_URI_PERMISSION 给Activity授权
If set, the recipient of this Intent will be granted permission to perform read operations on the Uri in the Intent's data and any URIs specified in its ClipData.When applying to an Intent's ClipData, all URIs as well as recursive traversals through data or other ClipData in Intent items will be granted; only the grant flags of the top-level Intent are used.
如果设置,Intent的接收者会被授予读权限,用来读取Intent中包含的或是在ClipData中指定的Uri。当被用于Intent中的ClipData时,被授予的是Intent中其它ClipData中的所有Uri和它们递归遍历到的Uri的读权限。只有顶级Intent中的授予标志会被使用。
FLAG_INCLUDE_STOPPED_PACKAGES 包含已停止的包
If set, this intent will always match any components in packages that are currently stopped. This is the default behavior when FLAG_EXCLUDE_STOPPED_PACKAGES is not set. If both of these flags are set, this one wins (it allows overriding of exclude for places where the framework may automatically set the exclude flag).
设置之后Intent总是会去匹配那些已被停止的包里的组件。如果没有设置 FLAG_EXCLUDE_STOPPED_PACKAGES 标志,那这个就是默认行为。如果两个标志都被设置,那这个会生效(在框架中一些地方可能会自动设置exclude标志,这些标志可以被覆盖掉)。
FLAG_RECEIVER_FOREGROUND 接受器以前台优先级运行
If set, when sending a broadcast the recipient is allowed to run at foreground priority, with a shorter timeout interval. During normal broadcasts the receivers are not automatically hoisted out of the background priority class.
当发送广播的时候设置了这个标志,会允许接收者以前台的优先级运行,有更短的时间间隔。正常广播的接受者是后台优先级,不会被自动提升。
FLAG_RECEIVER_REGISTERED_ONLY 只调用手动注册的接收器(忽略manifest中声明的)
If set, when sending a broadcast only registered receivers will be called -- no BroadcastReceiver components will be launched.
如果发送广播时设置了这个标志,那只会调用注册了的接收器——BroadcastReceiver组件不会被启动。
FLAG_RECEIVER_REPLACE_PENDING 替换掉等待中的广播
If set, when sending a broadcast the new broadcast will replace any existing pending broadcast that matches it. Matching is defined by Intent.filterEquals returning true for the intents of the two broadcasts. When a match is found, the new broadcast (and receivers associated with it) will replace the existing one in the pending broadcast list, remaining at the same position in the list.
This flag is most typically used with sticky broadcasts, which only care about delivering the most recent values of the broadcast to their receivers.
如果在发送广播时设置了这个标志,那新的广播会替换掉那些已存在的相同广播。相同的定义是通过Intent.filterEquals方法对两个广播的Intent处理返回true。 当匹配到相同的,新的广播和对应的接收器会将待发送的广播列表中已存在的替换掉,在列表中保留同样的位置。
这个标志通常被粘性广播(Sticky Broadcast)使用,只保证将最新的广播的值传递给接收器。
可能有些地方会有错误,欢迎大家指正,讨论。
原创翻译,如需转载,请标明出处。
http://blog.csdn.net/javensun/article/details/8700265
其中有几处地方还是存疑状态,有问号和红色标明,这几天有空搞搞清楚。
FLAG_ACTIVITY_BROUGHT_TO_FRONT 默认标志
This flag is not normally set by application code, but set for you by the system as described in the launchMode documentation for the singleTask mode.
通常在应用代码中不需要设置这个FLAG,当launchMode为singleTask时系统会默认设置这个标志。
FLAG_ACTIVITY_CLEAR_TASK 清空任务标志
If set in an Intent passed to Context.startActivity(), this flag will cause any existing task that would be associated with the activity to be cleared before the activity is started. That is, the activity becomes the new root of an otherwise empty task, and any old activities are finished. This can only be used in conjunction with FLAG_ACTIVITY_NEW_TASK.
如果Intent中设置了这个标志,会导致含有待启动Activity的Task在Activity被启动前清空。也就是说,这个Activity会成为一个新的root,并且所有旧的activity都被finish掉。这个标志只能与FLAG_ACTIVITY_NEW_TASK 一起使用。
FLAG_ACTIVITY_CLEAR_TOP 清空任务中在其之上的Activity
If set, and the activity being launched is already running in the current task, then instead of launching a new instance of that activity, all of the other activities on top of it will be closed and this Intent will be delivered to the (now on top) old activity as a new Intent.
For example, consider a task consisting of the activities: A, B, C, D. If D calls startActivity() with an Intent that resolves to the component of activity B, then C and D will be finished and B receive the given Intent, resulting in the stack now being: A, B.
The currently running instance of activity B in the above example will either receive the new intent you are starting here in its onNewIntent() method, or be itself finished and restarted with the new intent. If it has declared its launch mode to be "multiple" (the default) and you have not set FLAG_ACTIVITY_SINGLE_TOP in the same intent, then it will be finished and re-created; for all other launch modes or if FLAG_ACTIVITY_SINGLE_TOP is set then this Intent will be delivered to the current instance's onNewIntent().
This launch mode can also be used to good effect in conjunction with FLAG_ACTIVITY_NEW_TASK: if used to start the root activity of a task, it will bring any currently running instance of that task to the foreground, and then clear it to its root state. This is especially useful, for example, when launching an activity from the notification manager.
See Tasks and Back Stack for more information about tasks.
如果设置了这个标志,并且待启动的Activity已经存在于当前的task中,那就不会再给这个activity新起一个实例,而是将task中在它之上的其它activity全部关闭,然后把Intent作为一个新的Intent传给这个Activity(当前已在栈顶)。
例如,一个task中存在A,B,C,D四个Activity。如果D调用startActivity() 启动B,那么C和D会被finish掉并且B收到这个Intent,最后栈中只有A,B。
上面例子中运行的B activity既可以在onNewIntent()中接收新的Intent,也可以将自己finish掉然后使用新的Intent重启。如果在它的launch mode中设置了"multiple"(默认),并且intent中没有设置 FLAG_ACTIVITY_SINGLE_TOP 标志,那它就会被finish掉然后重新创建。如果是其它的launchMode或者是设置了FLAG_ACTIVITY_SINGLE_TOP 属性,那就会使用现有的实例的OnNewIntent()方法来接受Intent。
这种启动模式也可以与 FLAG_ACTIVITY_NEW_TASK 一起使用:如果用来启动一个任务的root activity,它会将这个任务中现在运行的实例调到前台,然后将任务清空至只有根Activity的状态。这很有用,例如要从通知中心里启动一个Activity时。
FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET 任务重置时将任务中在此标记之后的Activity清空
If set, this marks a point in the task's activity stack that should be cleared when the task is reset. That is, the next time the task is brought to the foreground with FLAG_ACTIVITY_RESET_TASK_IF_NEEDED (typically as a result of the user re-launching it from home), this activity and all on top of it will be finished so that the user does not return to them, but instead returns to whatever activity preceeded it.
This is useful for cases where you have a logical break in your application. For example, an e-mail application may have a command to view an attachment, which launches an image view activity to display it. This activity should be part of the e-mail application's task, since it is a part of the task the user is involved in. However, if the user leaves that task, and later selects the e-mail app from home, we may like them to return to the conversation they were viewing, not the picture attachment, since that is confusing. By setting this flag when launching the image viewer, that viewer and any activities it starts will be removed the next time the user returns to mail.
设置这个标志意味着在activity栈中做一个标记,在Task重置的时候栈就把从标记往上的activity都清除。也就是说,下次这个Task被通过FLAG_ACTIVITY_RESET_TASK_IF_NEEDED调到前台时(通常是由于用户从桌面重新启动),这个activity和它之上的activity都会被finish掉,这样用户就不会再回到他们,而是直接回到在它们之前的activity。
这在应用切换时非常有用。比如,Email应用会需要查看附件,就要调用查看图片的Activity来显示,那这个查看图片的Activity就会成为Email应用任务里的一部分。但是,如果用户离开了Email的任务,过了一会儿由通过Home来选择Email应用,我们会希望它回到查看邮件会话的页面,而不是浏览图片附件的页面,不然就感觉太诡异了。如果在启动查看图片Activity时设置了这个标志,那这个Activity及由它启动的Activity在下一次用户返回邮件时都会被清除。
FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS 不显示在近期任务中
If set, the new activity is not kept in the list of recently launched activities.
如果设置这个标志,这个Activity就不会在近期任务中显示。
FLAG_ACTIVITY_FORWARD_RESULT 转发结果
If set and this intent is being used to launch a new activity from an existing one, then the reply target of the existing activity will be transfered to the new activity. This way the new activity can call setResult(int) and have that result sent back to the reply target of the original activity.
如果Activity A 在启动 Activity B时设置了这个标志,那A的答复目标目标会传递给B,这样一来B就可以通过调用setResult(int) 将返回结果返回给A的答复目标。
简单如下:
O ----startActivityForResult()----> A ----FLAG_ACTIVITY_FORWARD_RESULT----> B
A的答复目标是O,如果A在启动B时使用了这个标志,A就会把答复目标O的信息传递给B,以便B将O作为它的答复目标。此时B调用setResult()时的结果信息都会传递给O,而不会给A。并且此时在A中调用setResult()的内容不会生效。我还没发现使A中setResult()生效的方法。
注意:这个标志不能与startActivityForResult()一起使用。
FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY 从近期任务中启动的标志
This flag is not normally set by application code, but set for you by the system if this activity is being launched from history (longpress home key).
这个标志通常情况下不会通过应用的代码来设置,而是在通过最近任务启动activity时由系统设置的。
FLAG_ACTIVITY_MULTIPLE_TASK Activity可在多任务运行的标志
Do not use this flag unless you are implementing your own top-level application launcher. Used in conjunction with FLAG_ACTIVITY_NEW_TASK to disable the behavior of bringing an existing task to the foreground. When set, a new task is always started to host the Activity for the Intent, regardless of whether there is already an existing task running the same thing.
Because the default system does not include graphical task management, you should not use this flag unless you provide some way for a user to return back to the tasks you have launched.
This flag is ignored if FLAG_ACTIVITY_NEW_TASK is not set.
See Tasks and Back Stack for more information about tasks.
除非你实现了自己的顶级应用启动器,否则不要使用这个标志。与 FLAG_ACTIVITY_NEW_TASK 一起使用可以不再把已存在的任务唤起到前台。 当被设置时,系统总会为Intent的Activity启动一个新的task,而不管是否已经有已存在的任务在做同样的事情。
因为默认系统不包含图形化的任务管理功能,所以除非你给用户提供了返回到已启动任务的方法,否则就不要用这个标志。
如果FLAG_ACTIVITY_NEW_TASK没有设置,则这个标志也被忽略。
FLAG_ACTIVITY_NEW_TASK 尝试在新任务中启动Activity的标志(并不一定就会在新的任务中)
If set, this activity will become the start of a new task on this history stack. A task (from the activity that started it to the next task activity) defines an atomic group of activities that the user can move to. Tasks can be moved to the foreground and background; all of the activities inside of a particular task always remain in the same order. See Tasks and Back Stack for more information about tasks.
This flag is generally used by activities that want to present a "launcher" style behavior: they give the user a list of separate things that can be done, which otherwise run completely independently of the activity launching them.
When using this flag, if a task is already running for the activity you are now starting, then a new activity will not be started; instead, the current task will simply be brought to the front of the screen with the state it was last in. See FLAG_ACTIVITY_MULTIPLE_TASK for a flag to disable this behavior.
This flag can not be used when the caller is requesting a result from the activity being launched.
设置这个标志可以为待启动的Activity创建一个新的任务。一个任务(从启动它的Activity到任务中的下一个Activity)就是用户可以跳转到的Activity的原子群。任务可以在前台与后台之间切换;在某一特定任务之中的所有Activity一直会保持同样的顺序。
这个标志通常被用来呈现一种"laucher"类型的行为:为用户提供一个可单独解决的事情列表,完全独立于启动他们的Activity之外运行。
使用这个标志时,如果有一个任务已经运行了你要启动的Activity,那就不会在创建新的Activity,而是将现有的任务保持之前的状态直接唤到前台。参见FLAG_ACTIVITY_MULTIPLE_TASK这个标志,可以禁用掉这个行为。
这个标志不能在调用者向待启动Activity请求返回结果时使用。
注意:假设A启动B,如果要让B在新的task中创建,要求这两个Activity的taskAffinity不同。也就是说,设置了这个标志后,新启动的activity并非就一定在新的task中创建,如果A和B在属于同一个package,而且都是使用默认的taskAffinity,那B还是会在A的task中被创建。 所以,只有A和B的taskAffinity不同时,设置了这个标志才会使B被创建到新的task。
FLAG_ACTIVITY_NO_ANIMATION 禁用切换动画
If set in an Intent passed to Context.startActivity(), this flag will prevent the system from applying an activity transition animation to go to the next activity state. This doesn't mean an animation will never run -- if another activity change happens that doesn't specify this flag before the activity started here is displayed, then that transition will be used. This flag can be put to good use when you are going to do a series of activity operations but the animation seen by the user shouldn't be driven by the first activity change but rather a later one.
禁用掉系统默认的Activity切换动画。
FLAG_ACTIVITY_NO_HISTORY 不保存Activity的历史状态
If set, the new activity is not kept in the history stack. As soon as the user navigates away from it, the activity is finished. This may also be set with the noHistory attribute.
如果设置这个标志,新的Activity就不会在历史栈中保存。用户一旦离开,这个Activity就会finish掉。也可以使用noHistory属性设置。
FLAG_ACTIVITY_NO_USER_ACTION 不响应onUserLeaveHint方法
If set, this flag will prevent the normal onUserLeaveHint() callback from occurring on the current frontmost activity before it is paused as the newly-started activity is brought to the front.
Typically, an activity can rely on that callback to indicate that an explicit user action has caused their activity to be moved out of the foreground. The callback marks an appropriate point in the activity's lifecycle for it to dismiss any notifications that it intends to display "until the user has seen them," such as a blinking LED.
If an activity is ever started via any non-user-driven events such as phone-call receipt or an alarm handler, this flag should be passed to Context.startActivity, ensuring that the pausing activity does not think the user has acknowledged its notification.
如果设置了这个标志,可以在避免用户离开当前Activity时回调到 onUserLeaveHint(). 通常,Activity可以通过这个回调表明有明确的用户行为将当前activity切出前台。 这个回调标记了activity生命周期中的一个恰当的点,可以用来“在用户看过通知之后”将它们清除,如闪烁LED灯。
如果Activity是由非用户驱动的事件(如电话呼入或闹钟响铃)启动的,那这个标志就应该被传入Context.startActivity,以确保被打断的activity不会认为用户已经看过了通知。
FLAG_ACTIVITY_PREVIOUS_IS_TOP
If set and this intent is being used to launch a new activity from an existing one, the current activity will not be counted as the top activity for deciding whether the new intent should be delivered to the top instead of starting a new one. The previous activity will be used as the top, with the assumption being that the current activity will finish itself immediately.
如果启动Activity时设置了这个标志,那当前这个 Activity 不会被当作顶部的 Activity 来判断是否之后新Intent应该被传给栈顶Activity而不是启动一个新的Activity。之前一个的Activity会被当作栈顶,假定当前的Acitvity会立即自己finish掉。
即 A---> B --->C,若B启动C时用了这个标志位,那在启动时B并不会被当作栈顶的Activity,而是用A做栈顶来启动C。此过程中B充当一个跳转页面。
典型的场景是在应用选择页面,如果在文本中点击一个网址要跳转到浏览器,而系统中又装了不止一个浏览器应用,此时会弹出应用选择页面。在应用选择页面选择某一款浏览器启动时,就会用到这个Flag。然后应用选择页面将自己finish,以保证从浏览器返回时不会在回到选择页面。
经常与FLAG_ACTIVITY_FORWARD_RESULT 一起使用。
FLAG_ACTIVITY_REORDER_TO_FRONT 任务中的Activity顺序重排
If set in an Intent passed to Context.startActivity(), this flag will cause the launched activity to be brought to the front of its task's history stack if it is already running.
For example, consider a task consisting of four activities: A, B, C, D. If D calls startActivity() with an Intent that resolves to the component of activity B, then B will be brought to the front of the history stack, with this resulting order: A, C, D, B. This flag will be ignored if FLAG_ACTIVITY_CLEAR_TOP is also specified.
如果设置了这个标志,而且被启动的Activity如果已经在运行,那这个Activity会被调到栈顶。
比如,一个任务中有4个Activity:A,B,C,D。如果D调用了startActivity() 来启动B时使用了这个标志,那B就会被调到历史栈的栈顶,结果顺序:A,C,D,B,否则顺序会是:A,B,C,D,B。 如果使用了标志 FLAG_ACTIVITY_CLEAR_TOP,那这个FLAG_ACTIVITY_REORDER_TO_FRONT标志会被忽略。
FLAG_ACTIVITY_SINGLE_TOP
If set, the activity will not be launched if it is already running at the top of the history stack.
设置这个标志之后,如果被启动的Activity已经在栈顶,那它就不会被再次启动。
FLAG_ACTIVITY_TASK_ON_HOME 直接返回桌面
If set in an Intent passed to Context.startActivity(), this flag will cause a newly launching task to be placed on top of the current home activity task (if there is one). That is, pressing back from the task will always return the user to home even if that was not the last activity they saw. This can only be used in conjunction with FLAG_ACTIVITY_NEW_TASK.
这个标志可以将一个新启动的任务置于当前的home任务(home activity task)之上(如果有的话)。也就是说,在任务中按back键总是会回到home界面,而不是回到他们之前看到的activity。这个标志只能与FLAG_ACTIVITY_NEW_TASK标志一起用。
比如,A->B->C->D,如果在C启动D的时候设置了这个标志,那在D中按Back键则是直接回到桌面,而不是C。
注意:
只有D是在新的task中被创建时(也就是D的launchMode是singleInstance时,或者是给D指定了与C不同的taskAffinity并且加了FLAG_ACTIVITY_NEW_TASK标志时),使用 FLAG_ACTIVITY_TASK_ON_HOME标志才会生效。
感觉实际使用效果和用 FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK 的效果一样。
FLAG_DEBUG_LOG_RESOLUTION 打开Activity解析的Log开关
A flag you can enable for debugging: when set, log messages will be printed during the resolution of this intent to show you what has been found to create the final resolved list.
可以用来启用调试的标志:设置以后,在intent的处理过程中log信息会打印出来,可以看到都找到了些什么来创建最终的解析列表。
Log输出如下:
04-15 11:19:09.446: V/IntentResolver(398): Resolving type null scheme null of intent Intent { act=android.intent.action.B_Activity flg=0x8 }
04-15 11:19:09.446: V/IntentResolver(398): Action list: [ActivityIntentInfo{42354338 com.example.icsorigintest.B_Activity}]
04-15 11:19:09.446: V/IntentResolver(398): Matching against filter ActivityIntentInfo{42354338 com.example.icsorigintest.B_Activity}
04-15 11:19:09.446: V/IntentResolver(398): Filter matched! match=0x108000
04-15 11:19:09.446: V/IntentResolver(398): Final result list:
04-15 11:19:09.446: V/IntentResolver(398): ResolveInfo{41ee95d0 com.example.icsorigintest.B_Activity p=0 o=0 m=0x108000}
FLAG_EXCLUDE_STOPPED_PACKAGES 排除已停止的包
If set, this intent will not match any components in packages that are currently stopped. If this is not set, then the default behavior is to include such applications in the result.
设置之后,Intent就不会再匹配那些当前被停止的包里的组件。如果没有设置,默认的匹配行为会包含这些被停止的包。
FLAG_FROM_BACKGROUND 后台启动Activity
Can be set by the caller to indicate that this Intent is coming from a background operation, not from direct user interaction.
可以给调用者用来标识这个Intent是来自后台操作,而不是用户的交互行为。
FLAG_GRANT_READ_URI_PERMISSION 给Activity授权
If set, the recipient of this Intent will be granted permission to perform read operations on the Uri in the Intent's data and any URIs specified in its ClipData.When applying to an Intent's ClipData, all URIs as well as recursive traversals through data or other ClipData in Intent items will be granted; only the grant flags of the top-level Intent are used.
如果设置,Intent的接收者会被授予读权限,用来读取Intent中包含的或是在ClipData中指定的Uri。当被用于Intent中的ClipData时,被授予的是Intent中其它ClipData中的所有Uri和它们递归遍历到的Uri的读权限。只有顶级Intent中的授予标志会被使用。
FLAG_INCLUDE_STOPPED_PACKAGES 包含已停止的包
If set, this intent will always match any components in packages that are currently stopped. This is the default behavior when FLAG_EXCLUDE_STOPPED_PACKAGES is not set. If both of these flags are set, this one wins (it allows overriding of exclude for places where the framework may automatically set the exclude flag).
设置之后Intent总是会去匹配那些已被停止的包里的组件。如果没有设置 FLAG_EXCLUDE_STOPPED_PACKAGES 标志,那这个就是默认行为。如果两个标志都被设置,那这个会生效(在框架中一些地方可能会自动设置exclude标志,这些标志可以被覆盖掉)。
FLAG_RECEIVER_FOREGROUND 接受器以前台优先级运行
If set, when sending a broadcast the recipient is allowed to run at foreground priority, with a shorter timeout interval. During normal broadcasts the receivers are not automatically hoisted out of the background priority class.
当发送广播的时候设置了这个标志,会允许接收者以前台的优先级运行,有更短的时间间隔。正常广播的接受者是后台优先级,不会被自动提升。
FLAG_RECEIVER_REGISTERED_ONLY 只调用手动注册的接收器(忽略manifest中声明的)
If set, when sending a broadcast only registered receivers will be called -- no BroadcastReceiver components will be launched.
如果发送广播时设置了这个标志,那只会调用注册了的接收器——BroadcastReceiver组件不会被启动。
FLAG_RECEIVER_REPLACE_PENDING 替换掉等待中的广播
If set, when sending a broadcast the new broadcast will replace any existing pending broadcast that matches it. Matching is defined by Intent.filterEquals returning true for the intents of the two broadcasts. When a match is found, the new broadcast (and receivers associated with it) will replace the existing one in the pending broadcast list, remaining at the same position in the list.
This flag is most typically used with sticky broadcasts, which only care about delivering the most recent values of the broadcast to their receivers.
如果在发送广播时设置了这个标志,那新的广播会替换掉那些已存在的相同广播。相同的定义是通过Intent.filterEquals方法对两个广播的Intent处理返回true。 当匹配到相同的,新的广播和对应的接收器会将待发送的广播列表中已存在的替换掉,在列表中保留同样的位置。
这个标志通常被粘性广播(Sticky Broadcast)使用,只保证将最新的广播的值传递给接收器。
相关推荐
总之,“安卓Android源码——Intent.rar”这个资源对于想要深入了解Android开发的开发者来说非常有价值,它涵盖了Intent的基本概念、工作原理和实际应用,结合源码分析可以提升开发者对Android系统级组件的理解。
在安卓(Android)开发中,Intent 是一个至关重要的概念,它是应用程序之间通信的主要方式,用于启动其他组件或传递数据。本示例 "Intent_ComponentSample" 旨在深入解析Intent的使用和组件间的交互,特别是Activity...
标题与描述均指向了“Android各组件详解——Intent”,这一主题深入探讨了Android开发中至关重要的Intent组件。本文将从多个角度解析Intent的功能、应用场景及其内部结构,为开发者提供全面的理解。 ### Intent概述...
在安卓(Android)开发中,Intent 是一个至关重要的组件,它起到了应用程序内部或者应用程序之间通信的桥梁作用。在深入理解 Android 源码的过程中,分析 Intent 的实现原理可以帮助开发者更好地利用这一功能,提高...
总之,深入研究这个压缩包中的源码和文档,可以帮助开发者更好地理解Android系统如何处理具有相同Action的Intent,以及如何在源码层面优化Activity的启动和选择逻辑,这对于开发高效且用户友好的应用至关重要。
在安卓(Android)开发中,实现在线更新是提高用户体验、保持应用版本及时更新的重要功能。这份"安卓Android源码——android 在线更新示例代码.rar"压缩包提供了一个实际的在线更新示例,帮助开发者理解如何在自己的...
在安卓(Android)开发中,Activity是应用的基本构建块,它是用户界面的载体,负责处理用户的交互和展示数据。这个压缩包文件“安卓Android源码——(Activity跳转与操作).zip”显然专注于讲解如何在Android系统中...
在安卓(Android)开发中,源码分析是提升技术能力的重要途径之一。"UseIntentRebackHomeSample"这个项目很可能是关于如何利用...在实际开发中,应灵活运用Intent的各种特性,遵循最佳实践,提高代码质量和可维护性。
在安卓开发中,Activity是应用程序的基本构建块,它代表了用户与应用交互的界面。Activity的进出栈管理是安卓系统中处理多任务和任务切换的关键机制。本资料"安卓Android源码——模拟Activity进出栈.zip"着重探讨了...
在安卓(Android)平台上,开发应用时,有时我们需要提供一种机制让用户能够完全退出应用程序,关闭所有相关的活动(Activities)和服务。本资料“安卓Android源码——完全退出程序的方法.rar”可能包含了一些实现这...
在安卓(Android)平台上,开发应用时,有时我们需要提供一种机制让用户能够完全退出应用程序,关闭所有相关的活动(Activities)和服务。本压缩包文件“安卓Android源码——完全退出程序的方法.zip”可能包含了一个...
本压缩包文件"安卓Android源码——返回键退出程序的两种方式.zip"提供了关于这个主题的详细示例。下面我们将深入探讨这两种方法。 1. **监听硬件返回键事件** 第一种方式是通过重写`Activity`类中的`onKeyDown()`...
- 在Android中,文件浏览通常通过Intent启动文件选择器,使用`ACTION_PICK`或`ACTION_GET_CONTENT`。Intent需要附带一个Uri,它代表了要打开的文件或目录。`ContentResolver`可以解析这个Uri,提供对文件的操作。 ...
4. **Intent的Flags**:Intent还可以设置各种标志(如FLAG_ACTIVITY_NEW_TASK、FLAG_ACTIVITY_CLEAR_TOP等),这些标志会影响Activity的启动行为,甚至可能改变匹配规则。 5. **处理多个匹配项**:如果存在多个匹配...
在安卓(Android)平台上,开发应用的过程中,更新功能是一个必不可少的部分。这个压缩包文件"安卓Android源码——app更新,实现service下载.zip"提供了一个示例,教你如何通过后台服务(Service)来实现应用程序的...
悬浮窗在Android应用开发中是一种常见的特性,通常用于实时显示信息或提供快捷操作入口。以下将详细介绍悬浮窗及其在Android源码中的实现原理。 首先,我们需要了解Android的权限管理。在Android 4.3之前,任何应用...
在Android开发领域,掌握如何远程下载并安装应用的源码是一项重要的技能,这对于开发者来说意味着能够灵活地更新和管理应用程序,提高用户体验。本教程将详细解释这个过程,并以"Android源码——如何远程下载安装的...
本资料包“安卓Android源码——快捷图标的创建与移除”深入探讨了如何在Android应用中实现快捷图标的添加和删除功能,这对于开发者优化用户体验至关重要。 首先,我们来理解快捷图标的基本概念。快捷图标是应用的...
在Android开发中,主题(Theme)是一个至关重要的概念,它为应用程序提供了统一的视觉样式和交互体验。主题允许开发者全局设定应用的外观,包括颜色、字体、图标等元素,而无需在每个单独的Activity或View中重复设置...