Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PhoneGap not running in Android

I'm trying to run PhoneGap first time on Android device (Samsung Galaxy S II); however, it doesn't seem to work.

Here the code I try so far:

<html>
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
            <meta name="format-detection" content="telephone=no" />
            <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width, height=device-height, target-densitydpi=device-dpi"
    />
            <title>Hello World</title>
            <script type="text/javascript" src="cordova-2.2.0.js"></script>
            <script type="text/javascript" charset="utf-8">
            document.addEventListener("deviceready", onDeviceReady, false);

            function onDeviceReady() {
                var options = new ContactFindOptions();
                options.filter = "";
                options.multiple = true;
                var filter = ["displayName"];
                navigator.contacts.find(filter, onSuccess, onError, options);
            }

            function onSuccess(contacts) {
                for(var i = 0; i < contacts.length; ++i) {
                    alert(contacts[i].displayName);
                }
                alert("OK then");
                console.log("OK");
            }

            function onError(contactError) {
                alert("onError!");
                console.log("Fail");
            }
            </script>
        </head>
        <body >


        </body>
        <h1> Example </h1>
        <p> Find Contacts </p>
    </html>

Nothing show-ups! No alert, no log, it just displays an HTML page with above body content.

@Edit: it displays results now; however, all results are null, showing up on Alert dialog. Has anyone experience this?

like image 927
Pete Houston Avatar asked Dec 13 '12 04:12

Pete Houston


1 Answers

First create one Android application with Eclipse and then

Follow this Steps :

1 Import cordova-2.2.0.jar file into your project.

[2] Add cordova-2.2.0.js file into your assets/www folder.

[3] Copy xml folder came with cordova source into res folder

[4] Replace your activity with this.

package com.cordovatest;

import org.apache.cordova.DroidGap;

import android.os.Bundle;

public class MainActivity extends DroidGap {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        super.setIntegerProperty("loadUrlTimeoutValue", 60000);        
        super.loadUrl("file:///android_asset/www/index.html",1000);
    }


}

[5] Add index.html file into assets/www folder with this content.

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

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

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

    function onDeviceReady() {    
        callFetchContacts();
    }

    function callFetchContacts(){
        var options = new ContactFindOptions();
        options.multiple=true; 
        var fields = ["id","name", "displayName", "organizations","emails","phoneNumbers","addresses"];
        navigator.contacts.find(fields, onSuccess, onError, options);   

    }

    function onSuccess(contacts) {

        alert('Done');
        alert(contacts.length);

        for(var i = 0; i < contacts.length; i++){
            alert(contacts[i].displayName);
        }


    };

    function onError(contactError) {
        alert('onError!');
    }

    </script>
  </head>
  <body>
    <h1>Example</h1>
    <p>Display Contacts</p>
  </body>
</html>

[6] Add following permissions into your Manifest file.

    <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.VIBRATE" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.RECEIVE_SMS" />
    <uses-permission android:name="android.permission.RECORD_AUDIO" />
    <uses-permission android:name="android.permission.RECORD_VIDEO"/>
    <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
    <uses-permission android:name="android.permission.READ_CONTACTS" />
    <uses-permission android:name="android.permission.WRITE_CONTACTS" />   
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />   
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="android.permission.BROADCAST_STICKY" />

That's It.

You will have your contacts details on your screen.

Hope it helps you.

Thanks.

like image 88
Pratik Sharma Avatar answered Sep 28 '22 08:09

Pratik Sharma