Android通讯录、通话记录、短信、应用等模糊查询方法

最近要开发一个功能,能够查询手机内部的通讯录、通话记录、短信、应用、资源管理等内容,所以将一下方法作下记录。其中通讯录、通话记录和短信都是支持输入号码或者名字或者内容就能模糊匹配的。

1、通讯录模糊查询

public List> searchContacts(Activity context, String keyword) {
        ContentResolver cr = context.getContentResolver();
        List> contactList = new ArrayList<>();

        if (isPhoneNum(keyword)) {
            Cursor cursorP = cr.query(
                    ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                    null,
                    ContactsContract.CommonDataKinds.Phone.NUMBER + " like " + "'%" + keyword + "%'",
                    null,
                    null);
            while (cursorP.moveToNext()) {
                Map map = new HashMap<>();

                String phoneNumber = cursorP.getString(cursorP.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                String photo = cursorP.getString(cursorP.getColumnIndex(ContactsContract.CommonDataKinds.Photo.PHOTO_URI));

                String contactId = cursorP.getString(cursorP.getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID));
                Cursor nameC = cr.query(ContactsContract.Contacts.CONTENT_URI, null, ContactsContract.Contacts._ID + "=" + contactId, null, null);
                while (nameC.moveToNext()) {

                    String name = nameC.getString(nameC.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                    Log.i(TAG, "name=" + name + "phoneNumber=" + phoneNumber + ",photo=" + photo);

                    map.put("name", name);
                    map.put("phoneNum", phoneNumber);
                    map.put("photo", photo);
                    contactList.add(map);
                }

            }
            cursorP.close();
        } else {
            Cursor cursorName = cr.query(
                    ContactsContract.Contacts.CONTENT_URI,
                    null,
                    ContactsContract.PhoneLookup.DISPLAY_NAME + " like " + "'%" + keyword + "%'",
                    null,
                    null);
            while (cursorName.moveToNext()) {

                String name = cursorName.getString(
                        cursorName.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                String contactId = cursorName.getString(cursorName.getColumnIndex(ContactsContract.Contacts._ID));
                Cursor phone = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=" + contactId, null, null);
                while (phone.moveToNext()) {

                    Map map = new HashMap<>();

                    String phoneNumber = phone.getString(phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                    String photo = phone.getString(phone.getColumnIndex(ContactsContract.CommonDataKinds.Photo.PHOTO_URI));
                    Log.i(TAG, "name=" + name + "phoneNumber=" + phoneNumber + ",photo=" + photo);

                    map.put("name", name);
                    map.put("phoneNum", phoneNumber);
                    map.put("photo", photo);
                    contactList.add(map);
                }

            }
            cursorName.close();
        }


        return contactList;
    }

其中还有个方法判断需要查询的关键字是号码还是名字

    private boolean isPhoneNum(String keyword) {
        //正则 匹配以数字或者加号开头的字符串(包括了带空格及-分割的号码
        if (keyword.matches("^([0-9]|[/+]).*")) {
            return true;
        } else {
            return false;
        }
    }

2、通话记录

public List<Map<String, String>> getCalllog(Activity context, String keyword) {
        ContentResolver cr = context.getContentResolver();
        Cursor cursor;
        if (isPhoneNum(keyword)) {
            cursor = cr.query(
                    CallLog.Calls.CONTENT_URI,
                    null,
                    "number like " + "'%" + keyword + "%'",
                    null,
                    null);
        } else {
            cursor = cr.query(
                    CallLog.Calls.CONTENT_URI,
                    null,
                    "name like " + "'%" + keyword + "%'",
                    null,
                    null);
        }


        List<Map<String, String>> callList = new ArrayList<>();
        while (cursor.moveToNext()) {
            Map<String, String> map = new HashMap<>();

            //备注名称
            String cachedName = cursor.getString(cursor.getColumnIndex(CallLog.Calls.CACHED_NAME));
            /**
             * 通话类型
             * 1为拨入,2为拨出,3未接
             */

            int type = cursor.getInt(cursor.getColumnIndex(CallLog.Calls.TYPE));

            //通话日期
            String date = cursor.getString(cursor.getColumnIndexOrThrow(CallLog.Calls.DATE));
            //通话时间
            String duration = cursor.getString(cursor.getColumnIndexOrThrow(CallLog.Calls.DURATION));
            //通话号码
            String number = cursor.getString(cursor.getColumnIndex(CallLog.Calls.NUMBER));

            String cachedPhoto = cursor.getString(cursor.getColumnIndex(CallLog.Calls.CACHED_PHOTO_URI));
            Log.i(TAG, "cachedName=" + cachedName + ",type=" + type + ",date=" + date + ",duration=" + duration + ",number=" + number + ",cachedPhoto=" + cachedPhoto);

            map.put("cachedName", cachedName);
            map.put("type", type + "");
            map.put("date", transFormDate(Long.valueOf(date)));
            map.put("duration", secToTime(Integer.valueOf(duration)));
            map.put("number", number);
            map.put("cachedPhoto", cachedPhoto);
            callList.add(map);
        }
        cursor.close();

        return callList;
    }

3、短信查询

/**
     * @param context
     * @param keyword
     * @return sms主要结构:
     * _id:短信序号,如100
     * thread_id:对话的序号,如100,与同一个手机号互发的短信,其序号是相同的
     * address:发件人地址,即手机号,如+8618661062659
     * person:发件人,如果发件人在通讯录中则为具体姓名,陌生人为null
     * date:日期,long型,如1256539465022,可以对日期显示格式进行设置
     * protocol:协议0SMS_RPOTO短信,1MMS_PROTO彩信
     * read:是否阅读0未读,1已读
     * status:短信状态-1接收,0complete,64pending,128failed
     * type:短信类型1是接收到的,2是已发出
     * body:短信具体内容
     * service_center:短信服务中心号码编号,如+8618661062659
     */
    public List<Map<String, String>> getMms(Activity context, String keyword) {
        ContentResolver cr = context.getContentResolver();
        Cursor cursor;
        if (isPhoneNum(keyword)) {
            cursor = cr.query(
                    Telephony.Sms.CONTENT_URI,
                    null,
                    "address like " + "'%" + keyword + "%'",
                    null,
                    null);
        } else {
            cursor = cr.query(
                    Telephony.Sms.CONTENT_URI,
                    null,
                    "body like " + "'%" + keyword + "%'",
                    null,
                    null);
        }


        List<Map<String, String>> mmsList = new ArrayList<>();
        while (cursor.moveToNext()) {
            Map<String, String> map = new HashMap<>();

            String person = cursor.getString(cursor.getColumnIndex("person"));
            String address = cursor.getString(cursor.getColumnIndex("address"));
            //通话日期
            String date = cursor.getString(cursor.getColumnIndexOrThrow("date"));
            String body = cursor.getString(cursor.getColumnIndexOrThrow("body"));

            List<Map<String, String>> list = searchContacts(context, address);
            String photo = "";
            if (list.size() > 0) {
                photo = list.get(0).get("photo");
            }

            Log.i(TAG, "person=" + person + ",address=" + address + ",date=" + date + ",body=" + body + ",photo=" + photo);


            map.put("person", person);
            map.put("address", address);
            map.put("date", transFormDate(Long.valueOf(date)));
            map.put("body", body);
            map.put("photo", photo);
            mmsList.add(map);
        }
        cursor.close();

        return mmsList;

    }

4、应用app查询

public List getApps(Activity context, String keyword) {
        PackageManager pm = context.getPackageManager();
        List applicationInfos = context.getPackageManager().getInstalledApplications(0);

        List appList = new ArrayList<>();

        for (ApplicationInfo applicationInfo : applicationInfos) {
            String appName = pm.getApplicationLabel(applicationInfo).toString();
            if (appName.toLowerCase().contains(keyword.toLowerCase())) {
                int appIcon = applicationInfo.icon;
                Log.i(TAG, "appName=" + appName + ",appIcon=" + appIcon);
                appList.add(applicationInfo);
            }
        }
        return appList;
    }

5、资源管理器文件查询

 /**
     * 通过路径递归查找文件
     *
     * @param path
     */
    public ArrayList searchByPath(String path, String query) {
        File[] files = new File(path).listFiles();
        ArrayList data = new ArrayList<>();
        for (int i = 0; i < files.length; i++) {
            File f = files[i];
            if (f.isDirectory()) {
                searchByPath(path + "/" + f.getName(), query);
            } else {
                if (f.getName().contains(query)) {
                    Log.i(TAG, "files[i].getName()=" + files[i].getName() + ",path=" + files[i].getPath());
                    //searchfilemap.put(files[i], files[i].getName());
                    data.add(f);
                }
            }
        }
        return data;
    }

你可能感兴趣的:(【Android常用知识点】)