开发者

How to access SMS and contacts data?

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 res开发者_如何学JAVAtore SMS and contacts and that would be a lot of work accessing data field one by one.


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]));
            }
        }


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]));

} }

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