我刚开始使用phoneGap和Android。建立基本样本。
我想知道,如果有API来获取通话记录。我希望创建一个网格显示:
提前致谢。
答案 0 :(得分:24)
我做了一些研究并成功构建了一个PhoneGap插件,可以从android.provider.CallLog
获取CallLog。
这将返回一个JSON { Rows: [] }
,其中Rows
是一个包含以下字段(作为数组)的二维调用记录数组,按以下顺序排列:
详情请见http://developer.android.com/reference/android/provider/CallLog.Calls.html
我还使用此插件制作了一个小样本,它会显示拨出电话,未接来电和来电的总数,并将它们绘制在饼图中。该示例使用的是FusionCharts的饼图。
您可以从以下网址下载试用版.apk:
http://www.sudipto.net/download/android/apps/CallLog/beta/CallChart.apk.zip
(使用适用于Android 3或更高版本的JavaScript SVG图表)
以下是您需要深入研究的源代码zip:
http://www.sudipto.net/download/android/apps/CallLog/beta/calllog_phonegap_eclipseclassic_source.zip
这是我的完整代码:
CallLog.java
package com.fusioncharts.phonegap.plugin;
import org.json.*;
import android.database.*;
import android.util.Log;
import com.phonegap.api.Plugin;
import com.phonegap.api.PluginResult;
import com.phonegap.api.PluginResult.Status;
public class CallLog extends Plugin {
@Override
public PluginResult execute(String actionName, JSONArray arguments, String callback)
{
JSONObject callLogs = new JSONObject();
PluginResult result = null;
try {
switch (getActionItem(actionName))
{
case 1:
callLogs = getAllCallLog(arguments);
result = new PluginResult(Status.OK, callLogs);
break;
default:
result = new PluginResult(Status.INVALID_ACTION);
}
} catch (JSONException jsonEx) {
result = new PluginResult(Status.JSON_EXCEPTION);
}
return result;
}
private JSONObject getAllCallLog(JSONArray requirements) throws JSONException
{
JSONObject callLog = new JSONObject();
String[] strFields = {
android.provider.CallLog.Calls.DATE,
android.provider.CallLog.Calls.NUMBER,
android.provider.CallLog.Calls.TYPE,
android.provider.CallLog.Calls.DURATION,
android.provider.CallLog.Calls.NEW,
android.provider.CallLog.Calls.CACHED_NAME,
android.provider.CallLog.Calls.CACHED_NUMBER_TYPE,
android.provider.CallLog.Calls.CACHED_NUMBER_LABEL//,
};
try {
Cursor callLogCursor = ctx.getContentResolver().query(
android.provider.CallLog.Calls.CONTENT_URI,
strFields,
null,
null,
android.provider.CallLog.Calls.DEFAULT_SORT_ORDER
);
int callCount = callLogCursor.getCount();
if(callCount>0){
JSONArray callLogItem = new JSONArray();
JSONArray callLogItems = new JSONArray();
String[] columnNames = callLogCursor.getColumnNames();
callLogCursor.moveToFirst();
do
{
callLogItem.put(callLogCursor.getLong(0));
callLogItem.put(callLogCursor.getString(1));
callLogItem.put(callLogCursor.getInt(2));
callLogItem.put(callLogCursor.getLong(3));
callLogItem.put(callLogCursor.getInt(4));
callLogItem.put(callLogCursor.getString(5));
callLogItem.put(callLogCursor.getInt(6));
callLogItems.put(callLogItem);
callLogItem = new JSONArray();
}while(callLogCursor.moveToNext());
callLog.put("Rows", callLogItems);
}
callLogCursor.close();
}catch(Exception e)
{
Log.d("CallLog_Plugin", " ERROR : SQL to get cursor: ERROR " + e.getMessage());
}
return callLog;
}
private JSONObject getTimeRangeCallLog(JSONArray requirements)
{
private int getActionItem(String actionName) throws JSONException
{
JSONObject actions = new JSONObject("{'all':1,'last':2,'time':3}");
if (actions.has(actionName))
return actions.getInt(actionName);
return 0;
}
}
calllog.phonegap.js
var CallLog = function() {};
CallLog.prototype.all = function(params, successCallback, failureCallback)
{
return PhoneGap.exec(successCallback, failureCallback, 'CallLog', 'all', [params]);
};
PhoneGap.addConstructor( function() {
PhoneGap.addPlugin("calllog", new CallLog());
PluginManager.addService("CallLog","com.fusioncharts.phonegap.plugin.CallLog");
});
Application.java
var CallLog = function() {};
CallLog.prototype.all = function(params, successCallback, failureCallback)
{
/* @param successCallback
* @param failureCallback
* @param plugin name
* @param action
* @param JSONArray of parameters
*/
return PhoneGap.exec(successCallback, failureCallback, 'CallLog', 'all', [params]);
};
PhoneGap.addConstructor( function() {
//Register the javascript plugin with PhoneGap
PhoneGap.addPlugin("calllog", new CallLog());
//Register the native class of plugin with PhoneGap
PluginManager.addService("CallLog","com.fusioncharts.phonegap.plugin.CallLog");
});
答案 1 :(得分:4)
在phonegap中没有标准的API来实现你想要做的事情,但你总是可以为你的平台编写一个插件并从android API获取信息。 (我实际上不知道你是否可以在android上获得你需要的信息)。
以下是有关如何编写phonegap插件的链接:http://wiki.phonegap.com/w/page/36752779/PhoneGap-Plugins