堪培拉的风
堪培拉的风
  • 发布:2015-02-04 14:20
  • 更新:2019-11-07 11:38
  • 阅读:2410

有使用native.js获取通话记录的案例吗?求指点啊!

分类:Native.js

//获取主activity
var CallLog = plus.android.importClass("android.provider.CallLog");
var main = plus.android.runtimeMainActivity();
var obj = main.getContentResolver();
//查询
obj.query(CallLog.Calls.CONTENT_URI, null, null, null, null);

查询的时候报“Uncaught TypeError: Object #<h> has no method 'query' at commontel/commontel_index.html:34”的错误?有谁知道这里是哪里错了?求指点!

2015-02-04 14:20 负责人:无 分享
已邀请:
IDo

IDo

试试这样:
function getCallLog() {
var CallLog = plus.android.importClass("android.provider.CallLog");
var main = plus.android.runtimeMainActivity();
var obj = main.getContentResolver();
plus.android.importClass(obj);
//查询
var cursor = obj.query(CallLog.Calls.CONTENT_URI, null, null, null, null);
var SimpleDateFormat = plus.android.importClass("java.text.SimpleDateFormat");
var Date = plus.android.importClass("java.util.Date");
var Long = plus.android.importClass("java.util.Long");
plus.android.importClass(cursor);
var count =0;
if (cursor.moveToFirst()) {
while (cursor.moveToNext()){
count++;
//号码
var number = cursor.getString(cursor.getColumnIndex(CallLog.Calls.NUMBER));
//呼叫类型
var type;
switch (cursor.getString(cursor.getColumnIndex(CallLog.Calls.TYPE))) {
case CallLog.Calls.INCOMING_TYPE:
type = "呼入";
break;
case CallLog.Calls.OUTGOING_TYPE:
type = "呼出";
break;
case CallLog.Calls.MISSED_TYPE:
type = "未接";
break;
default:
type = "挂断"; //应该是挂断.根据我手机类型判断出的
break;
}
var sfd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
var Date_Col = cursor.getColumnIndexOrThrow(CallLog.Calls.DATE);
var Date_Value = cursor.getString(Date_Col);
var t = parseInt(Date_Value);
//呼叫时间
var time = sfd.format(new Date(t));
//联系人
var Name_Col = cursor.getColumnIndexOrThrow(CallLog.Calls.CACHED_NAME);
var name = cursor.getString(Name_Col);
//通话时间,单位:s
var Duration_Col = cursor.getColumnIndexOrThrow(CallLog.Calls.DURATION);
var duration = cursor.getString(Duration_Col);
console.log(time + " " + ((name == null || name == '')?number:name) + type + " " + duration);
if(count > 50){
break;
}
}
}
}

  • 堪培拉的风 (作者)

    非常感谢,我试试!

    2015-02-10 09:18

  • yearconxinxi@163.com

    报错啊: Uncaught java.lang.SecurityException: Permission Denial: reading com.android.providers.contacts.CallLogProvider uri content://call_log/calls from pid=3479, uid=10174 requires android.permission.READ_CALL_LOG, or grantUriPermission();at android.app.ContextImpl$ApplicationContentResolver.query at HTML/unit/test2.html:1

    2017-07-17 10:50

  • 剑锋

    回复 yearconxinxi@163.com: 你要,我也 报这个提示,有解决方法么?谢谢

    2019-08-04 23:48

1208868230@qq.com

1208868230@qq.com

楼上大佬的代码亲测成功。还需要:manifest.json添加targetSdkVersion:26.然后requestPermissions请求权限确保有权限,我也是授权啥都有,但还是报错没权限,这么解决的。

  • 236154186@qq.com

    26是安卓8,我华为荣耀9是安卓7,按你说的设置了,还是报错没权限!不知咋整!

    2019-11-07 12:19

  • 1208868230@qq.com

    回复 236154186@qq.com: 我用荣耀10和iphoneX测了,都能成功取到通话记录......反正就是这个权限问题,报错就是没权限。手机系统设置里面给权限不算数,必须要requestPermissions提示权限都有了,才是有权限。

    2019-11-07 14:36

  • 1208868230@qq.com

    回复 1208868230@qq.com: 不对不对,说错了,iphoneX是测的电话本,ios取不到通话记录,哈哈

    2019-11-07 14:37

  • 1208868230@qq.com

    再补充一下,必须要用自定义调试基座,先打包一个自定义基座再试。使用默认官方的真机调试会报没授权!

    2020-02-12 12:39

该问题目前已经被锁定, 无法添加新回复