码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2016-03-23 13:14:41      阅读:182      评论:0      收藏:0      [点我收藏+]

标签:

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

 1 package com.ithaimazyh.readcontact;
 2 
 3 import java.util.ArrayList;
 4 import java.util.List;
 5 
 6 import com.ithaimazyh.readcontact.domain.PersonInfo;
 7 import com.ithaimazyh.readcontact.service.SavePersonsInfo;
 8 
 9 import android.net.Uri;
10 import android.os.Bundle;
11 import android.app.Activity;
12 import android.content.ContentResolver;
13 import android.database.Cursor;
14 import android.view.Menu;
15 import android.view.View;
16 import android.view.View.OnClickListener;
17 import android.widget.Button;
18 
19 public class MainActivity extends Activity {
20 
21     @Override
22     protected void onCreate(Bundle savedInstanceState) {
23         super.onCreate(savedInstanceState);
24         setContentView(R.layout.activity_main);
25         
26         Button rp = (Button) findViewById(R.id.rp);
27         rp.setOnClickListener(new OnClickListener() {
28             
29             @Override
30             public void onClick(View v) {
31                 // TODO Auto-generated method stub
32                 //System.out.println("hello world");
33                 readContact();
34             }
35         });
36     }
37 
38     public void readContact(){
39         Uri uri = Uri.parse("content://com.android.contacts/raw_contacts");
40         Uri data_uri = Uri.parse("content://com.android.contacts/data");
41         
42         ContentResolver resolver = getContentResolver();
43         Cursor cursor = resolver.query(uri, null, null, null, null);
44         
45         /*String[] names = cursor.getColumnNames();
46         for(String name : names){
47             System.out.println(name);
48         }*/
49         List<PersonInfo> persons = new ArrayList<PersonInfo>();
50         while(cursor.moveToNext()){
51             String contact_id = cursor.getString(cursor.getColumnIndex("contact_id"));
52             if(contact_id != null){
53                 String name = "";
54                 String phoneNum = "";
55                 Cursor datacursor = resolver.query(data_uri, null, "raw_contact_id=?", new String[]{contact_id}, null);
56                 while(datacursor.moveToNext()){
57                     String data1 = datacursor.getString(datacursor.getColumnIndex("data1"));
58                     String mimetype = datacursor.getString(datacursor.getColumnIndex("mimetype"));
59                     
60                     //System.out.println(data1 + "-----" + mimetype);
61                     if(mimetype.equals("vnd.android.cursor.item/name")){
62                         name = data1;
63                     }else if(mimetype.equals("vnd.android.cursor.item/phone_v2")){
64                         phoneNum = data1;
65                     }else{
66                         
67                     }
68                 }
69                 
70                 if(name != null && phoneNum != null){
71                     PersonInfo person = new PersonInfo(name,phoneNum);
72                     persons.add(person);
73                 }
74                 datacursor.close();
75                 //System.out.print("=========================");
76             }
77         }
78         cursor.close();
79         
80         SavePersonsInfo.savePersonsInfo(this, persons);
81     /*    Cursor datacursor = resolver.query(data_uri, null, null, null, null);
82         String[] names = datacursor.getColumnNames();
83         for(String name : names){
84             System.out.println(name);
85         }*/
86     }
87 }

2、权限配置

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

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

 1 package com.ithaimazyh.readcontact.service;
 2 
 3 import java.io.File;
 4 import java.io.FileNotFoundException;
 5 import java.io.FileOutputStream;
 6 import java.util.List;
 7 
 8 import org.xmlpull.v1.XmlSerializer;
 9 
10 import com.ithaimazyh.readcontact.domain.PersonInfo;
11 
12 import android.content.Context;
13 import android.os.Environment;
14 import android.util.Xml;
15 import android.widget.Toast;
16 
17 public class SavePersonsInfo {
18     public static boolean savePersonsInfo(Context context, List<PersonInfo> persons){
19         if(persons.size() > 0){
20             try {
21                 File file = new File(Environment.getExternalStorageDirectory(),"persons.xml");
22                 FileOutputStream fos = new FileOutputStream(file);
23                 XmlSerializer serializer = Xml.newSerializer();
24                 serializer.setOutput(fos, "utf-8");
25                 serializer.startDocument("utf-8", true);
26                 serializer.startTag(null, "persons");
27                 
28                 for(PersonInfo person : persons){
29                     serializer.startTag(null, "person");
30                     
31                     serializer.startTag(null, "name");
32                     serializer.text(person.getName());
33                     serializer.endTag(null, "name");
34                     
35                     serializer.startTag(null, "phoneNum");
36                     serializer.text(person.getPhoneNum());
37                     serializer.endTag(null, "phoneNum");
38                     
39                     serializer.endTag(null, "person");
40                 }
41                 
42                 serializer.endTag(null, "persons");
43                 serializer.endDocument();
44                 
45                 fos.close();
46                 Toast.makeText(context, "保存xml成功", 0).show();
47             } catch (Exception e) {
48                 // TODO Auto-generated catch block
49                 e.printStackTrace();
50                 Toast.makeText(context, "保存xml失败", 0).show();
51             }
52         }
53         return true;
54     }
55 }

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

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

5、person类

 1 package com.ithaimazyh.readcontact.domain;
 2 
 3 public class PersonInfo {
 4     private String name;
 5     private String phoneNum;
 6     
 7     public PersonInfo() {
 8         
 9     }
10     
11     public PersonInfo(String name, String phoneNum) {
12         this.name = name;
13         this.phoneNum = phoneNum;
14     }
15     public String getName() {
16         return name;
17     }
18     public void setName(String name) {
19         this.name = name;
20     }
21     public String getPhoneNum() {
22         return phoneNum;
23     }
24     public void setPhoneNum(String phoneNum) {
25         this.phoneNum = phoneNum;
26     }
27 }

 

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

String[] columnNames = datacursor.getColumnNames();

 

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

标签:

原文地址:http://www.cnblogs.com/zhongyinghe/p/5310583.html

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