我正在使用cordova phonegap 2.1.0
我已下载代码: Phone Gap SMS Read Extension
然后我根据新的cordova库修改了代码
修改了Smsread.js
var SMSReader = function () {};
SMSReader.prototype.getInbox = function(params, success, fail) {
return cordova.exec(function (args) {
success(args);
}, function(args) {
fail(args);
}, 'SMSReader', 'inbox', [params]);
};
SMSReader.prototype.getSent = function(params, success, fail) {
return cordova.exec(function (args) {
success(args);
}, function(args) {
fail(args);
}, 'SMSReader', 'sent', [params]);};
if (!window.plugins) {
window.plugins = {};
}
if (!window.plugins.SMSReader) {
window.plugins.SMSReader = new SMSReader();
}
//cordova.addConstructor(function () {
// cordova.addPlugin("SMSReader", new SMSReader());
// PluginManager.addService("SMSReader", "com.example.sample2");
//});
修改了SMSReader.java
package com.example.sample2;
import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.ContactsContract;
import android.util.Log;
import org.apache.cordova.api.Plugin;
import org.apache.cordova.api.PluginResult;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class SMSReader extends Plugin {
Context ctx;
@Override
public PluginResult execute(String action, JSONArray data, String callbackId) {
Log.d("SMSReadPlugin", "Plugin Called");
PluginResult result = null;
JSONObject messages = new JSONObject();
if (action.equals("inbox")) {
try {
Log.d("SMSReadPlugin", "TRY Called");
messages = readSMS("inbox");
Log.d("SMSReadPlugin", "Returning " + messages.toString());
result = new PluginResult(PluginResult.Status.OK, messages);
} catch (JSONException jsonEx) {
Log.d("SMSReadPlugin", "Got JSON Exception "+ jsonEx.getMessage());
result = new PluginResult(PluginResult.Status.JSON_EXCEPTION);
}
}
else if(action.equals("sent")){
try {
messages = readSMS("sent");
Log.d("SMSReadPlugin", "Returning " + messages.toString());
result = new PluginResult(PluginResult.Status.OK, messages);
} catch (JSONException jsonEx) {
Log.d("SMSReadPlugin", "Got JSON Exception "+ jsonEx.getMessage());
result = new PluginResult(PluginResult.Status.JSON_EXCEPTION);
}
}
else {
result = new PluginResult(PluginResult.Status.INVALID_ACTION);
Log.d("SMSReadPlugin", "Invalid action : "+action+" passed");
}
return result;
}
//Read messages from inbox/or sent box.
private JSONObject readSMS(String folder) throws JSONException {
JSONObject data = new JSONObject();
Uri uriSMSURI = Uri.parse("");
if(folder.equals("inbox")){
uriSMSURI = Uri.parse("content://sms/inbox");
Log.d("SMSReadPlugin", "Inbox Called");
}
else if(folder.equals("sent")){
uriSMSURI = Uri.parse("content://sms/sent");
Log.d("SMSReadPlugin", "Sent Called");
}
Log.d("SMSReadPlugin", "content Before");
// Cursor cur = getContentResolver().query(uriSMSURI, null, null, null,null);
Cursor cur = getContentResolver().query(uriSMSURI, null, null, null, null);
Log.d("SMSReadPlugin", "content Called");
JSONArray smsList = new JSONArray();
data.put("messages", smsList);
while (cur.moveToNext()) {
Log.d("SMSReadPlugin", "cursor Called");
JSONObject sms = new JSONObject();
sms.put("number",cur.getString(2));
sms.put("text",cur.getString(11));
String name = getContact(cur.getString(2));
if(!name.equals("")){
sms.put("name",name);
}
smsList.put(sms);
}
return data;
}
private String getContact(String number){
Cursor cur = this.cordova.getActivity().getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,null,null,null,null);
String returnName= "";
if(cur.getCount() > 0){
while(cur.moveToNext()){
String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
Log.d("Contact","ID:" + id );
String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
Log.d("Contact","name:" + name);
if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
Cursor pcur = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,
ContactsContract.CommonDataKinds.Phone.NUMBER + "=?",new String[]{number},null);
int numindex = pcur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DATA);
if(pcur.moveToFirst()){
String dbNum = pcur.getString(numindex);
if(dbNum.equals(number)){
returnName = name;
Log.d("number","number:" + dbNum);
}
else {
Log.d("number","numbers dont match!");
}
}
else {
Log.d("number", "no result");
}
}
else {
Log.d("number", "No Number");
}
}
}
return returnName;
}
private ContentResolver getContentResolver(){
return ctx.getContentResolver();
}
}
仍然无法得到结果。请帮助。