Android端通话记录读取

Android端读取手机通话记录,直接上代码:

public classCallInfo {

publicStringname;

publicStringnumber;//号码

public longdate;//日期

public inttype;//类型:来电、去电、未接

publicStringduration;

private staticCallInfosingleton=null;

public static synchronizedCallInfogetInstance(String name,String number, longdate, inttype,String duration){

// if(singleton==null){

// singleton = new CallInfo(name,number,date,type,duration);

// }

singleton=newCallInfo(name,number,date,type,duration);

returnsingleton;

}

publicCallInfo(String name,String number, longdate, inttype,String duration) {

this.name= name;

this.number= number;

this.date= date;

this.type= type;

this.duration= duration;

}

publicStringgetName() {

returnname;

}

public voidsetName(String name) {

this.name= name;

}

publicStringgetNumber() {

returnnumber;

}

public voidsetNumber(String number) {

this.number= number;

}

public longgetDate() {

returndate;

}

public voidsetDate(longdate) {

this.date= date;

}

public intgetType() {

returntype;

}

public voidsetType(inttype) {

this.type= type;

}

publicStringgetDuration() {

returnduration;

}

public voidsetDuration(String duration) {

this.duration= duration;

}

}

public classCallInfoService {

/**

*获取通话记录

*

*@paramcontext上下文。通话记录需要从系统的【通话应用】中的内容提供者中获取,内容提供者需要上下文。通话记录保存在联系人数据库中:data/data/com.android.provider.contacts/databases/contacts2.db库中的calls表。

*@return包含所有通话记录的一个集合

*/

public staticListgetCallInfos(finalContext context) {

finalList infos =newArrayList<>();

finalList newList =newArrayList<>();

RxPermissions.getInstance(context).request(Manifest.permission.READ_CALL_LOG)

.subscribe(newAction1() {

@Override

public voidcall(Boolean aBoolean) {

if(aBoolean) {

ContentResolver resolver =context.getContentResolver();

// uri的写法需要查看源码JB\packages\providers\ContactsProvider\AndroidManifest.xml中内容提供者的授权

//从清单文件可知该提供者是CallLogProvider,且通话记录相关操作被封装到了Calls类中

Uri uri = CallLog.Calls.CONTENT_URI;

String[] projection =newString[]{

CallLog.Calls.CACHED_NAME,//姓名

CallLog.Calls.NUMBER,//号码

CallLog.Calls.DATE,//日期

CallLog.Calls.TYPE,//类型:来电、去电、未接

CallLog.Calls.DURATION

};

if(ActivityCompat.checkSelfPermission(context,Manifest.permission.READ_CALL_LOG) != PackageManager.PERMISSION_GRANTED) {

//TODO: Consider calling

// ActivityCompat#requestPermissions

// here to request the missing permissions, and then overriding

// public void onRequestPermissionsResult(int requestCode, String[] permissions,

// int[] grantResults)

// to handle the case where the user grants the permission. See the documentation

// for ActivityCompat#requestPermissions for more details.

return;

}

try{

Cursor cursor = resolver.query(uri,projection, null, null, null);

while(cursor.moveToNext()) {

if(infos.size()>2000){

break;

}

// instance = CallInfo.getInstance();

// instance.setName(cursor.getString(0));

// instance.setNumber(cursor.getString(1));

// instance.setDate(cursor.getLong(2));

// instance.setType(cursor.getInt(3));

// instance.setDuration(cursor.getString(4));

// infos.add(instance);

String name = cursor.getString(0);

String number = cursor.getString(1);

longdate = cursor.getLong(2);

inttype = cursor.getInt(3);

String duration=cursor.getString(4);

infos.add(CallInfo.getInstance(name,number,date,type,duration));

// infos.add(new CallInfo(name, number, date, type,duration));

}

cursor.close();

}catch(Exception e) {

e.printStackTrace();

}

for(inti =0;i

for(intj = i +1;j

if(infos.get(i).getNumber().equals(infos.get(j).getNumber())) {

newList.add(infos.get(i));

break;

}

}

}

}else{

Toast.makeText(context,"请开启通话记录权限",Toast.LENGTH_SHORT).show();

}

}

});

returnnewList;

}

}


转载于:https://juejin.im/post/5a1e31216fb9a0451463cbb6

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值