package cn.myjdb;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import android.content.ContentResolver;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.database.Cursor;
import android.net.Uri;
import android.os.Environment;
import android.util.Log;
public class GetContractPerson {
private SharedPreferences sp;
private Context thisContext;
private Long readInteval;
private String fileName;
public GetContractPerson(Context thisContext) {
super();
sp=thisContext.getSharedPreferences("contract", Context.MODE_PRIVATE);
this.thisContext = thisContext;
fileName="contract";
readInteval=(long) 10000;
}
public void getContract() {
try {
if(isNeedGetcontract()){
getContractlist();
}
readFile();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// 判断是否需要获取联系人
private boolean isNeedGetcontract() {
String filePath = getFilepath();
// 读取文件中的时间 默认是0
Long lastTime = readLastTime();
Long currTime = System.currentTimeMillis();
if ((currTime - lastTime)>readInteval) {
File file = new File(filePath);
file.delete();
if (!file.exists()) {
Log.i("wang", "删除成功");
}
return true;
}else {
return false;
}
}
// 获取联系人列表;
private void getContractlist() throws Exception {
// 获取文件路径;
String filePath = getFilepath();
// 保存读取时间联系人的时间;
saveCurrentTime(filePath);
Uri uri = Uri.parse("content://com.android.contacts/contacts");
ContentResolver resolver = thisContext.getContentResolver();
Cursor cursor = resolver.query(uri, new String[] { "_id" }, null, null,
null);
StringBuilder sb=new StringBuilder();
while (cursor.moveToNext()) {
int contractID = cursor.getInt(0);
sb.append("contractID=");
sb.append(contractID);
uri = Uri.parse("content://com.android.contacts/contacts/"
+ contractID + "/data");
Cursor cursor1 = resolver.query(uri, new String[] { "mimetype",
"data1", "data2" }, null, null, null);
while (cursor1.moveToNext()) {
String data1 = cursor1.getString(cursor1
.getColumnIndex("data1"));
String mimeType = cursor1.getString(cursor1
.getColumnIndex("mimetype"));
if ("vnd.android.cursor.item/name".equals(mimeType)) { // 是姓名
sb.append(",name=" + data1);
} else if ("vnd.android.cursor.item/email_v2".equals(mimeType)) { // 邮箱
sb.append(",email=" + data1);
} else if ("vnd.android.cursor.item/phone_v2".equals(mimeType)) { // 手机
sb.append(",phone=" + data1);
}
}
sb.append("\r\n");
cursor1.close();
}
Log.i("wang", sb.toString());
saveFile(sb.toString(), filePath);
cursor.close();
}
// 获取路径;
private String getFilepath() {
String filePath = null;
boolean hasSDCard = Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED);
if (hasSDCard) { // SD卡根目录的contract.text
filePath = Environment.getExternalStorageDirectory().toString()
+ File.separator + fileName+".txt";
} else
// 系统下载缓存根目录的contract.txt
filePath = Environment.getDownloadCacheDirectory().toString()
+ File.separator + fileName+".txt";
return filePath;
}
// 联系人保存到contract.txt 文件
private void saveFile(String str, String filePath) {
try {
File file = new File(filePath);
if (!file.exists()) {
File dir = new File(file.getParent());
dir.mkdirs();
file.createNewFile();
}
FileOutputStream outStream = new FileOutputStream(file);
outStream.write(str.getBytes());
outStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
// 把当前时间保存进去
private void saveCurrentTime(String filePath) {
Editor editor=sp.edit();
Log.i("wang", "存入的时间"+System.currentTimeMillis());
editor.putLong("currTime", System.currentTimeMillis());
editor.commit();
}
// 读取上次读取联系人的时间
private Long readLastTime() {
SharedPreferences sp=thisContext.getSharedPreferences("contract", Context.MODE_PRIVATE);
Log.i("wang", "lastTime"+sp.getLong("currTime", 0));
return sp.getLong("currTime", 0);
}
private void readFile(){
String filePath=getFilepath();
File file=new File(filePath);
try {
InputStreamReader isr=new InputStreamReader(new FileInputStream(file));
BufferedReader br=new BufferedReader(isr);
StringBuilder sb=new StringBuilder();
String str="";
while ((str=br.readLine())!=null) {
sb.append(str);
}
Log.i("wang", "文件的所有内容"+sb.toString());
br.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/*getter和setter*/
public long getReadInteval() {
return readInteval;
}
public void setReadInteval(long readInteval) {
this.readInteval = readInteval;
}
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
}
最后getter和setter函数可以设置文件名字和多久时间建个获取联系人信息,
通过调用这样两句进行获取联系人信息
getContract=new GetContractPerson(thisContext);
getContract.getContract();