首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用Visual Studio社区显示来自Cordova-Calllog-Plugin for Android的响应

如何使用Visual Studio社区显示来自Cordova-Calllog-Plugin for Android的响应
EN

Stack Overflow用户
提问于 2016-07-06 03:07:12
回答 0查看 360关注 0票数 1

我是一个新的Cordova-Plugins学习者,并试图获得电话呼叫记录和显示在Html表中。我在Visual Studio中创建了一个TypeScript项目,并添加了来自git的Cordova-Calllog-Plugin。我修改了下面给出的默认index.html页面,但它不起作用。请帮我把它修好。我花了很多时间在上面,但是失败了。以下是我的代码

代码语言:javascript
复制
    <body>
    <div>
        <h1>Call History</h1>
        <div id="deviceready">
            <p class="event listening">Connecting to Device</p>
            <p class="event received">Device is Ready</p><hr />
            <button onclick="loadLogs();">Display History</button>
            <div id="id01"></div>
        </div>
    </div>
    <script type="text/javascript" src="cordova.js"></script>
    <script type="text/javascript" src="scripts/platformOverrides.js"></script>
    <script type="text/javascript" src="scripts/appBundle.js"></script>
    <script>
        function loadLogs() {
            var out = "<table style='border:solid;'><thead><tr><td>Number</td><td>Date</td><td>Type</td></tr></thead>";
            document.getElementById("id01").innerHTML = out;
            window.plugins.calllog.list(1, function (response)
            {
               alert("Going to use the response and add rows to the Html Tble table ");
               alert(response); // its giving the response [Object Object]
               // checked the length of response
               var length = 0;
               for (var i in response) {if (isFinite(i) && i > length) length = i; }
               alert("response length:" + length); // its giving response length:0

                // Assumed that the response is a JSON Object so tried to parse it
                var arr = JSON.parse(response);
                alert("Array Lenth:" + arr.length); // Its not showing anything
                   var i;
                   for (i = 0; i < arr.length; i++) {
                       alert("Loop Started:" + i); // to check if entering in the loop but its not reaching here
                       out += "<tr><td>" + arr[i].number + "</td><td>" + arr[i].date + "</td><td>" + arr[i].type + "</td></tr>";
                       alert(out); // just to check result of each row
                   }
                   out += "</table>";
                   document.getElementById("id01").innerHTML = out;
                   alert(out);
                   alert("Successfull");
            }, function (error)
            {
                // Damn got an error. useful print it
            });
            alert("Finished")
        }
    </script>
</body>

它现在起作用了。非常感谢phonelog在这方面帮了我的忙。完整的工作代码在这里,根据Answer#1中给出的建议进行了修改。

代码语言:javascript
复制
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Security-Policy" content="default-src 'self' data: gap: https://ssl.gstatic.com 'unsafe-eval'; style-src 'self' 'unsafe-inline'; media-src *">
    <meta name="format-detection" content="telephone=no">
    <meta name="msapplication-tap-highlight" content="no">
    <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width">
    <link rel="stylesheet" type="text/css" href="css/index.css">
    <title>Call History</title>
    <script type="text/javascript" charset="utf-8" src="cordova.js"></script>
    <script type="text/javascript" src="scripts/platformOverrides.js"></script>
    <script type="text/javascript" src="scripts/appBundle.js"></script>
    <script type="text/javascript" charset="utf-8">
        function onLoad() { document.addEventListener("deviceready", onDeviceReady, false); }
        function onDeviceReady() { alert("Hello Stage#1 DeviceReadyEvent Fired and Device is Ready") }
        function loadLogs() {
            var out = "<table><thead><tr><td>Number</td><td>Date</td><td>Type</td></tr></thead>";
            document.getElementById("id01").innerHTML = out;
            window.plugins.calllog.list(2, function (response)
            {
                var rows = response.rows;                
                for (i = 0; i < rows.length; i++)
                    { out += "<tr><td>" + rows[i].number + "</td><td>" + rows[i].date + "</td><td>" + rows[i].type + "</td></tr>"; }
                document.getElementById("id01").innerHTML = out + "</table>";
            }, function (error) {
                alert("There is something wrong")
            });
        }
    </script>
