用于查询通话记录的 PhoneGap API [英] PhoneGap API to query call-logs
问题描述
我刚刚开始使用 phoneGap 和 Android.构建基本示例.
I have just started with phoneGap and Android. Built basic samples.
我想知道,是否有用于获取通话记录的 API.我想创建一个网格显示:
I would like to know, if there is an API to get the call logs. I wish to create a grid showing:
- 一段时间内未接来电的次数
- 接听电话次数
- 拨打的电话次数
- 接听电话和拨打电话的总时间
提前致谢.
推荐答案
我做了一些研究并成功构建了一个 PhoneGap 插件,它可以从 android.provider.CallLog
获取 CallLog.
I did a bit of research and has successfully built a PhoneGap plugin which would fetch the CallLog from android.provider.CallLog
.
这将返回一个 JSON { Rows: [] }
其中 Rows
是一个二维的呼叫记录数组,包含以下字段(作为数组),顺序如下:
This returns an JSON { Rows: [] }
where Rows
is an 2 dimensional array of call records containing the following fields (as Array) in the following order :
- 日期(作为 UNIX 时间戳)
- 数量,
- 类型(1-传入,2-传出,3-未命中)
- 持续时间(以秒为单位)
- 新
- 缓存名称
- 缓存号码类型
- 缓存号码标签
详情在 http://developer.android.com/reference/android/provider/CallLog.Calls.html
我还使用此插件制作了一个小示例,该示例将显示拨出电话、未接电话和拨入电话的总数,并将它们绘制在饼图中.示例使用的是 FusionCharts 的饼图.
I have also made a small sample using this plugin which would show total number of outgoing calls, missed calls and incoming calls and plot them in a Pie chart. The sample is using FusionCharts' Pie chart.
您可以从以下位置下载 Beta 版试用版 .apk:
You can download a beta try-out .apk from :
http://www.sudipto.net/下载/android/apps/CallLog/beta/CallChart.apk.zip
(使用适用于 Android 3 或更高版本的 JavaScript SVG 图表)
(using JavaScript SVG charts that works in Android version 3 or above)
这是供您深入研究的源代码 zip:
Here is the source-code zip for you to delve into:
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");
});
这篇关于用于查询通话记录的 PhoneGap API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!