您的位置:首页 > 移动开发

H5唤醒app并跳转到指定页面

2017-03-30 18:58 489 查看
在manifest文件中最开始启动的activity中添加:
<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="host"
android:pathPrefix="/pathPrefix"
android:scheme="scheme" />
</intent-filter>
//注意host,pathPrefix,scheme都是自己自定义的,只要与h5页面调用的一致即可,如下所示
<activity
android:name=".activitys.MainActivity"
android:label="@string/app_name"
android:screenOrientation="portrait">
<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:host="host"
android:pathPrefix="/pathPrefix"
android:scheme="scheme" />
</intent-filter>
</activity>

如果要跳转到指定的页面,在MainActivity的onCreate()中添加:
Intent intent = getIntent();
Uri uri = intent.getData();
if (uri != null) {
String routeId = uri.getQueryParameter("pid");
Intent intent0 = new Intent(MainActivity.this, ZhidingActivity.class);
startActivity(intent0);
}
uri.getQueryParameter("pid");获取h5页面传递的参数,如果没有的话可以忽略
注意一点,微信上对于app的唤醒有拦截,在浏览器中才可以起作用对于h5的代码如下:
<span style="font-size:18px;"><script>  
function startAPP(){  
window.location = "scheme://host/pathPrefix";  
}  
function downloadAPP(){  
window.location = "apk下载地址";  
}  
  
</script>  
<style type="text/css">  
        body{background:#D2D460;text-align:center;margin-top:10%}  
        div{width:100%;margin:0 auto;background:#fff;text-align:left;}  
</style>  
</head>  
<body>  
  
<?php   
$var_name =  $_GET["isappinstalled"];   
if ($var_name=="1"){  
    echo '<img src=images/start.png alt=打开APP onclick=startAPP() ></img>';  
}else if ($var_name=="0"){  
    echo '<img src=images/download.png alt=打开APP onclick=downloadAPP() ></img>';  
}else{  
    echo '<img src=images/welcome.png alt=打开APP onclick=startAPP() ></img>';  
}  
?>  
  
</body></span> 一定要注意:
window.location = "scheme://host/pathPrefix"; 
scheme,host,pathPrefix一定要与android定义的一致
如果要传递参数的话:在后面拼接?pid=...即可
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: