在html页面中判断本地app是否安装并打开

48 篇文章 0 订阅

http://blog.csdn.net/vinrex/article/details/38082759

html中其实是无法判断应用是否安装,除非在webview中通过js bridge,这里通过一种方式达到此目的。

1、编辑AndroidManifest.xml:

    主要是增加第二个<intent-filter>,myapp用来标识schema,最好能保证手机系统唯一,那样就可以打开应用,而不是弹出一个选择框。

    Android:pathPrefix标识url的path,可以附带自己的数据通过string传递到activity,比如完整url为 myapp://xxx/openwith?data=mydata

  1. <activity  
  2.   android:name="com.abc.MainActivity"  
  3.   android:configChanges="orientation|keyboardHidden|navigation|screenSize"  
  4.   android:screenOrientation="landscape"  
  5.   android:theme="@android:style/Theme.NoTitleBar.Fullscreen" >  
  6.   <intent-filter>  
  7.       <action android:name="android.intent.action.MAIN" />  
  8.       <category android:name="android.intent.category.LAUNCHER" />  
  9.   </intent-filter>  
  10.   <intent-filter>  
  11.       <action android:name="android.intent.action.VIEW" />  
  12.       <category android:name="android.intent.category.BROWSABLE" />  
  13.       <category android:name="android.intent.category.DEFAULT"/>  
  14.       <data android:scheme="myapp" android:pathPrefix="/xxx/openwith" />  
  15.   </intent-filter>  
  16. t;/activity>  
          <activity
            android:name="com.abc.MainActivity"
            android:configChanges="orientation|keyboardHidden|navigation|screenSize"
            android:screenOrientation="landscape"
            android:theme="@android:style/Theme.NoTitleBar.Fullscreen" >
            <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.BROWSABLE" />
                <category android:name="android.intent.category.DEFAULT"/>
                <data android:scheme="myapp" android:pathPrefix="/xxx/openwith" />
            </intent-filter>
        </activity>
   然后通过activity获得data数据:

  1.  public void onCreate(Bundle savedInstanceState) {  
  2.      Uri uridata = this.getIntent().getData();  
  3.      String mydata = uridata.getQueryParameter("data");  
  4.      ...  
  5. }  
    public void onCreate(Bundle savedInstanceState) {
        Uri uridata = this.getIntent().getData();
        String mydata = uridata.getQueryParameter("data");
        ...
   }

  2、编写html页面:

   整个页面也许是某个app的详细介绍,这里只写出关键的js代码:

[javascript] view plain copy
print ?
  1. function openApp() {  
  2.   
  3.         if (/android/i.test(navigator.userAgent)) {  
  4.              var isrefresh = getUrlParam('refresh'); // 获得refresh参数  
  5.              if(isrefresh == 1) {  
  6.                  return  
  7.              }  
  8.              window.location.href = 'myapp://xxx/openwith?data=mydata';  
  9.              window.setTimeout(function () {  
  10.                      window.location.href += '&refresh=1' // 附加一个特殊参数,用来标识这次刷新不要再调用myapp:// 了  
  11.              }, 500);  
  12.          }  
  13.   
  14. }  
   function openApp() {

           if (/android/i.test(navigator.userAgent)) {
                var isrefresh = getUrlParam('refresh'); // 获得refresh参数
                if(isrefresh == 1) {
                    return
                }
                window.location.href = 'myapp://xxx/openwith?data=mydata';
                window.setTimeout(function () {
                        window.location.href += '&refresh=1' // 附加一个特殊参数,用来标识这次刷新不要再调用myapp:// 了
                }, 500);
            }

   }
  上面代码可以达到这样一个目的,先请求 myapp:// ,如果系统能处理,或者说已经安装了myapp表示的应用,那么就可以打开,另外,如果不能打开,直接刷新一下当前页面,等于是重置location。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值