如何访问短信和联系人数据?

发布于 2024-12-01 19:54:20 字数 126 浏览 1 评论 0原文

内容提供商是读取/写入短信和联系人等私人数据的唯一方法吗?我首先尝试简单而懒惰的方法(复制短信和联系人 SQLite 数据库文件),但我遇到了一些权限问题。我这么问是因为我正在尝试备份和恢复短信和联系人,而逐一访问数据字段将需要大量工作。

is content providers the only way to read/write private data such SMS and contacts? I first try the easy and lazy way (copy sms and contacts SQLite databases files) but I faced some permission issues. I'm asking because I'm trying to backup and restore SMS and contacts and that would be a lot of work accessing data field one by one.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

与酒说心事 2024-12-08 19:54:20

获取联系人:

如何调用 Android 联系人列表?

如何从android中的本机电话簿获取联系人

如何在 Android 中获取联系人的所有详细信息

如何从 Android 获取名字和姓氏联系人?

如何将联系人从电话簿导入到我们的应用程序

Android 联系人提取

如何获取所有Android联系人但没有SIM 卡上的内容

和短信:

Uri mSmsinboxQueryUri = Uri.parse("content://sms/inbox");
        Cursor cursor1 = getContentResolver().query(mSmsinboxQueryUri,
                    new String[] { "_id", "thread_id", "address", "person", "date",
                                    "body", "type" }, null, null, null);
        startManagingCursor(cursor1);
        String[] columns = new String[] { "address", "person", "date", "body","type" };
        if (cursor1.getCount() > 0) {
            String count = Integer.toString(cursor1.getCount());
            Log.e("Count",count);
            while (cursor1.moveToNext()){
                String address = cursor1.getString(cursor1.getColumnIndex(columns[0]));
                String name = cursor1.getString(cursor1.getColumnIndex(columns[1]));
                String date = cursor1.getString(cursor1.getColumnIndex(columns[2]));
                String msg = cursor1.getString(cursor1.getColumnIndex(columns[3]));
                String type = cursor1.getString(cursor1.getColumnIndex(columns[4]));
            }
        }

Getting Contacts:

How to call Android contacts list?

How to get contacts from native phonebook in android

How to obtain all details of a contact in Android

How to get the first name and last name from Android contacts?

How to import contacts from phonebook to our application

Android contacts extraction

How to get all android contacts but without those which are on SIM

and for sms:

Uri mSmsinboxQueryUri = Uri.parse("content://sms/inbox");
        Cursor cursor1 = getContentResolver().query(mSmsinboxQueryUri,
                    new String[] { "_id", "thread_id", "address", "person", "date",
                                    "body", "type" }, null, null, null);
        startManagingCursor(cursor1);
        String[] columns = new String[] { "address", "person", "date", "body","type" };
        if (cursor1.getCount() > 0) {
            String count = Integer.toString(cursor1.getCount());
            Log.e("Count",count);
            while (cursor1.moveToNext()){
                String address = cursor1.getString(cursor1.getColumnIndex(columns[0]));
                String name = cursor1.getString(cursor1.getColumnIndex(columns[1]));
                String date = cursor1.getString(cursor1.getColumnIndex(columns[2]));
                String msg = cursor1.getString(cursor1.getColumnIndex(columns[3]));
                String type = cursor1.getString(cursor1.getColumnIndex(columns[4]));
            }
        }
谁的新欢旧爱 2024-12-08 19:54:20

获取联系方式

Cursor cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,null, null, null, null);
ArrayList<HashMap<String,String>> contactData=new ArrayList<HashMap<String,String>>();
         while (cursor.moveToNext()) {
             try{
             String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID)); 
             String name=cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
             String hasPhone = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)); 
             if (Integer.parseInt(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
                 Cursor phones = getContentResolver().query( ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId, null, null);
                 while (phones.moveToNext()) { 
                     String phoneNumber = phones.getString(phones.getColumnIndex( ContactsContract.CommonDataKinds.Phone.NUMBER));
                     HashMap<String,String> map=new HashMap<String,String>();
                     map.put("name", name);
                     map.put("number", phoneNumber);
                     contactData.add(map);
                 } 
                 phones.close(); 
             }
         }catch(Exception e){}
         }

阅读收件箱中的短信

Uri mSmsinboxQueryUri = Uri.parse("content://sms");
Cursor cursor1 = getContentResolver().query(
        mSmsinboxQueryUri,
        new String[] { "_id", "thread_id", "address", "person", "date",
                "body", "type" }, null, null, null);
startManagingCursor(cursor1);
String[] columns = new String[] { "address", "person", "date", "body",
        "type" };
if (cursor1.getCount() > 0) {
    String count = Integer.toString(cursor1.getCount());
    Log.e("Count",count);
    while (cursor1.moveToNext()) {
        out.write("<message>");
        String address = cursor1.getString(cursor1
                .getColumnIndex(columns[0]));
        String name = cursor1.getString(cursor1
                .getColumnIndex(columns[1]));
        String date = cursor1.getString(cursor1
                .getColumnIndex(columns[2]));
        String msg = cursor1.getString(cursor1
                .getColumnIndex(columns[3]));
        String type = cursor1.getString(cursor1
                .getColumnIndex(columns[4]));

}
}

To get the contact

Cursor cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,null, null, null, null);
ArrayList<HashMap<String,String>> contactData=new ArrayList<HashMap<String,String>>();
         while (cursor.moveToNext()) {
             try{
             String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID)); 
             String name=cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
             String hasPhone = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)); 
             if (Integer.parseInt(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
                 Cursor phones = getContentResolver().query( ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId, null, null);
                 while (phones.moveToNext()) { 
                     String phoneNumber = phones.getString(phones.getColumnIndex( ContactsContract.CommonDataKinds.Phone.NUMBER));
                     HashMap<String,String> map=new HashMap<String,String>();
                     map.put("name", name);
                     map.put("number", phoneNumber);
                     contactData.add(map);
                 } 
                 phones.close(); 
             }
         }catch(Exception e){}
         }

To read the sms from the inbox

Uri mSmsinboxQueryUri = Uri.parse("content://sms");
Cursor cursor1 = getContentResolver().query(
        mSmsinboxQueryUri,
        new String[] { "_id", "thread_id", "address", "person", "date",
                "body", "type" }, null, null, null);
startManagingCursor(cursor1);
String[] columns = new String[] { "address", "person", "date", "body",
        "type" };
if (cursor1.getCount() > 0) {
    String count = Integer.toString(cursor1.getCount());
    Log.e("Count",count);
    while (cursor1.moveToNext()) {
        out.write("<message>");
        String address = cursor1.getString(cursor1
                .getColumnIndex(columns[0]));
        String name = cursor1.getString(cursor1
                .getColumnIndex(columns[1]));
        String date = cursor1.getString(cursor1
                .getColumnIndex(columns[2]));
        String msg = cursor1.getString(cursor1
                .getColumnIndex(columns[3]));
        String type = cursor1.getString(cursor1
                .getColumnIndex(columns[4]));

}
}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文