在android浏览器中如果安装了app,则打开app,如果没有则跳转到应用市场下载。...

这个方式也兼容ios。

先看html

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>App Redirection</title>
</head>
<body>


<script>
var IS_IPAD = navigator.userAgent.match(/iPad/i) != null,
IS_IPHONE = !IS_IPAD && ((navigator.userAgent.match(/iPhone/i) != null) || (navigator.userAgent.match(/iPod/i) != null)),
IS_IOS = IS_IPAD || IS_IPHONE,
IS_ANDROID = !IS_IOS && navigator.userAgent.match(/android/i) != null,
IS_MOBILE = IS_IOS || IS_ANDROID;

function open() {

// If it's not an universal app, use IS_IPAD or IS_IPHONE
if (IS_IOS) {
window.location = "myapp://view?id=123";

setTimeout(function() {

// If the user is still here, open the App Store
if (!document.webkitHidden) {

// Replace the Apple ID following '/id'
window.location = 'http://itunes.apple.com/app/id1234567';
}
}, 25);

} else if (IS_ANDROID) {

// Instead of using the actual URL scheme, use 'intent://' for better UX
window.location = 'intent://view?xxxx=123#Intent;package=com.example.myapp;scheme=vip;launchFlags=268435456;end;';

}
}

open();
</script>
</body>
</html>


android app中androidminifest.xml的activity的设置,具体看MyActivity

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myapp"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="4" android:targetSdkVersion="19"/>
<application android:label="@string/app_name" android:icon="@drawable/ic_launcher" android:hardwareAccelerated="true">
<activity android:name=".MyActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
<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:scheme="vip" android:path="/view" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.INTERNET"/>
</manifest>


在activity获取传入的参数xxxx的值,需要解析Uri方式来获取

package com.example.myapp;


import org.apache.http.protocol.UriPatternMatcher;

import java.net.URI;
import java.net.URISyntaxException;

public class MyActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String s = getIntent().toURI();
Uri parse = Uri.parse(s);
String xxxx = parse.getQueryParameter("xxxx");

Log.d("MyActivity", xxxx);
}

@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
Log.d("MyActivity", "xx" + getIntent().toURI());
}
}



Reference:[url]https://gist.github.com/FokkeZB/6635236[/url] 我使用的是这篇文章中js的实现方式, 这篇文章也有后端php的实现方式。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值