Intent匹配规则
上文中提到了,当发送一个隐式Intent后,系统会将它与设备中的每一个组件的过滤器进行匹配,匹配属性有Action、Category、Data三个,需要这三个属性都匹配成功才能唤起相应的组件。
Action 匹配规则
省略...
Category 匹配规则
省略...
Data匹配Uri的规则
<scheme>://<host>:<port>/<path>
content://192.168.0.1:8080/folder/subfolder/etc
在这个URI中,scheme是content,host是192.168.0.1,port是8080,path是folder/subfolder/etc。我们平时使用的网络url就是这种格式。
在URI中,每个组成部分都是可选的,但是有线性的依赖关系
如果没有scheme部分,那么host部分会被忽略
如果没有host部分,那么port部分会被忽略
如果host部分和port部分都没有,那么path部分会被忽略
打开App传递参数Uri的格式:
scheme://host/path?query
scheme:传输协议(一般为http or https,但是也可以为其他);
host:包名
path:为了格式化path所用,不同可以区分Activity
query:传递参数,eg:?userName=lisi&pwd=123456;
Example
Activity
com.niubashaoye.android.demo.activity.BrowserActivity
private void initData() {
//获取数据
Intent intent = getIntent();
Uri uri = intent.getData();
txt_userNamer.setText("姓名:" + uri.getQueryParameter("userName"));
txt_pwd.setText("密码:" + uri.getQueryParameter("pwd"));
}
AndroidManifest.xml
Activity add <intent-filter />
and <Data />
<activity
android:name="com.niubashaoye.android.demo.activity.BrowserActivity"
android:label="@string/str_browser" >
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data
android:host="www.niubashaoye.com"
android:path="/browserActivity"
android:scheme="browser" />
</intent-filter>
</activity>
注:
- scheme不能是http或https;
- 通过data的path设置多个Activity区别;
Call
在浏览器输入:browser://www.niubashaoye.com/browserActivity?userName=lisi&pwd=123456;
也可以通过:
<a href='browser://www.niubashaoye.com/browserActivity?userName=lisi&pwd=123456'>浏览器启动App并传参</a>