在我的 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”函数的等效项。
我已经在这上面花了一整天了,而且它开始看起来有点过分了,所以我向你们寻求帮助。访问 native 联系人选择器真的有那么难吗?
最佳答案
等同于this.ctx.getActivity().getContentResolver().query
下面的代码适用于 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 文件。
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 列表中。
关于android - Phonegap Android 联系人选择器插件(更新插件),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16621105/