在我的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 :(得分:1)
等效于 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列表中。
答案 1 :(得分:0)
在cordova 3.0.0中 这对我有用
this.cordova.startActivityForResult((CordovaPlugin) this, intent, PICK_CONTACT);