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

原创在https://blog.csdn.net/m0_37678565/article/details/68491444

在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=...即可

---------------------

作者:又宠又烦的Android女

来源:CSDN

原文:https://blog.csdn.net/m0_37678565/article/details/68491444

版权声明:本文为博主原创文章,转载请附上博文链接!

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 一、知识点回顾:Activity (一)、如何实现Activity页面跳转? 示例代码: //第一种方式: Int...
    白话徐文涛阅读 2,075评论 0 15
  • Intent组件虽然不是四大组件,但却是连接四大组件的桥梁,学习好这个知识,也非常的重要。 一、什么是Intent...
    困惑困惑困惑阅读 1,545评论 0 0
  • 什么是Intent intent是一个消息传递对象,可以使用它从其他应用组件请求操作。通常我们用intent来启动...
    dony_east阅读 853评论 0 3
  • 首先,能活到八十岁,本身就是一种幸运。 八十岁是一种什么样的状态呢?牙齿稀疏,头发花白,说话呓语不清,甚至无法自...
    阿不思奇缘阅读 623评论 0 1
  • 分别很久后 某日在人潮拥挤中突然看到你 当你被人群推攘着路过我身旁时 我仿佛看到了旧日时光它翩翩向我飞来 你还是如...
    星寞阅读 277评论 0 0