使用内容提供者和xml备份联系人

1、通过内容提供者获取联系人信息

 package com.ithaimazyh.readcontact;

 import java.util.ArrayList;
import java.util.List; import com.ithaimazyh.readcontact.domain.PersonInfo;
import com.ithaimazyh.readcontact.service.SavePersonsInfo; import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button; public class MainActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); Button rp = (Button) findViewById(R.id.rp);
rp.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
// TODO Auto-generated method stub
//System.out.println("hello world");
readContact();
}
});
} public void readContact(){
Uri uri = Uri.parse("content://com.android.contacts/raw_contacts");
Uri data_uri = Uri.parse("content://com.android.contacts/data"); ContentResolver resolver = getContentResolver();
Cursor cursor = resolver.query(uri, null, null, null, null); /*String[] names = cursor.getColumnNames();
for(String name : names){
System.out.println(name);
}*/
List<PersonInfo> persons = new ArrayList<PersonInfo>();
while(cursor.moveToNext()){
String contact_id = cursor.getString(cursor.getColumnIndex("contact_id"));
if(contact_id != null){
String name = "";
String phoneNum = "";
Cursor datacursor = resolver.query(data_uri, null, "raw_contact_id=?", new String[]{contact_id}, null);
while(datacursor.moveToNext()){
String data1 = datacursor.getString(datacursor.getColumnIndex("data1"));
String mimetype = datacursor.getString(datacursor.getColumnIndex("mimetype")); //System.out.println(data1 + "-----" + mimetype);
if(mimetype.equals("vnd.android.cursor.item/name")){
name = data1;
}else if(mimetype.equals("vnd.android.cursor.item/phone_v2")){
phoneNum = data1;
}else{ }
} if(name != null && phoneNum != null){
PersonInfo person = new PersonInfo(name,phoneNum);
persons.add(person);
}
datacursor.close();
//System.out.print("=========================");
}
}
cursor.close(); SavePersonsInfo.savePersonsInfo(this, persons);
/* Cursor datacursor = resolver.query(data_uri, null, null, null, null);
String[] names = datacursor.getColumnNames();
for(String name : names){
System.out.println(name);
}*/
}
}

2、权限配置

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

3、通过xml保存联系人信息

 package com.ithaimazyh.readcontact.service;

 import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.List; import org.xmlpull.v1.XmlSerializer; import com.ithaimazyh.readcontact.domain.PersonInfo; import android.content.Context;
import android.os.Environment;
import android.util.Xml;
import android.widget.Toast; public class SavePersonsInfo {
public static boolean savePersonsInfo(Context context, List<PersonInfo> persons){
if(persons.size() > 0){
try {
File file = new File(Environment.getExternalStorageDirectory(),"persons.xml");
FileOutputStream fos = new FileOutputStream(file);
XmlSerializer serializer = Xml.newSerializer();
serializer.setOutput(fos, "utf-8");
serializer.startDocument("utf-8", true);
serializer.startTag(null, "persons"); for(PersonInfo person : persons){
serializer.startTag(null, "person"); serializer.startTag(null, "name");
serializer.text(person.getName());
serializer.endTag(null, "name"); serializer.startTag(null, "phoneNum");
serializer.text(person.getPhoneNum());
serializer.endTag(null, "phoneNum"); serializer.endTag(null, "person");
} serializer.endTag(null, "persons");
serializer.endDocument(); fos.close();
Toast.makeText(context, "保存xml成功", 0).show();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
Toast.makeText(context, "保存xml失败", 0).show();
}
}
return true;
}
}

4、把文件保存在外存,权限配置

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

5、person类

 package com.ithaimazyh.readcontact.domain;

 public class PersonInfo {
private String name;
private String phoneNum; public PersonInfo() { } public PersonInfo(String name, String phoneNum) {
this.name = name;
this.phoneNum = phoneNum;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhoneNum() {
return phoneNum;
}
public void setPhoneNum(String phoneNum) {
this.phoneNum = phoneNum;
}
}

注意:如果在查询数据时不知道列名,可以通过如下方法打印来了解

String[] columnNames = datacursor.getColumnNames();
上一篇:在不同版本的 IIS 上使用 ASP.NET MVC


下一篇:ng2响应式表单-翻译与概括官网REACTIVE FORMS页面