歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux編程 >> Linux編程

Android手機通訊錄備份還原代碼

最近想寫段Android程序玩玩。開發環境 Eclipse ,Android 2.2

開發環境搭建

1.先安裝jdk

2.下載安裝eclipse

3.下載安裝android sdk

4.安裝eclipse插件  adt

5.配置 Window > Preferences 中的android sdk路徑

6.創建 AVD 

實現方法很簡單
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手機通訊錄備份還原代碼下載地址

免費下載地址在 http://linux.linuxidc.com/

用戶名與密碼都是www.linuxidc.com

具體下載目錄在 /pub/Android源碼集錦/2011年/11月/Android手機通訊錄備份還原代碼/

Copyright © Linux教程網 All Rights Reserved