近期公司业务调整,让我做一个手机通话记录获取并上传的功能,手机通话记录数据库中大部分信息都可以获取到,唯独参与通话的本机号码拿不到,让人很是头疼,先记录一下。
代码如下
//获取通话记录
private void getContentCallLog() {
Cursor cursor = getContentResolver().query(callUri, // 查询通话记录的URI
// columns 数据库列选择 不选则获取所有列
null
, null, null, CallLog.Calls.DEFAULT_SORT_ORDER// 按照时间逆序排列,最近打的最先显示
);
Log.i(TAG, "cursor count:" + cursor.getCount());
int i = 0;
while (cursor.moveToNext() && i < 20) {
i++;
String name = cursor.getString(cursor.getColumnIndex(CallLog.Calls.CACHED_NAME)); //姓名
String number = cursor.getString(cursor.getColumnIndex(CallLog.Calls.NUMBER)); //号码
long dateLong = cursor.getLong(cursor.getColumnIndex(CallLog.Calls.DATE)); //获取通话日期
String date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(dateLong));
String time = new SimpleDateFormat("HH:mm").format(new Date(dateLong));
int duration = cursor.getInt(cursor.getColumnIndex(CallLog.Calls.DURATION));//获取通话时长,值为多少秒
int type = cursor.getInt(cursor.getColumnIndex(CallLog.Calls.TYPE)); //获取通话类型:1.呼入2.呼出3.未接
String dayCurrent = new SimpleDateFormat("dd").format(new Date());
String dayRecord = new SimpleDateFormat("dd").format(new Date(dateLong));
String phone_account_address = cursor.getString(cursor.getColumnIndex("phone_account_address"));//本机号码可能获取不到(华为、oppo获取不到)
String phone_account_id = cursor.getString(cursor.getColumnIndex(CallLog.Calls.PHONE_ACCOUNT_ID));//本机sim卡id,即ICCID
String phone_account_hidden = cursor.getString(cursor.getColumnIndex("phone_account_hidden"));
//
Log.i(TAG, "Call log: " + "\n"
+ "name: " + name + "\n"
+ "date: " + date + "\n"
+ "phone number: " + number + "\n"
+ "phone_account_address: " + phone_account_address + "\n"
+ "phone_account_id: " + phone_account_id + "\n"
+ "phone_account_hidden: " + phone_account_hidden + "\n"
);
}
}
String phone_account_address = cursor.getString(cursor.getColumnIndex("phone_account_address"));//本机号码可能获取不到(华为、oppo获取不到)
String phone_account_id = cursor.getString(cursor.getColumnIndex(CallLog.Calls.PHONE_ACCOUNT_ID));//本机sim卡id,即ICCID 可能获取不到(华为、oppo获取不到)
这两条信息在华为手机上获取不到,有了解的大神帮忙指点一下