2013-05-18 42 views
2

在我的PhoneGap應用程序中,我試圖讓原生Android聯繫人選擇器啓動,以便我可以獲取一些電話號碼。Phonegap Android聯繫人選取器插件(更新插件)

我在網上研究並不能找到太多,直到我看到了ContactView插件: https://github.com/phonegap/phonegap-plugins/tree/master/Android/ContactView

當我按說明書設置插件,我在ContactView.java文件中遇到的錯誤隨處可見。看起來它正在使用ctx和其他不贊成使用的命令(例如startActivityForResult)的插件結構的舊版本。

所以我試圖通過一行一行地將它轉換成一個現代的插件,但我太n00b並卡在約40行(在startContactActivity函數內)。這是我到目前爲止有:

package com.rearden; 

import org.apache.cordova.api.CallbackContext; 
import org.apache.cordova.api.CordovaPlugin; 
import org.json.JSONArray; 
import org.json.JSONException; 
import org.json.JSONObject; 

import android.app.Activity; 
import android.content.ContentResolver; 
import android.content.Intent; 
import android.database.Cursor; 
import android.net.Uri; 
import android.provider.ContactsContract; 
import android.util.Log; 

public class ContactView extends CordovaPlugin { 
    private static final String TAG = "PickContactPlugin"; 
    private static final int PICK_CONTACT = 1; 

    @Override 
    public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException { 
     Log.d(TAG, "Inside ContactView plugin."); 

     JSONObject result = new JSONObject(); 

     if (action.equals("") || action.equals("ContactView")) { 
      return startContactActivity(); 
     } else { 
      Log.e(TAG, "Unknown action provided."); 
      result.put("error", "Unknown action provided."); 
      callbackContext.success(result); 
      return false; 
     } 
    } 

    public boolean startContactActivity() { 
     Intent intent = new Intent(Intent.ACTION_PICK); 
     intent.setType(ContactsContract.Contacts.CONTENT_TYPE); 
     //STUCK HERE 
     this.cordova.getActivity().startActivityForResult((Plugin) this, intent, PICK_CONTACT); 
    } 

    @Override 
    public void onActivityResult(int reqCode, int resultCode, Intent data) { 
     String name = null; 
     String number = null; 
     String email = null; 
     ContentResolver contentResolver = cordova.getActivity().getContentResolver(); 
     switch (reqCode) { 
     case (PICK_CONTACT): 
      if (resultCode == Activity.RESULT_OK) { 
       Uri contactData = data.getData(); 
       Cursor c = contentResolver.query(contactData, null, null, null, null); 
       if (c.moveToFirst()) { 
        String ContactID = c.getString(c 
          .getColumnIndex(ContactsContract.Contacts._ID)); 
        String hasPhone = c.getString(c.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)); 

        if (Integer.parseInt(hasPhone) == 1) { 
         Cursor phoneCursor = contentResolver.query(
             ContactsContract.CommonDataKinds.Phone.CONTENT_URI, 
             null, 
             ContactsContract.CommonDataKinds.Phone.CONTACT_ID 
               + "='" + ContactID + "'", null, 
             null); 
         while (phoneCursor.moveToNext()) { 
          number = phoneCursor 
            .getString(phoneCursor 
            .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); 
         } 
        } 
        // get email address 
        Cursor emailCur = contentResolver.query( 
          ContactsContract.CommonDataKinds.Email.CONTENT_URI, 
          null, 
          ContactsContract.CommonDataKinds.Email.CONTACT_ID + "='" + ContactID + "'", null,null); 
         while (emailCur.moveToNext()) { 
          // This would allow you get several email addresses 
           // if the email addresses were stored in an array 
          email = emailCur.getString(
              emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA)); 
          //String emailType = emailCur.getString(
           //   emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE)); 
         } 
         emailCur.close(); 

        name = c.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME)); 
        JSONObject contactObject = new JSONObject(); 
        try { 
         contactObject.put("name", name); 
         contactObject.put("phone", number); 
         contactObject.put("email", email); 
        } catch (JSONException e) { 
         e.printStackTrace(); 
        } 

        this.success(new PluginResult(PluginResult.Status.OK, 
          contactObject), this.callback); 
       } 
      } 
      break; 
     } 
    } 
} 

似乎我不能夠做的就是找到適合「this.ctx.startActivityForResult」功能等價物。

我已經花了整整一天的時間,它開始顯得過度,所以我轉向你們尋求幫助。難道真的很難訪問本地的聯繫人選擇器嗎?

+0

