JS启用本地安卓APP的方法

1、编辑AndroidManifest.xml:

       <!--要启动的activity -->
       <activity
            android:name="com.huahan.lovebook.MainActivity"
            android:label="@string/app_name"
            android:launchMode="singleTask"
            android:screenOrientation="portrait" >
            <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="hhsoftopenracs" android:pathPrefix="hhsoftopenracs://xxx/openwith?data=mydata"/><!--替换自己的 -->
            </intent-filter>
        </activity>

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

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

2、通过activity获得data数据

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

3、编写js代码:

    function openApp() {  

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

    }  

上面代码可以达到这样一个目的,先请求 hhsoftopenracs:// ,如果系统能处理,或者说已经安装了hhsoftopenracs表示的应用,那么就可以打开,另外,如果不能打开,直接刷新一下当前页面,等于是重置location。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

帅气的铅笔

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值