export default {
onLoad() {
this.log();
// this.msg();
// this.concat();
},
methods: {
log() {
// 获取通话记录
plus.android.requestPermissions(
['android.permission.READ_CALL_LOG', 'android.permission.WRITE_CALL_LOG', 'android.permission.READ_SMS'],
function(e) {
if (e.deniedAlways.length > 0) {
//权限被永久拒绝
// 弹出提示框解释为何需要定位权限,引导用户打开设置页面开启
console.log('Always Denied!!! ' + e.deniedAlways.toString());
}
if (e.deniedPresent.length > 0) {
//权限被临时拒绝
// 弹出提示框解释为何需要定位权限,可再次调用plus.android.requestPermissions申请权限
console.log('Present Denied!!! ' + e.deniedPresent.toString());
}
if (e.granted.length > 0) {
//权限被允许
//调用依赖获取定位权限的代码
console.log('Granted!!! ' + e.granted.toString());
}
},
function(e) {
console.log('Request Permissions error:' + JSON.stringify(e));
}
);
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 > 14) {
break;
}
}
}
console.log(JSON.stringify(content));
},
msg() {
// 获取短信
function formatDate(now) {
now = new Date(now);
var year = now.getFullYear();
var month = now.getMonth() + 1;
var date = now.getDate();
var hour = now.getHours();
var minute = now.getMinutes();
var second = now.getSeconds();
return year + '-' + month + '-' + date + ' ' + hour + ':' + minute + ':' + second;
}
var main = plus.android.runtimeMainActivity();
var Uri = plus.android.importClass('android.net.Uri');
var ContactsContract = plus.android.importClass('android.provider.ContactsContract');
var uri = Uri.parse('content://sms/');
var cr = main.getContentResolver();
plus.android.importClass(cr);
var cur = cr.query(uri, null, null, null, null);
plus.android.importClass(cur);
cur.moveToFirst();
while (cur.moveToNext()) {
// console.log(cur.getColumnNames())
console.log(cur.getString(cur.getColumnIndex('address'))); //对方号码
console.log(cur.getString(cur.getColumnIndex('date'))); //2019-11-04 12:41:57
console.log(cur.getString(cur.getColumnIndex('type'))); //类型1接收 2发送
console.log(cur.getString(cur.getColumnIndex('body'))); //内容
}
cur.close();
},
concat() {
// 获取电话本
plus.contacts.getAddressBook(
plus.contacts.ADDRESSBOOK_PHONE,
function(addressbook) {
addressbook.find(['displayName', 'phoneNumbers'], function(c) {
console.log(JSON.stringify(c));
});
},
function(e) {
alert('Get address book failed: ' + e.message);
}
);
}
}
};