美文网首页
Android端通话记录读取

Android端通话记录读取

作者: imperial_南桥 | 来源:发表于2017-11-29 11:59 被阅读0次

    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;

    }

    }

    相关文章

      网友评论

          本文标题:Android端通话记录读取

          本文链接:https://www.haomeiwen.com/subject/mteubxtx.html