Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
165 views
in Technique[技术] by (71.8m points)

android - PhoneGap API to query call-logs

I have just started with phoneGap and Android. Built basic samples.

I would like to know, if there is an API to get the call logs. I wish to create a grid showing:

  • Number of missed calls over a period of time
  • Number of received calls
  • Number of calls made
  • Total time of the received calls and calls made

Thanks in advance.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I did a bit of research and has successfully built a PhoneGap plugin which would fetch the CallLog from android.provider.CallLog.

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 :

  • Date (as UNIX Time stamp)
  • Number,
  • Type (1- incoming, 2- outgoing, 3- missed)
  • Duration (in seconds)
  • New
  • Cached Name
  • Cached number type
  • Cached number label

Details are in http://developer.android.com/reference/android/provider/CallLog.Calls.html

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.

You can download a beta try-out .apk from :

http://www.sudipto.net/download/android/apps/CallLog/beta/CallChart.apk.zip

(using JavaScript SVG charts that works in Android version 3 or above)

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

Here is my complete code:

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");
});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...