Android端通话记录读取

Android端读取手机通话记录,直接上代码:
public class CallInfo {
public String name;
public String number; // 号码
public long date; // 日期
public int type; // 类型:来电、去电、未接
public String duration;
private static CallInfo singleton=null;
public static synchronized CallInfo getInstance(String name, String number, long date, int type, String duration){
// if(singleton==null){
// singleton = new CallInfo(name,number,date,type,duration);
// }
singleton = new CallInfo(name,number,date,type,duration);
return singleton;
}

public CallInfo(String name, String number, long date, int type, String duration) {
    this.name = name;
    this.number = number;
    this.date = date;
    this.type = type;
    this.duration = duration;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getNumber() {
    return number;
}

public void setNumber(String number) {
    this.number = number;
}

public long getDate() {
    return date;
}

public void setDate(long date) {
    this.date = date;
}

public int getType() {
    return type;
}

public void setType(int type) {
    this.type = type;
}

public String getDuration() {
    return duration;
}

public void setDuration(String duration) {
    this.duration = duration;
}

}

public class CallInfoService {

/**
 * 获取通话记录
 *
 * @param context 上下文。通话记录需要从系统的【通话应用】中的内容提供者中获取,内容提供者需要上下文。通话记录保存在联系人数据库中:data/data/com.android.provider.contacts/databases/contacts2.db库中的calls表。
 * @return 包含所有通话记录的一个集合
 */
public static List<CallInfo> getCallInfos(final Context context) {
    final List<CallInfo> infos = new ArrayList<>();
    final List<CallInfo> newList = new ArrayList<>();
    RxPermissions.getInstance(context).request(Manifest.permission.READ_CALL_LOG)
            .subscribe(new Action1<Boolean>() {

                @Override
                public void call(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 = new String[]{
                                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);
long date = cursor.getLong(2);
int type = 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 (int i = 0; i < infos.size(); i++) {
                            for (int j = i + 1; j < infos.size(); 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();
                    }
                }
            });
    return newList;
}

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值