HBuilderx打包h5的APP,解决获取相机、麦克风权限问题

index.html文件script中加入以下内容

获取照相机权限

document.addEventListener( plusready ,function () { // 这里必定要加上  不然会报push 未定义

        plus.android.requestPermissions([ android.permission.CAMERA ], function(e){

        if(e.deniedAlways.length>0){    //权限被永久拒绝

            // 弹出提示框解释为何需要定位权限,引导用户打开设置页面开启

            //alert( 永久 );

            document.addEventListener(“plusready”, onPlusReady, false);

            // 扩展API加载完毕,目前可以正常调用扩展API

            function onPlusReady() {

                // 创建扫码控件来调获取手机的相机权限

                barcode = plus.barcode.create( barcode , [plus.barcode.QR], {

                    top: -9999 , //改为-9999px隐藏该页面

                    left: 0 ,

                    width: 100% ,

                    height: 500px ,

                    position: static

                });

                //plus.webview.currentWebview().append(barcode);;

                //barcode.start();开始扫码识别(我们把这句代码注释了,由于我们不需要扫描任何东西)

            }

            //上面打开权限后,这里直接重启APP

            plus.runtime.restart();

        }

        if(e.deniedPresent.length>0){   //权限被临时拒绝

            // 弹出提示框解释为何需要定位权限,可再次调用plus.android.requestPermissions申请权限

            //alert( 临时 );

        }

        if(e.granted.length>0){ //权限被允许

            //调用依赖获取定位权限的代码

            //alert( 允许!!! );

        }

    }, function(e){

        alert( Request Permissions error: +JSON.stringify(e));

    });

    },false);

获取麦克风权限

  document.addEventListener( plusready ,function () {

    plus.android.requestPermissions([ android.permission.RECORD_AUDIO ], function(e){

    if(e.deniedAlways.length>0){    //权限被永久拒绝

        // 弹出提示框解释为何需要定位权限,引导用户打开设置页面开启

        //alert( 永久 );

        document.addEventListener(“plusready”, onPlusReady, false);

        // 扩展API加载完毕,目前可以正常调用扩展API

        function onPlusReady() {

            // 创建扫码控件来调获取手机的相机权限

            barcode = plus.barcode.create( barcode , [plus.barcode.QR], {

                top: -9999 , //改为-9999px隐藏该页面

                left: 0 ,

                width: 100% ,

                height: 500px ,

                position: static

            });

            //plus.webview.currentWebview().append(barcode);;

            //barcode.start();开始扫码识别(我们把这句代码注释了,由于我们不需要扫描任何东西)

        }

        //上面打开权限后,这里直接重启APP

        plus.runtime.restart();

    }

    if(e.deniedPresent.length>0){   //权限被临时拒绝

        // 弹出提示框解释为何需要定位权限,可再次调用plus.android.requestPermissions申请权限

        //alert( 临时 );

    }

    if(e.granted.length>0){ //权限被允许

        //调用依赖获取定位权限的代码

        //alert( 允许!!! );

    }

    }, function(e){

      alert( Request Permissions error: +JSON.stringify(e));

    });

  },false);

© 版权声明
THE END
如果内容对您有所帮助,就支持一下吧!
点赞0 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容