</head>
<body onload="onLoad()">
    <h1>Call History</h1>
    <div id="deviceready">
        <p class="event listening">Connecting to Device ... </p>
        <p class="event received">Device is Ready</p>        
    </div>   
    <button onclick="loadLogs();">Click here to get Call History</button>
    <div id="id01"></div>
</body>
</html>

这是Calllog.js

代码语言:javascript
复制
function CallLog() {
}

CallLog.prototype.list = function (period, successCallback, errorCallback) {
  cordova.exec(successCallback, errorCallback, "CallLog", "list", [period]);
};

CallLog.prototype.contact = function (phoneNumber, successCallback, errorCallback) {
  cordova.exec(successCallback, errorCallback, "CallLog", "contact", [phoneNumber]);
};

CallLog.prototype.show = function (phoneNumber, successCallback, errorCallback) {
  cordova.exec(successCallback, errorCallback, "CallLog", "show", [phoneNumber]);
};

CallLog.prototype.delete = function (id, successCallback, errorCallback) {
  cordova.exec(successCallback, errorCallback, "CallLog", "delete", [id]);
};

CallLog.prototype.insert = function (args, successCallback, errorCallback) {
  cordova.exec(successCallback, errorCallback, "CallLog", "insert", args);
};

CallLog.install = function () {
  if (!window.plugins) {
    window.plugins = {};
  }

  window.plugins.calllog = new CallLog();
  return window.plugins.calllog;
};

cordova.addConstructor(CallLog.install);

这是CallLogPlugin.java

代码语言:javascript
复制
package com.ubookr.plugins;

import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.provider.ContactsContract.Intents;
import android.provider.ContactsContract.PhoneLookup;
import android.util.Log;
import android.content.pm.PackageManager;
import static android.Manifest.permission.READ_CALL_LOG;

import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.PluginResult;
import org.apache.cordova.PluginResult.Status;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.Calendar;
import java.util.Date;

public class CallLogPlugin extends CordovaPlugin {

    private static final String ACTION_LIST = "list";
    private static final String ACTION_CONTACT = "contact";
    private static final String ACTION_SHOW = "show";
    private static final String ACTION_DELETE = "delete";
    private static final String ACTION_INSERT = "insert";
    private static final String TAG = "CallLogPlugin";
    // Permission request stuff.
    private static final int READ_CALL_LOG_REQ_CODE = 0;
    private static final String PERMISSION_DENIED_ERROR =
        "User refused to give permissions for reading call log";
    // Exec arguments.
    private CallbackContext callbackContext;
    private JSONArray args;
    private String action;

    public static final String READ_CALL_LOG =
        android.Manifest.permission.READ_CALL_LOG;

    @Override
    public boolean execute(String action, JSONArray args,
            final CallbackContext callbackContext) {

        Log.d(TAG, "execute called");

        this.action = action;
        this.args = args;
        this.callbackContext = callbackContext;

        if (cordova.hasPermission(READ_CALL_LOG)) {
            Log.d(TAG, "Permission available");
            executeHelper();
        } else {
            Log.d(TAG, "No permissions, will request");
            cordova.requestPermission(this, READ_CALL_LOG_REQ_CODE,
                    READ_CALL_LOG);
        }
        return true;
    }

    private void executeHelper() {
        Log.d(TAG, "executeHelper with action " + action);
        if (ACTION_CONTACT.equals(action)) {
            contact();
        } else if (ACTION_SHOW.equals(action)) {
            show();
        } else if (ACTION_LIST.equals(action)) {
            list();
        } else if (ACTION_DELETE.equals(action)) {
            delete();
        } else if (ACTION_INSERT.equals(action)) {
            insert();
        } else {
            Log.d(TAG, "Invalid action: " + action + " passed");
            callbackContext.sendPluginResult(
                    new PluginResult(Status.INVALID_ACTION));
        }
    }

