一、首先要在AndroidManifest.xml中设置一下过滤器,以JumpActivity为例:
<activity android:name=".JumpActivity" >
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<!-- 在data里设置了scheme和host,则该Activity可以接收和处理类似于"scheme://data/XXX"的链接 -->
<data
android:host="data"
android:scheme="scheme" />
</intent-filter>
</activity>
二、在JumpActivity中做打开后的处理,用来接收外部的跳转
public class JumpActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
String data = intent.getDataString(); // 接收到网页传过来的数据:scheme://data/xxx
String[] split = data.split("data/");
String param = split[1]; // 获取到网页传过来的参数
}
}
三、我们需要找到html前端,在网页中加入:
如下:index.html
<!DOCTYPE html>
<html>
<body>
<iframe src="scheme://data/param=abc" style="display:none"/>
</body>
</html>
四、实现原理
就Android平台而言,URI主要分三个部分:scheme、authority、path。其中authority又分为host、port。格式如下:
scheme://host:port/path
举个实际的例子:
content://com.example.project:200/folder/subfolder/etc
现在大家应该知道data flag中那些属性的含义了吧:
<data android:host="string"
android:mimeType="string"
android:path="string"
android:pathPattern="string"
android:pathPrefix="string"
android:port="string"
android:scheme="string" />