欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android 通话记录,短信记录粘出来可以直接用

程序员文章站 2022-06-22 08:46:12
public class CallLogUtils { private Uri callUri = CallLog.Calls.CONTENT_URI; private String[] columns = {CallLog.Calls.CACHED_NAME// 通话记录的联系人 , CallLog.Calls.NUMBER// 通话记录的电话号码 , CallLog.Calls.DATE// 通话记录的日期 , ....
public class CallLogUtils {

    private Uri callUri = CallLog.Calls.CONTENT_URI;
    private String[] columns = {CallLog.Calls.CACHED_NAME// 通话记录的联系人
            , CallLog.Calls.NUMBER// 通话记录的电话号码
            , CallLog.Calls.DATE// 通话记录的日期
            , CallLog.Calls.DURATION// 通话时长
            , CallLog.Calls.TYPE};// 通话类型}
    CallLogBean callLogBean = new CallLogBean();
    List<CallLogBean> logs = new ArrayList<>();
    private static final String TAG = "call_log";

    public List<CallLogBean> getContentCallLog(Context context) {
//        between date('now', "-1 month") and date('now')
        String where = " date between date(\"now\", \"-1 month\") and date(\"now\")";
        Cursor cursor = context.getContentResolver().query(callUri, // 查询通话记录的URI
                columns
                , null, null, CallLog.Calls.DEFAULT_SORT_ORDER// 按照时间逆序排列,最近打的最先显示
        );

        Log.i(TAG, "cursor count:" + cursor.getCount());
        while (cursor.moveToNext()) {
            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());
            String time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").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.未接

            long l = dateDiff(date, time, "yyyy-MM-dd HH:mm:ss");
            if (l < 30) {
                if (name != null) {
                    callLogBean = new CallLogBean();
                    callLogBean.setName(name);
                    callLogBean.setPhone(number);
                    callLogBean.setCall_time(date);
                    callLogBean.setDuration_time(duration);
                    callLogBean.setIn_out(type);
                    logs.add(callLogBean);
                } else {
                    callLogBean = new CallLogBean();
                    callLogBean.setName(number);
                    callLogBean.setPhone(number);
                    callLogBean.setCall_time(date);
                    callLogBean.setDuration_time(duration);
                    callLogBean.setIn_out(type);
                    logs.add(callLogBean);
                }
                Log.i(TAG, "Call log: " + "\n"
                        + "name: " + name + "\n"
                        + "phone number: " + number + "\n" +
                        "dateLong :" + dateLong + "\n"

                );
            }

        }
        cursor.close();
        return logs;
    }

    //**************授权信息
    private void getPersimmionInfo(Activity context) {
        if (Build.VERSION.SDK_INT >= 23) {
            //1. 检测是否添加权限   PERMISSION_GRANTED  表示已经授权并可以使用
            if (ContextCompat.checkSelfPermission(context, Manifest.permission.READ_CALL_LOG) != PackageManager.PERMISSION_GRANTED) {
                //手机为Android6.0的版本,权限未授权去i请授权
                //2. 申请请求授权权限
                //1. Activity
                // 2. 申请的权限名称
                // 3. 申请权限的 请求码
                ActivityCompat.requestPermissions(context, new String[]
                        {Manifest.permission.READ_CALL_LOG, Manifest.permission.WRITE_APN_SETTINGS,
                                Manifest.permission.READ_EXTERNAL_STORAGE//通话记录
                        }, 1005);
            }

        }

    }

    public long dateDiff(String endTime, String startTime, String format) {
        // 按照传入的格式生成一个simpledateformate对象
        SimpleDateFormat sd = new SimpleDateFormat(format);
        long nd = 1000 * 24 * 60 * 60;// 一天的毫秒数
        long nh = 1000 * 60 * 60;// 一小时的毫秒数
        long nm = 1000 * 60;// 一分钟的毫秒数
        long ns = 1000;// 一秒钟的毫秒数
        long diff;
        long day = 0;
        try {
            // 获得两个时间的毫秒时间差异
            diff = sd.parse(endTime).getTime()
                    - sd.parse(startTime).getTime();
            day = diff / nd;// 计算差多少天
            long hour = diff % nd / nh;// 计算差多少小时
            long min = diff % nd % nh / nm;// 计算差多少分钟
            long sec = diff % nd % nh % nm / ns;// 计算差多少秒
            // 输出结果
//            System.out.println("时间相差:" + day + "天" + hour + "小时" + min
//                    + "分钟" + sec + "秒。");
            if (day >= 1) {
                return day;
            } else {
                if (day == 0) {
                    return 1;
                } else {
                    return 0;
                }

            }

        } catch (ParseException e) {
            e.printStackTrace();
        }
        return 0;
    }

