实现h5链接打开Android app
2021-11-22 本文已影响0人
hao_developer
AndroidManifest.xml中设置data属性,data代表数据源,是中最复杂的标签,因为不同的Activity支持的数据来源和类型多种多样,所以需要通过详细的data标签信息来指明。
启动页中加入下列代码属性
<activity
android:name=".activitys.StartActivity"
android:configChanges="orientation|keyboard|mcc|mnc|locale|keyboardHidden|uiMode|fontScale"
android:screenOrientation="portrait"
android:theme="@style/AppTheme.customeTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<!--h5跳转app -->
<!--需要添加下面的intent-filter配置-->
<intent-filter>
<data
android:host="com.aaa.cn"
android:scheme="myscheme" />
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
</intent-filter>
</activity>
h5调用Uri uri = Uri.parse(“myscheme://com.aaa.cn”); 才可以匹配
如果在manifest里这样写:
<data android:scheme="something" android:host="project.example.com" />
那么Uri uri = Uri.parse(“something://project.example.com”); 才可以匹配
再如:
<data android:scheme="something" android:host="project.example.com" android:port="80"/>
那么Uri uri = Uri.parse(“something://project.example.com:80”); 才可以匹配
h5传值app接收
例如h5调用`
Uri uri = Uri.parse("myscheme://com.aaa.cn?content=homefragment"); `
android接收
//接收h5跳转意图
Uri uri = getIntent().getData();
if (uri != null) {
String content= uri.getQueryParameter("content");
//接收值做具体操作
}
h5拉起app
if (navigator.userAgent.match(/(iPhone|iPod|iPad);?/i)) {
var loadDateTime = new Date();
window.location = "...";//schema链接或者universal link
window.setTimeout(function() { //如果没有安装app,便会执行setTimeout跳转下载页
var timeOutDateTime = new Date();
if (timeOutDateTime - loadDateTime < 5000) {
window.location = "..."; //ios下载地址
} else {
window.close();
}
}, 500);
} else if (navigator.userAgent.match(/android/i)) {
var state = null;
try {
window.location = '...'; //schema链接或者universal link
window.setTimeout(function() {
window.location = "..."; //android下载地址
}, 500);
} catch (e) {}
}
},