设为首页 加入收藏

TOP

Android手机通讯录备份还原代码(一)
2014-11-24 13:04:48 来源: 作者: 【 】 浏览:1
Tags:Android 手机 通讯录 备份 还原 代码

开发环境搭建


1.先安装jdk


2.下载安装eclipse


3.下载安装android sdk


4.安装eclipse插件 adt


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


6.创建 AVD


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


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





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


And

首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇Linux Shell脚本中如何定义函数及.. 下一篇jQuery实现图片延迟加载

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·Announcing October (2025-12-24 15:18:16)
·MySQL有什么推荐的学 (2025-12-24 15:18:13)
·到底应该用MySQL还是 (2025-12-24 15:18:11)
·进入Linux世界大门的 (2025-12-24 14:51:47)
·Download Linux | Li (2025-12-24 14:51:44)