uniapp获取通话记录 报错

她丶 发布于 2022/07/25 18:24
阅读 751
收藏 0

1024程序员节,OpenHarmony Meetup,技术盛宴,不见不散

 

getContacts:function(){
              
                 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
                 );
                 plus.android.importClass(cursor);
                 var content = []; // 用来存储数据
                 var count = 0; // 记录多少条
                 if (cursor.moveToFirst()) {
                    while (cursor.moveToNext()) {
                      count++;
                      //号码
                      var number = cursor.getString(cursor.getColumnIndex(CallLog.Calls.NUMBER));
                      
                      //呼叫类型
                      var type;
                      switch (
                        parseInt(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 date = new Date(parseInt(
                            cursor.getString(cursor.getColumnIndexOrThrow(CallLog.Calls.DATE)))
                         );
                        
                         // 联系人
                         var Name_Col = cursor.getColumnIndexOrThrow(CallLog.Calls.CACHED_NAME);
                         var name = cursor.getString(Name_Col);
                        
                         // 号码归属地 返回:北京 联通
                         var numberLocation = cursor.getString(
                            cursor.getColumnIndex(CallLog.Calls.GEOCODED_LOCATION)
                         );
                        
                         //通话时间,单位:s
                         var Duration_Col = cursor.getColumnIndexOrThrow(CallLog.Calls.DURATION);
                         var duration = cursor.getString(Duration_Col);
                        
                         // 存入数组
                         content.push({
                            name: name, // 联系人的姓名
                            mobile: number, // 联系人电话
                            numberLocation: numberLocation, // 号码的归属地
                            callTime: new Date().getTime(date), // 呼入或呼出时间
                            talkTime: duration, // 通话时长
                            type: type
                         });
                        
                         // 查询50条 就跳出
                         if (count > 50) {
                            break;
                          }
                        }
                      }
                      
                      //将获取到的通话记录赋值
                      console.log(content)
                
            }

 

总是报错 TypeError: Cannot read property '__TYPE__' of null 请问是什么问题?

怎么解决呢?

 

加载中
0
Diluc
Diluc

获取授权了吗?先获取通信权限才能获取列表

plus.android.requestPermissions([  
    'android.permission.READ_CALL_LOG',  
    'android.permission.WRITE_CALL_LOG',  
    'android.permission.CALL_PHONE'  
], res => {  
    if (res.granted.length > 0) {  
        console.log('"已授权");  
                // this.getCallRecord({success:(res)=>{  
                // ...  
                // }});  
    } else {  
        console.log("请先授权!");  
    }  
})
OSCHINA
登录后可查看更多优质内容
返回顶部
顶部