在我的 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”函数的等效项。
我已经花了一整天的时间在这上面,它开始显得过分了,所以我向你们寻求帮助。访问本机联系人选择器真的那么难吗?