您的位置:首页 > 其它

隐式打开Activity——Intent设置(如何打开)和Intent-fileter配置(怎么能被打开)

2015-05-20 15:50 267 查看
打开一个Activity的Intent设置和目标Activity的intent-filter配置是一一对应的。

首先看一个Activity的intent-filter的配置:

<activity
android:name=".InexplicitConfigActivity"
android:label="@string/app_name" >
<intent-filter>
<!-- 设置接收动作,可以是系统标准动作,也可以是自定义动作,值皆为一个字符串 -->
<action android:name="android.intent.action.VIEW"/>
<!-- 设置接收意图范畴,一般使用系统给定的标准范畴,自定义的范畴无意义 -->
<category android:name="android.intent.category.DEFAULT"/>
<!-- 定义接收数据格式 -->
<data android:scheme="https"></data>
<data android:scheme="svn"></data>
</intent-filter>

<intent-filter>
<action android:name="customAction"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>


该配置表明该Activity在两种情况下能打开(隐式意图,显式意图不讨论):

  1、使用系统标准动作android.intent.action.VIEW,默认范畴,且必须设置附加数据,数据的模式必须类似于“https://....”或“svn://...”;

  2、使用自定义动作customAction,默认范畴,可以不设置附加数据;

对应的打开该Activity的代码如下:

Intent intent = new Intent();

intent.setAction(Intent.ACTION_VIEW);
intent.setData(Uri.parse("https://www.baidu.com"));
intent.addCategory(Intent.CATEGORY_DEFAULT);

startActivity(intent);


Intent intent = new Intent();

intent.setAction("customAction");
intent.addCategory(Intent.CATEGORY_DEFAULT);

startActivity(intent);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: