这个方式也兼容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:
https://gist.github.com/FokkeZB/6635236 我使用的是这篇文章中js的实现方式, 这篇文章也有后端php的实现方式。