    public void onRequestPermissionResult(
            int requestCode, String[] permissions,
            int[] grantResults) throws JSONException {
        for (int r : grantResults) {
            if (r == PackageManager.PERMISSION_DENIED) {
                Log.d(TAG, "Permission denied");
                callbackContext.sendPluginResult(
                        new PluginResult(PluginResult.Status.ERROR,
                            PERMISSION_DENIED_ERROR));
                return;
            }
        }
        executeHelper();
            }

    private void show() {
        //        cordova.getThreadPool().execute(new Runnable() {
        cordova.getActivity().runOnUiThread(new Runnable() {
            public void run() {
                PluginResult result;
                try {
                    String phoneNumber = args.getString(0);
                    viewContact(phoneNumber);
                    result = new PluginResult(Status.OK);
                } catch (JSONException e) {
                    Log.d(TAG, "Got JSON Exception " + e.getMessage());
                    result = new PluginResult(Status.JSON_EXCEPTION, e.getMessage());
                } catch (Exception e) {
                    Log.d(TAG, "Got Exception " + e.getMessage());
                    result = new PluginResult(Status.ERROR, e.getMessage());
                }
                callbackContext.sendPluginResult(result);
            }
        });
        }

    private void contact() {
        // TODO: this code path needs to ask user for permission, currently
        // it does not.
        cordova.getThreadPool().execute(new Runnable() {
            public void run() {
                PluginResult result;
                try {
                    final String phoneNumber = args.getString(0);
                    String contactInfo = getContactNameFromNumber(phoneNumber);
                    Log.d(TAG, "Returning " + contactInfo);
                    result = new PluginResult(Status.OK, contactInfo);
                } catch (JSONException e) {
                    Log.d(TAG, "Got JSON Exception " + e.getMessage());
                    result = new PluginResult(Status.JSON_EXCEPTION, e.getMessage());
                }
                callbackContext.sendPluginResult(result);
            }
        });
    }

    private void list() {
        cordova.getThreadPool().execute(new Runnable() {
            public void run() {
                PluginResult result;
                try {
                    String limiter = null;
                    if (!args.isNull(0)) {
                        // make number positive in case caller give negative days
                        int days = Math.abs(Integer.valueOf(args.getString(0)));
                        Log.d(TAG, "Days is: " + days);
                        //turn this into a date
                        Calendar calendar = Calendar.getInstance();
                        calendar.setTime(new Date());
                        calendar.add(Calendar.DAY_OF_YEAR, -days);
                        Date limitDate = calendar.getTime();
                        limiter = String.valueOf(limitDate.getTime());
                    }
                    // Do required search
                    JSONObject callLog = getCallLog(limiter);
                    Log.d(TAG, "Returning " + callLog.toString());
                    result = new PluginResult(Status.OK, callLog);
                } catch (JSONException e) {
                    Log.d(TAG, "Got JSON Exception " + e.getMessage());
                    result = new PluginResult(Status.JSON_EXCEPTION, e.getMessage());
                } catch (NumberFormatException e) {
                    Log.d(TAG, "Got NumberFormatException " + e.getMessage());
                    result = new PluginResult(Status.ERROR, "Non integer passed to list");
                } catch (Exception e) {
                    Log.d(TAG, "Got Exception " + e.getMessage());
                    result = new PluginResult(Status.ERROR, e.getMessage());
                }
                callbackContext.sendPluginResult(result);
            }
        });
    }

    private void viewContact(String phoneNumber) {

        Intent i = new Intent(Intents.SHOW_OR_CREATE_CONTACT,
                Uri.parse(String.format("tel: %s", phoneNumber)));
        cordova.getActivity().startActivity(i);
    }

    private void delete() {
        cordova.getThreadPool().execute(new Runnable() {
            public void run() {
                PluginResult result;
                try {

                    int res = CallLogPlugin.this.cordova.getActivity().getContentResolver().delete(
                        android.provider.CallLog.Calls.CONTENT_URI, "_ID = " + args.getString(0), null);
                    if (res == 1) {
                        result = new PluginResult(Status.OK, res);

                    } else {
                        result = new PluginResult(Status.ERROR, res);
                    }

                } catch (JSONException e) {
                    Log.d(TAG, "Got JSON Exception " + e.getMessage());
                    result = new PluginResult(Status.JSON_EXCEPTION, e.getMessage());
                } catch (Exception e) {
                    Log.d(TAG, "Got Exception " + e.getMessage());
                    result = new PluginResult(Status.ERROR, e.getMessage());
                }
                callbackContext.sendPluginResult(result);
            }
        });
    }

