我正在尝试制作一个用于获取Android手机通话记录的cordova插件。 我已经按照本教程来获取插件的java代码: http://www.androidhub4you.com/2013/09/android-call-history-example-how-to-get.html?showComment=1402049673596#c4881780435926453105
我的java文件是:
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.CallbackContext;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.Date;
import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.CallLog;
import android.content.ContentResolver;
public class CallLogList extends CordovaPlugin {
@Override
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
if (action.equals("callLog")) {
String message = args.getString(0);
this.getCallDetails(message, callbackContext);
return true;
}
return false;
}
private void getCallDetails(String message, CallbackContext callbackContext) {
if (message != null && message.length() > 0) {
StringBuffer sb = new StringBuffer();
Cursor managedCursor = getContentResolver().query(CallLog.Calls.CONTENT_URI, null, null, null, null);
managedCursor.close();
}
else {
callbackContext.error("Expected one non-empty string argument.");
}
}
}
我总是收到以下错误:
[javac] Compiling 4 source files to /Users/xxx/Projects/cordova/platforms/android/ant-build/classes
[javac] /Users/xxx/Projects/cordova/platforms/android/src/it/prova/calllog/CallLogList.java:41: cannot find symbol
[javac] symbol : method getContentResolver()
[javac] location: class it.prova.calllog.CallLogList
[javac] Cursor managedCursor = getContentResolver().query(CallLog.Calls.CONTENT_URI, null, null, null, null);
[javac] ^
[javac] 1 error
我想我缺少一些导入,因为我在一个原生Android应用程序中运行相同的代码并且它有效。
由于
答案 0 :(得分:3)
我找到了解决方案!
我必须添加:this.cordova.getActivity()
才能创建新的Cursor。
Cursor managedCursor = this.cordova.getActivity().getContentResolver().query(CallLog.Calls.CONTENT_URI, null, null, null, null);