我是靠谱客的博主 精明小白菜,这篇文章主要介绍android Intent匹配,自定义action data category,现在分享给大家,希望可以做个参考。

官网对Intent的action、data和category的说明

复制代码
1
2
3
4
5
6
7
8
9
public Intent setAction (String action) Added in API level 1 Set the general action to be performed. Parameters action An action name, such as ACTION_VIEW. Application-specific actions should be prefixed with the vendor's package name. Returns Returns the same Intent object, for chaining multiple calls into a single statement.

复制代码
1
2
3
4
5
6
7
8
9
10
11
public Intent setData (Uri data) Added in API level 1 Set the data this intent is operating on. This method automatically clears any type that was previously set by setType(String) or setTypeAndNormalize(String). Note: scheme matching in the Android framework is case-sensitive, unlike the formal RFC. As a result, you should always write your Uri with a lower case scheme, or use normalizeScheme() or setDataAndNormalize(Uri) to ensure that the scheme is converted to lower case. Parameters data The Uri of the data this intent is now targeting. Returns Returns the same Intent object, for chaining multiple calls into a single statement.

复制代码
1
2
3
4
5
6
7
8
9
public Intent addCategory (String category) Added in API level 1 Add a new category to the intent. Categories provide additional detail about the action the intent performs. When resolving an intent, only activities that provide all of the requested categories will be used. Parameters category The desired category. This can be either one of the predefined Intent categories, or a custom category in your own namespace. Returns Returns the same Intent object, for chaining multiple calls into a single statement.

可以看出action, data, category都是可以自定义的。

复制代码
1
2
3
4
5
6
7
Only three aspects of an Intent object are consulted when the object is tested against an intent filter: action data (both URI and data type) category The extras and flags play no part in resolving which component receives an intent.

系统根据IntentFilter决定哪个元件可以接收哪个Intent, Intent是否匹配的判定只跟action,data和category有关。

自定义action的官方命名规范:

You can also define your own action strings for activating the components in your application. Those you invent should include the application package as a prefix — for example: "com.example.project.SHOW_COLOR".


category需要注意的地方

复制代码
1
In principle, therefore, an Intent object with no categories should always pass this test, regardless of what's in the filter.
复制代码
1
That's mostly true. However, with one exception, Android treats all implicit intents passed to startActivity() 
复制代码
1
as if they contained at least one category: "android.intent.category.DEFAULT" (the CATEGORY_DEFAULT constant). 
复制代码
1
Therefore, activities that are willing to receive implicit intents must include "android.intent.category.DEFAULT" 
复制代码
1
in their intent filters. (Filters with "android.intent.action.MAIN" and "android.intent.category.LAUNCHER" settings are the exception. 
复制代码
1
They mark activities that begin new tasks and that are represented on the launcher screen.
复制代码
1
They can include "android.intent.category.DEFAULT" in the list of categories, but don't need to.)



最后

以上就是精明小白菜最近收集整理的关于android Intent匹配,自定义action data category的全部内容,更多相关android内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(91)

评论列表共有 0 条评论

立即
投稿
返回
顶部