android 定时获取联系人信息

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

基于bert实现关系三元组抽取python源码+数据集+项目说明.zip基于bert实现关系三元组抽取python源码+数据集+项目说明.zip基于bert实现关系三元组抽取python源码+数据集+项目说明.zip基于bert实现关系三元组抽取python源码+数据集+项目说明.zip基于bert实现关系三元组抽取python源码+数据集+项目说明.zip 个人大四的毕业设计、课程设计、作业、经导师指导并认可通过的高分设计项目,评审平均分达96.5分。主要针对计算机相关专业的正在做毕设的学生和需要项目实战练习的学习者,也可作为课程设计、期末大作业。 [资源说明] 不懂运行,下载完可以私聊问,可远程教学 该资源内项目源码是个人的毕设或者课设、作业,代码都测试ok,都是运行成功后才上传资源,答辩评审平均分达到96.5分,放心下载使用! 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用! 2、本项目适合计算机相关专业(如计科、人工智能、通信工程、自动化、电子信息等)的在校学生、老师或者企业员工下载学习,也适合小白学习进阶,当然也可作为毕设项目、课程设计、作业、项目初期立项演示等。 3、如果基础还行,也可在此代码基础上进行修改,以实现其他功能,也可用于毕设、课设、作业等。 下载后请首先打开README.md文件(如有),供学习参考。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值