你好,我發現了同樣的插件,我設法得到它與PhoneGap的2.7.0工作。你是否想要修理你的?我現在試圖將它轉換爲phonegap3.0,它給了我一個很好的踢,看到這裏的問題http://stackoverflow.com/questions/18860177/phonegap-3-0-custom-plugin。如果你還沒有把它整理出來,可能有些東西可以幫到你 –

回答

1

等效是this.ctx.getActivity()。getContentResolver()。查詢

下面的代碼在科爾多瓦2.6。

package com.rearden; 

import org.json.JSONArray; 
import org.json.JSONException; 
import org.json.JSONObject; 

import android.app.Activity; 
import android.content.Intent; 
import android.database.Cursor; 
import android.net.Uri; 
import android.provider.ContactsContract; 

import org.apache.cordova.api.Plugin; 
import org.apache.cordova.api.PluginResult; 
import org.apache.cordova.api.PluginResult.Status; 

import android.util.Log; 

public class ContactViewPlugin extends Plugin { 
    private static final int PICK_CONTACT = 1; 
    private String callback; 

    @Override 
    public PluginResult execute(String action, JSONArray args, String callbackId) { 

     startContactActivity(); 
     PluginResult mPlugin = new PluginResult(PluginResult.Status.NO_RESULT); 
     mPlugin.setKeepCallback(true); 
     this.callback = callbackId; 
     return mPlugin; 
    } 

    public void startContactActivity() { 
     Intent intent = new Intent(Intent.ACTION_PICK); 
     intent.setType(ContactsContract.Contacts.CONTENT_TYPE); 
     this.ctx.startActivityForResult((Plugin) this, intent, PICK_CONTACT); 
    } 

    @Override 
    public void onActivityResult(int reqCode, int resultCode, Intent data) { 
     String name = null; 
     String number = null; 
     String email = null; 

     switch (reqCode) { 
     case (PICK_CONTACT): 
      if (resultCode == Activity.RESULT_OK) { 

       Uri contactData = data.getData(); 
       Cursor c = this.ctx.getActivity().getContentResolver().query(contactData, null, null, null, null); 
       if (c.moveToFirst()) { 

        String ContactID = c.getString(c 
          .getColumnIndex(ContactsContract.Contacts._ID)); 
        String hasPhone = c.getString(c.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)); 

        if (Integer.parseInt(hasPhone) == 1) { 

         Cursor phoneCursor = this.ctx.getActivity().getContentResolver().query(
             ContactsContract.CommonDataKinds.Phone.CONTENT_URI, 
             null, 
             ContactsContract.CommonDataKinds.Phone.CONTACT_ID 
               + "='" + ContactID + "'", null, 
             null); 
         while (phoneCursor.moveToNext()) { 
          number = phoneCursor 
            .getString(phoneCursor 
            .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); 
         } 
        } 

        // get email address 
        Cursor emailCur = this.ctx.getActivity().getContentResolver().query( 
          ContactsContract.CommonDataKinds.Email.CONTENT_URI, 
          null, 
          ContactsContract.CommonDataKinds.Email.CONTACT_ID + "='" + ContactID + "'", null,null); 
         while (emailCur.moveToNext()) { 
          // This would allow you get several email addresses 
           // if the email addresses were stored in an array 
          email = emailCur.getString(
              emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA)); 
          //String emailType = emailCur.getString(
           //   emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE)); 
         } 
         emailCur.close(); 

        name = c.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME)); 
        JSONObject contactObject = new JSONObject(); 
        try { 
         contactObject.put("name", name); 
         contactObject.put("phone", number); 
         contactObject.put("email", email); 

        } catch (JSONException e) { 

         e.printStackTrace(); 
        } 

        this.success(new PluginResult(PluginResult.Status.OK, 
          contactObject), this.callback); 
       } 
      } 
      break; 
     } 
    } 
} 

我也改變了JS文件。

var ContactViewPlugin = function() { 
}; 

ContactViewPlugin.prototype.show = function(successCallback, failureCallback) { 

    console.log('calling *** show'); 

    return cordova.exec(successCallback,  
      failureCallback, 
      'ContactViewPlugin', 
      'show', 
      []); 

}; 


if(!window.plugins) { 
    window.plugins = {}; 
} 
if (!window.plugins.contactViewPlugin) { 
    window.plugins.contactViewPlugin = new ContactViewPlugin(); 
} 

不要忘了將插件添加到config.xml列表中。

+0

我可以找到任何一個例子,我可以看到所有的代碼,我是手機新手,我想做的完全一樣的東西 – Uahmed

0

科爾多瓦3.0.0 這個工作對我來說

this.cordova.startActivityForResult((CordovaPlugin) this, intent, PICK_CONTACT);