    private void insert() {
        cordova.getThreadPool().execute(new Runnable() {
            public void run() {
                PluginResult result;
                ContentValues values = new ContentValues();
                Uri uri;

                try {
                    values.put(android.provider.CallLog.Calls.NUMBER, args.getString(0));
                    values.put(android.provider.CallLog.Calls.DATE, System.currentTimeMillis());
                    values.put(android.provider.CallLog.Calls.DURATION, args.getInt(1));
                    values.put(android.provider.CallLog.Calls.TYPE, android.provider.CallLog.Calls.OUTGOING_TYPE);
                    values.put(android.provider.CallLog.Calls.NEW, 1);
                    values.put(android.provider.CallLog.Calls.CACHED_NAME, "");
                    values.put(android.provider.CallLog.Calls.CACHED_NUMBER_TYPE, 0);
                    values.put(android.provider.CallLog.Calls.CACHED_NUMBER_LABEL, "");

                    uri = CallLogPlugin.this.cordova.getActivity().getContentResolver().insert(android.provider.CallLog.Calls.CONTENT_URI, values);

                    result = new PluginResult(Status.OK, uri.toString());

                }
                catch (JSONException e) {
                    Log.d(TAG, "Got JSON Exception " + e.getMessage());
                    result = new PluginResult(Status.JSON_EXCEPTION, e.getMessage());
                }
                catch (Exception e) {
                    Log.d(TAG, "Got Exception " + e.getMessage());
                    result = new PluginResult(Status.ERROR, e.getMessage());
                }

                callbackContext.sendPluginResult(result);
            }
        });
    }

    private JSONObject getCallLog(String limiter) 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 = this.cordova.getActivity().getContentResolver().query(
                    android.provider.CallLog.Calls.CONTENT_URI,
                    strFields,
                    limiter == null ? null : android.provider.CallLog.Calls.DATE + ">?",
                    limiter == null ? null : new String[] {limiter},
                    android.provider.CallLog.Calls.DEFAULT_SORT_ORDER);

            int callCount = callLogCursor.getCount();

            if (callCount > 0) {
                JSONObject callLogItem = new JSONObject();
                JSONArray callLogItems = new JSONArray();

                callLogCursor.moveToFirst();
                do {
                    callLogItem.put("date", callLogCursor.getLong(0));
                    callLogItem.put("number", callLogCursor.getString(1));
                    callLogItem.put("type", callLogCursor.getInt(2));
                    callLogItem.put("duration", callLogCursor.getLong(3));
                    callLogItem.put("new", callLogCursor.getInt(4));
                    callLogItem.put("cachedName", callLogCursor.getString(5));
                    callLogItem.put("cachedNumberType", callLogCursor.getInt(6));
                    callLogItem.put("cachedNumberLabel", callLogCursor.getInt(7));
                    //callLogItem.put("name", getContactNameFromNumber(callLogCursor.getString(1))); //grab name too
                    callLogItems.put(callLogItem);
                    callLogItem = new JSONObject();
                } 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 String getContactNameFromNumber(String number) {
        // define the columns I want the query to return
        String[] projection = new String[] {
            PhoneLookup.DISPLAY_NAME
        };

        // encode the phone number and build the filter URI
        Uri contactUri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number));

        // query time
        Cursor c = cordova.getActivity().getContentResolver().query(contactUri, projection, null, null, null);

        // if the query returns 1 or more results
        // return the first result
        if (c.moveToFirst()) {
            String name = c.getString(c.getColumnIndex(PhoneLookup.DISPLAY_NAME));
            c.deactivate();
            return name;
        }

        // return the original number if no match was found
        return number;
    }

    }
EN

回答

页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38210875

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档