android 定时获取联系人信息
来源:互联网 发布:淘宝上碧欧泉是正品吗 编辑:程序博客网 时间:2024/05/18 01:46
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();
0 0
- android 定时获取联系人信息
- 获取android联系人信息
- android获取联系人信息
- android获取联系人信息
- 获取android联系人信息
- 获取android联系人信息
- 获取android联系人信息
- android,获取联系人信息
- android获取联系人信息
- 获取android联系人信息
- android获取联系人信息,插入联系人
- Android 之 获取联系人信息
- android 获取手机联系人信息
- Android中获取联系人信息
- 获取android所有联系人信息
- android 获取通讯录联系人信息
- android获取所有联系人信息
- 获取android所有联系人信息
- Linux面试题集锦三
- xshell连接不上ubuntu(仅主机模式下)
- fastjson之toJSONStringWithDateFormat与SerializerFeature.WriteDateUseDateFormat
- 计算机文化基础—计算机硬件系统
- Windows下 VM12虚拟机安装OS X 10.11(详细教程)
- android 定时获取联系人信息
- PHP网页的安全性问题
- 字符串操作
- yii2查询条件Where全解析
- 当spring 容器初始化完成后执行某个方法 防止onApplicationEvent方法被执行两次
- 50个常用的sql语句
- 请求、响应与超时机制详解
- Ubuntu & terminal 实用快捷键(实用,非大全)
- JNI学习积累之二 ---- 数据类型映射、域描述符说明