码迷,mamicode.com
首页 > 移动开发 > 详细

Android手机通讯录备份还原代码

时间:2015-05-19 01:52:47      阅读:196      评论:0      收藏:0      [点我收藏+]

标签:

实现方法很简单
1.把通讯录中的联系人,电话号码保存到txt文件中完成备份。
2.读取txt文件,导入到通讯录完成还原。

  具体代码
1.添加 通讯录读写权限,存储卡写权限

  <uses-permission android:name="android.permission.READ_CONTACTS"></uses-permission>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
<uses-permission android:name="android.permission.WRITE_CONTACTS"></uses-permission>

  2.写文件代码

  File saveFile=new File("/sdcard/test.txt");
                FileOutputStream outStream;
try {
outStream = new FileOutputStream(saveFile);
       outStream.write(str.getBytes());
       outStream.close();
} catch (Exception e) {

  
setTitle(e.toString());
}

  3.取通讯录联系人

  str="";
       Cursor cur = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);  
       if (cur.moveToFirst()) {  
           int idColumn  = cur.getColumnIndex(ContactsContract.Contacts._ID);  
           int displayNameColumn = cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);  
           do {  
              String contactId = cur.getString(idColumn);  
              String disPlayName = cur.getString(displayNameColumn);  
              str+=disPlayName;
              int phoneCount = cur.getInt(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)); 
              if(phoneCount>0){  
                  Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID+ " = " + contactId, null, null);
                  int i=0;
                  String phoneNumber;
                  if(phones.moveToFirst()){  
                      do{  
                       i++;
                          phoneNumber= phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                          if(i==1)
                          str=str+","+phoneNumber;
                          System.out.println(phoneNumber);  
                      }while(phones.moveToNext());  
                  }  
              }  
              str+="\r\n";
              } while (cur.moveToNext());  
       }  
       } 

  4.读文件代码

  try {
              File file = new File("/sdcard/test.txt");
            FileInputStream inStream = new FileInputStream(file);
          ByteArrayOutputStream outStream = new ByteArrayOutputStream();
          byte[] buffer = new byte[1024*5];
          int length = -1;
          while((length = inStream.read(buffer)) != -1 ){
           outStream.write(buffer, 0, length);
          }
          outStream.close();
          inStream.close();
          String txt= outStream.toString();

  
   } catch (IOException e){
             setTitle(e.toString());
         }

  
5.写通讯录

  ContentValues values = new ContentValues();
    Uri rawContactUri = getContentResolver().insert(RawContacts.CONTENT_URI, values);
    long rawContactId = ContentUris.parseId(rawContactUri);
    values.clear();
    values.put(Data.RAW_CONTACT_ID, rawContactId);
    values.put(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE);
    values.put(StructuredName.GIVEN_NAME, name);
    getContentResolver().insert(Data.CONTENT_URI, values);
     values.clear();
    values.put(Data.RAW_CONTACT_ID, rawContactId);
    values.put(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE);
    values.put(Phone.NUMBER, num);
    values.put(Phone.TYPE, Phone.TYPE_HOME);
    getContentResolver().insert(Data.CONTENT_URI, values);

  Android手机通讯录备份还原代码下载地址

  免费下载地址在

  用户名与密码都是

  具体下载目录在 /pub/Android源码集锦/2011年/11月/Android手机通讯录备份还原代码/

Android手机通讯录备份还原代码

标签:

原文地址:http://www.cnblogs.com/lingyi1111/p/4513405.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!