Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
530 views
in Technique[技术] by (71.8m points)

cordova - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...