当前位置: 动力学知识库 > 问答 > 编程问答 >

android - PhoneGap : device APIs not working

问题描述:

I'm trying to create an app that can run on Android, iOS and Windows Phone using PhoneGap. Currently, I have only phones with Android so I can't know if the problem I'm having exists on iOS and WP.

I tried the notification (alert, beep and vibration) and the camera APIs.

I took the code lines from the Apache Cordova Documentation. I built the app on PhoneGap Built site, scanned the barcode etc. The app is installed and launched perfectly on the phones but nothing is working (for example, when I clicked on the "Vibrate" link, it doesn't vibrate).

The install and all were done by my tutors (I'm in intership), so I guess it's okay for that part.

I checked for the uses-permissions in the AndroidManifest.xml and it's okay.

I've been looking for answers by searching on google, forums etc. but so far, I've found nothing that matches or fixes my issue. That's why I'm posting this message (please don't pay to much attention for language mistake, I'm not an english native speaker).

Thank you in advance for your help.

Edit : 1st phone : Samsung Galaxy Grand Plus (GT-I9060I), Android : 4.4.4. 2nd phone : Samsung Galaxy S5 Prime (SM-G901F), Android : 5.0.2

Using Cordova version 5.0.0. Necessary plugin features have already been added.

Code from the Apache Cordova Documentation for the notifications :

<!DOCTYPE html>

<html>

<head>

<title>Notification Example</title>

<script type="text/javascript" charset="utf-8" src="cordova-1.5.0.js"></script>

<script type="text/javascript" charset="utf-8">

// Wait for PhoneGap to load

//

document.addEventListener("deviceready", onDeviceReady, false);

// PhoneGap is ready

//

function onDeviceReady() {

// Empty

}

// Show a custom alert

//

function showAlert() {

navigator.notification.alert(

'You are the winner!', // message

'Game Over', // title

'Done' // buttonName

);

}

// Beep three times

//

function playBeep() {

navigator.notification.beep(3);

}

// Vibrate for 2 seconds

//

function vibrate() {

navigator.notification.vibrate(2000);

}

</script>

</head>

<body>

<p><a href="#" onclick="showAlert(); return false;">Show Alert</a></p>

<p><a href="#" onclick="playBeep(); return false;">Play Beep</a></p>

<p><a href="#" onclick="vibrate(); return false;">Vibrate</a></p>

</body>

网友答案:

If you are having cordova version prior to 3.0.0 then you must include plugins manually in config.xml which resides in resources folder. If cordova version is >= 3.0.0 then you must include these native features via command line.

You need to provide the events such as click, touchend, pagebeforeshow etc. in OnDeviceReady() method. i.e. you have to register listeners to the HTML elements so that they listen to the events.

网友答案:

It seems that I was not using the correct version of the Apache Cordova Documention (5.0.0) but an older version. The following code is taken from the doc and is working only for vibrate(), and once again, nothing is happening for playBeep() and showAlert(). It doesn't make sense.

I also tried the Camera API (from the correct version of the doc) and it's not working either. I really don't understand why it's okay for vibration and not for the other API.

<!DOCTYPE html>
<html>
    <head>
        <title>Notification Example</title>

       <script type="text/javascript" charset="utf-8" src="cordova.js"></script>
       <script type="text/javascript" charset="utf-8">

         document.addEventListener("deviceready", onDeviceReady, false);
         function onDeviceReady() {
            console.log(navigator.notification);
            console.log(navigator.vibrate);
         }
         function alertDismissed() {
            alert('Dismissed');
         }
         function showAlert() {
            navigator.notification.alert(
                    'You are the winner!', // message
                    alertDismissed, // callback
                    'Game Over', // title
                    'Done'                  // buttonName
                    );
        }
        function playBeep() {
            navigator.notification.beep(2);
        }
        function vibrate() {
            navigator.vibrate(3000);
        }

    </script>
 </head>
    <body>
        <p><a href="#" onclick="showAlert();
            return false;">Show Alert</a></p>
        <p><a href="#" onclick="playBeep();
            return false;">Play Beep</a></p>
        <p><a href="#" onclick="vibrate();
            return false;">Vibrate</a></p>
    </body>
</html>
分享给朋友:
您可能感兴趣的文章:
随机阅读: