码迷,mamicode.com
首页 > 移动开发 > 详细

在手机的浏览器上通过连接打开App

时间:2016-09-22 23:49:47      阅读:5587      评论:0      收藏:0      [点我收藏+]

标签:

Android系统中实现


 

1、在系统系统自带的浏览器中

首先做成HTML的页面,页面内容格式如下:

<a href="[scheme]://[host]/[path]?[query]">启动应用程序</a> 

这一句就可以了。

各个项目含义如下所示:

scheme:判别启动的App。 ※详细后述

host:适当记述

path:传值时必须的key     ※没有也可以

query:获取值的Key和Value  ※没有也可以

 

作为测试好好写了一下,如下:

<a href="myapp://jp.app/openwith?name=zhangsan&age=26">启动应用程序</a>  

 

接下来是Android端。
首先在AndroidManifest.xml的MAIN Activity下追加以下内容。(启动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:scheme="myapp" android:host="jp.app" android:pathPrefix="/openwith"/>  
</intent-filter>

HTML记述的内容加入<data …/>。
其中必须的内容仅scheme,没有其他内容app也能启动。

※注意事项:intent-filter的内容【android.intent.action.MAIN】和 【android.intent.category.LAUNCHER】这2个,不能与这次追加的内容混合。
                 所以,如果加入了同一个Activity,请按以下这样做,否则会导致应用图标在桌面消失等问题。

<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="myapp" android:host="jp.app" android:pathPrefix="/openwith"/>  
</intent-filter> 

这样的话,没有问题。

 

接下来在Activity中需要取值的地方添加以下代码,我是直接写在OnCreate函数里的:

 
Intent i_getvalue = getIntent();  
String action = i_getvalue.getAction();  
  
if(Intent.ACTION_VIEW.equals(action)){  
    Uri uri = i_getvalue.getData();  
    if(uri != null){  
        String name = uri.getQueryParameter("name");  
        String age= uri.getQueryParameter("age");  
    }  
}

这样就能获取到URL传递过来的值了。

2、在第三方的浏览器中

把一个http服务宿主在本地应用中,本地的服务地址为127.0.0.1:8765中,宿主用于监控服务数据,并打开自身。

3、在微信中打开

在微信开放平台登记应用之后,可以获得appid,通过这个appid就可以跳转到你的app。
iOS平台格式如下:appid://openwebview/?ret=0,appid要替换成实际的,后面可以带参数,在你的app可以接收到。
例如:location.href = wx234ad233ae222://openwebview/?ret=0

IOS系统中实现(没有苹果设备,只能拿mac模拟)


 

1、在系统自带的浏览器

  // To avoid the "protocol not supported" alert, fail must open another app.  
            var appstore = "itms://itunes.apple.com/us/app/facebook/id284882215?mt=8&uo=6";  
            function applink(fail){  
                return function(){  
                    var clickedAt = +new Date;  
                    // During tests on 3g/3gs this timeout fires immediately if less than 500ms.  
                    setTimeout(function(){  
                              // To avoid failing on return to MobileSafari, ensure freshness!  
                              if (+new Date - clickedAt < 2000){  
                              window.location = fail;  
                              }  
                              }, 500);      
                };  
            }  
            document.getElementById("applink1").onclick = applink(appstore);  
            document.getElementById("applink2").onclick = applink(appstore); 

 

其原理就是为HTML页面中的超链接点击事件增加一个setTimeout方法.

如果在iPhone上面500ms内,本机有应用程序能解析这个协议并打开程序,则这个回调方法失效;如果本机没有应用程序能解析该协议或者500ms内没有打开个程序,则执行setTimeout里面的function,就是跳转到apple的itunes。

2、在第三方的浏览器中

网易云音乐的地址,直接就能打开云音乐

https://itunes.apple.com/app/id590338362

3、在微信中打开

在微信开放平台登记应用之后,可以获得appid,通过这个appid就可以跳转到你的app。
iOS平台格式如下:appid://openwebview/?ret=0,appid要替换成实际的,后面可以带参数,在你的app可以接收到。
例如:location.href = wx234ad233ae222://openwebview/?ret=0

在手机的浏览器上通过连接打开App

标签:

原文地址:http://www.cnblogs.com/sexintercourse/p/5898242.html

(1)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!