2012-12-13 27 views
5

Tôi đang cố gắng chạy PhoneGap lần đầu tiên trên thiết bị Android (Samsung Galaxy S II); tuy nhiên, nó dường như không hoạt động.PhoneGap không chạy trong Android

Dưới đây là đoạn code tôi cố gắng cho đến nay:

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

Không có gì show-up! Không cảnh báo, không có nhật ký, nó chỉ hiển thị một trang HTML với nội dung trên body.

@Edit: nó hiển thị kết quả ngay bây giờ; tuy nhiên, tất cả kết quả là null, hiển thị trên hộp thoại Alert. Có ai có kinh nghiệm này không?

+0

'contacts.length' của bạn là gì? –

+0

@PratikSharma: số liên lạc được tìm thấy. Điện thoại của tôi có kết quả là '693', nhưng tất cả 'displayName' là' null'. Nó không nên là 'null' theo điều này: http://docs.phonegap.com/en/2.2.0/cordova_contacts_contacts.md.html#Contact –

+0

bạn có thể thử đặt câu lệnh này trong các liên hệ cảnh báo [i] .name .givenName' và thấy nó vẫn là null? –

Trả lời

17

Đầu tiên tạo ra một ứng dụng Android với Eclipse và sau đó

Thực hiện Bước này:

1 nhập tập tin cordova-2.2.0.jar vào dự án của bạn.

[2] Thêm tệp cordova-2.2.0.js vào thư mục tài sản/www của bạn.

[3] Sao chép thư mục xml đi kèm với nguồn cordova vào thư mục res

[4] Thay thế hoạt động của bạn bằng cách này.

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] Thêm tệp index.html vào nội dung/thư mục www có nội dung này.

<!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] Thêm quyền sau vào tệp kê khai của bạn.

<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.

Bạn sẽ có chi tiết liên hệ trên màn hình của mình.

Hy vọng nó sẽ giúp bạn.

Cảm ơn.

+0

cảm ơn bạn đã hướng dẫn chi tiết; tuy nhiên, nó dường như không hoạt động. Cảnh báo hiển thị 'Xong' ->' 693' -> 'null' ->' null' .... ahh, không hiểu .. –

+0

đặt điều kiện ở trên cảnh báo 'if (contacts [i] .displayName! = Null) '. –

+0

thú vị! có nhiều giá trị 'null', phần còn lại thì không. Cảm ơn, nó đã làm việc bây giờ. –

3

Cách thay thế dễ dàng hơn để sử dụng PhoneGap trong Eclipse.
1.Click Help-Eclipse Marketplace
2.Search- "Điện thoại Gap"
3.Install PhoneGap (PhoneGap cũng được gọi cordova)
Làm điều này chỉ là lần 1. Từ lần tiếp theo bất cứ khi nào bạn tạo dự án phonegap, chỉ cần tạo "Dự án PhoneGap mới". Đó là tất cả. Quá trình này giúp bạn tiết kiệm rất nhiều thời gian và lỗi.

+0

Không có điện thoại nào được tìm thấy. Chỉ cần AppLaud, LDPA, MyEclipse ... ect, cái nào là tốt nhất để sử dụng? Tôi muốn tạo ứng dụng cho WP8 và iOS cũng như – ProblemsOfSumit

+0

@sourav Tôi đã có thể tìm thấy bất kỳ thứ gì như thế này. –

Các vấn đề liên quan