    private Uri SMS_INBOX = Uri.parse("content://sms/");

    public List<SmsBean> getSmsFromPhone(Context context) {
        ContentResolver cr = context.getContentResolver();
        List<SmsBean> smsBeanList = new ArrayList<>();
        //between date('now', "-1 month") and date('now')
        String[] projection = new String[]{"_id","body", "address", "person", "date", "type"};//"_id", "address", "person",, "date", "type
//        String where = " date >  "
//                + (System.currentTimeMillis() - 10 * 60 * 1000);
        Cursor cur = cr.query(SMS_INBOX, projection, null, null, "date desc");

        if (null == cur) {
            Log.i("ooc", "************cur == null");
            return smsBeanList;
        }

        while (cur.moveToNext()) {
            String number = cur.getString(cur.getColumnIndex("address"));//手机号
            String name = cur.getString(cur.getColumnIndex("person"));//联系人姓名列表
            String body = cur.getString(cur.getColumnIndex("body"));//内容
            long time = cur.getLong(cur.getColumnIndex("date"));//时间
            String type = cur.getString(cur.getColumnIndex("type"));//类型

            String date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());//当前时间
            String dateLong = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(time));

            long l = dateDiff(date, dateLong, "yyyy-MM-dd HH:mm:ss");
            if (l < 30) {
                Log.i(TAG, "Call log: " + "\n"
                        + "name: " + name + "\n"
                        + "phone number: " + number + "\n" +
                        "body" + body + "\n"

                );
//
                if (name != null) {
                    SmsBean smsBean = new SmsBean();
                    smsBean.setSms_phone(number);
                    smsBean.setSms_name(name);
                    smsBean.setSms_content(body);
                    smsBean.setSms_time(dateLong);
                    smsBean.setSms_status(type);
                    smsBeanList.add(smsBean);
                } else {
                    SmsBean smsBean = new SmsBean();
                    smsBean.setSms_phone(number);
                    smsBean.setSms_name(number);
                    smsBean.setSms_content(body);
                    smsBean.setSms_time(dateLong);
                    smsBean.setSms_status(type);
                    smsBeanList.add(smsBean);
                }
            }
        }
        cur.close();
        return smsBeanList;
    }

    public static String[] getNewPhoneContacts(Activity mActivity, Uri uri) {
        String[] contact = new String[2];
        try {
            // 创建内容解析者
            ContentResolver contentResolver = mActivity.getContentResolver();
            Cursor cursor = null;
            String phoneNum = null;
            String contactName = null;
            if (uri != null) {
                cursor = contentResolver.query(uri, new String[]{"display_name", "data1"}, null, null, null);
            } else {

            }
            while (cursor.moveToNext()) {
                contactName = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
                phoneNum = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
            }
            cursor.close();
            if (phoneNum != null && !phoneNum.isEmpty()) {
                if (phoneNum.contains(" ")) {
                    phoneNum = phoneNum.replace(" ", "");
                }
                if (phoneNum.contains("-")) {
                    phoneNum = phoneNum.replace("-", "");
                }
                if (phoneNum.contains("+63")) {
                    phoneNum = phoneNum.replace("+63", "0");
                }
                if (phoneNum.length() < 9) {
                    if (phoneNum.contains("+")) {
                        phoneNum = phoneNum.replace("+", "");
                    }
                }
                String startOne = phoneNum.substring(0, 1);
                if ("9".equals(startOne)) {
                    phoneNum = "0" + phoneNum;
                } else {


                }
                contact[0] = contactName;
                contact[1] = phoneNum;
            }
            return contact;

        } catch (Exception e) {
            e.fillInStackTrace();
        }
        return contact;
    }


}

本文地址:https://blog.csdn.net/weixin_41194750/article/details/107175121