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

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

2018-03-27 12:01 465 查看
在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=...即可实现原理最近,在使用QQ和微信等SDK来实现分享网页的时候,发现,SDK已经为页面跳转回应用提供了基本的数据支持。我们只需在应用里和被分享的网页进行简单的设置,即可实现此功能。那么我们先来看下网页跳转回应用的实现原理。就Android平台而言,URI主要分三个部分:scheme, authority and path。其中authority又分为host和port。格式如下: 
scheme://host:port/path 
举个实际的例子: 
content://com.example.project:200/folder/subfolder/etc 
\---------/  \---------------------------/ \---/ \--------------------------/ 
scheme                 host               port        path 
                \--------------------------------/ 
                          authority    现在大家应该知道data flag中那些属性的含义了吧,看下data flag 
<data android:host="string" 
      android:mimeType="string" 
      android:path="string" 
      android:pathPattern="string" 
      android:pathPrefix="string" 
      android:port="string" 
      android:scheme="string" /> 
点击微信和QQ分享跳转到程序内部的原理与此一致。写在后面:由于微信在5.0.3以后就禁用了微信浏览器里打开别的app,所以上面的方法在微信里不能直接起作用。但是我们有补救方法,1,通过跳转应用宝,来判断是否安装应用,如果安装应用宝会直接打开2,引导用户在浏览器里打开当前网页,因为微信虽然禁止了android:scheme跳转,但是浏览器都是支持的。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  网页跳转到app