PhoneGap的Andr​​oid的联系人选择器插件(更新插件) [英] Phonegap Android Contact Picker Plugin (updating a plugin)

查看:152
本文介绍了PhoneGap的Andr​​oid的联系人选择器插件(更新插件)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的PhoneGap的应用程序,我试图让原生的Andr​​oid联系人选择器来启动,所以我可以取一些电话号码。

In my PhoneGap app, I am trying to get the native Android contact picker to launch so I can fetch some phone numbers.

我研究了网上,但没有找到太多,直到我看到了 ContactView 插件: https://github.com/phonegap/phonegap-plugins/tree/master /安卓/ ContactView

I researched online and couldn't find much until I saw the ContactView plugin: https://github.com/phonegap/phonegap-plugins/tree/master/Android/ContactView

当我按说明书设置的插件,我到处在其ContactView.java文件中遇到的错误。看来,它使用一个很旧版本的插件结构,CTX等德precated命令(例如startActivityForResult)的。

When I set up the plugin according to the instructions, I encountered errors everywhere in its ContactView.java file. It seems that it is using a very old version of the plugin structure with ctx and other deprecated commands (e.g. startActivityForResult).

于是,我就用它通过它逐行转换成现代的插件,但我太的n00b,并卡住了约40行(内startContactActivity功能)。这是我到目前为止有:

So I tried to convert it into a modern plugin by going through it line by line but I was too n00b and got stuck at about line 40 (inside the startContactActivity function). This is what I have so far:

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功能。

What I don't seem to be able to do is to find an equivalent for the "this.ctx.startActivityForResult" function.

我已经花了在这整个一天,它开始显得过大,所以我求助于你们的帮助。难道真的是很难进入本地联系人选择器?

I've already spent an entire day on this and it's starting to seem excessive, so I turn to you guys for help. Can it really be that hard to access the native contact picker?

推荐答案

应的是 this.ctx.getActivity()。getContentResolver()查询

以下作品中的code科尔多瓦2.6。

The code below works in cordova 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文件。

I also changed the JS file.

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列表。

Do not forget to add the plugin to the config.xml list.

这篇关于PhoneGap的Andr​​oid的联系人选择器插件(更新插件)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