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

实现方法很简单
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手机通讯录备份还原代码/

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。