Using native alerts and cofirmations of mobile using Cordova

The PhoneGap/Cordova framework allows you to display native dialog box of the device, which allows you to use say custom title and better control on number of buttons on the dialog box. In addition to that it also allows you to beep or vibrate the device(Both beep and vibrate feature dont seem to work in the Android emulator). I wanted to try these features so i built this sample application, which allows you to play with different notifications. This is how the native alert box looks like This is how the native confirmation box looks like I followed these steps to build Android PhoneGap application
  1. I followed the instructions in Getting Started with Android to build application that points to index.html inside the application, i tried it once to make sure that it works
  2. Then i used the following index.html code
    
    <!DOCTYPE html>
    <html>
      <head>
        <title>Google Map Example</title>
      <meta name="viewport" content="width=device-width, 
      initialscale=1.0, user-scalable=no"></meta>
        <script type="text/javascript" charset="utf-8" 
    src="cordova-1.7.0.js"></script>
        <script type="text/javascript" charset="utf-8">
        function alertDismissed() {
            console.log("Inside alertDismissed");
        }
        function onConfirm(){
          console.log("Inside onConfirmation");
        }
        function showAlert() {
            navigator.notification.alert(
                'Sample native alert message',  
                alertDismissed,         
                'Sample native alert title',            
                'alert'                  
            );
        }
        function showConfirmation(){
          navigator.notification.confirm(
                  'Sample native confirmation',  
                  onConfirm,              
                  'Sample confirmation title',            
                  'Confirm'          
              );
        }
        function playBeep() {
            navigator.notification.beep(3);
        }
        function vibrate() {
            navigator.notification.vibrate(2000);
        }
        </script>
      </head>
      <body >
        <ul>
          <li><a href="#" onclick="alert('Browser alert'); 
    return false;">Show browser Alert</a></li>
          <li><a href="#" onclick="showConfirmation(); 
    return false;">Show Confirmation</a></li>
          <li><a href="#" onclick="showAlert(); 
    return false;">Show Alert</a></li>
          <li><a href="#" onclick="playBeep(); 
    return false;">Play Beep</a></li>
          <li><a href="#" onclick="vibrate(); 
    return false;">Vibrate</a></li>
        </ul>
         <p></p>
      </body>
    </html>
    
    The HTML of the page shows 5 different links each pointing to one JavaScript function. Each of the functions uses Phone Gap native feature to display native dialog boxes.

No comments: