package com.cy.contact.dao;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import com.cy.contact.pojo.NetiveUser;
import android.app.PendingIntent;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.provider.CallLog;
import android.provider.CallLog.Calls;
import android.telephony.SmsManager;
/**
* 本地通讯电话和短息信息记录
* @author wangqiang
*
*/
public class NativeCallSMSImpl {
private Context context;
private NetiveUser netiveUser;
public NativeCallSMSImpl( Context context){
this.context = context;
}
/**
* 获取所有的通话记录
*
* type 类型有
* 来电 CallLog.Calls.INCOMING_TYPE 1
* 已拨CallLog.Calls.OUTGOING_TYPE 2
* 未接CallLog.Calls.MISSED_TYPE 3
*/
public List<NetiveUser> getAllCallRecord(){
ContentResolver resolver = context.getContentResolver();
String selection = "0==0) GROUP BY ("+ Calls.NUMBER;
Cursor cursor = resolver.query(CallLog.Calls.CONTENT_URI, new String[]{
CallLog.Calls._ID,CallLog.Calls.NUMBER,CallLog.Calls.CACHED_NAME,CallLog.Calls.TYPE,CallLog.Calls.DATE
}, selection, null, null);
return getCallRecord(cursor);
}
public List<NetiveUser> getCallRecord(Cursor cursor){
List<NetiveUser> list = new ArrayList<NetiveUser>();
if(cursor == null){
return list;
}
while(cursor.moveToNext()){
System.out.println("count---"+cursor.getCount());
String id = cursor.getString(0);
String number = cursor.getString(1);
String name = cursor.getString(2);
int type = cursor.getInt(3);
Date time = new Date(Long.parseLong(cursor.getString(4)));
//System.out.println(" id-----"+id+" number-----"+number+" name-----"+name+" type-----"+type+" time-----"+time);
netiveUser = new NetiveUser();
netiveUser.setId(id);
netiveUser.setUserName(name);
netiveUser.setPhoneNumber(number);
netiveUser.setType(String.valueOf(type));
netiveUser.setCtreatetime(time);
String times =getCallTiems(number,String.valueOf(type));
//for(int m = 0;m<times.length; m++){
netiveUser.setTimes(String.valueOf(times));
//System.out.println("times---"+String.valueOf(times[m]));
//}
list.add(netiveUser);
}
return list;
}
/**
* 分别获取来电 已拨、未接 的信息(包含电话号码,时间,姓名等)
* 1 来电
* 2 已拨
* 3 未接
*/
public List<NetiveUser> getCallInfo(int s){
List<NetiveUser> list = new ArrayList<NetiveUser>();
ContentResolver cr = context.getContentResolver();
String number =null;
Date time = null;
String username = null;
if( s == 1){
Cursor cursor =cr.query(CallLog.Calls.CONTENT_URI, new String[]{CallLog.Calls.NUMBER,CallLog.Calls.CACHED_NAME
,CallLog.Calls.DATE},
CallLog.Calls.TYPE + "=?",
new String[] { String.valueOf(CallLog.Calls.INCOMING_TYPE) },
CallLog.Calls.DEFAULT_SORT_ORDER);
for (int i = 0; i < cursor.getCount(); i++)
{
cursor.moveToPosition(i);
number = cursor.getString(0);
username = cursor.getString(1);
time = new Date(Long.parseLong(cursor.getString(2)));
netiveUser = new NetiveUser();
netiveUser.setPhoneNumber(number);
netiveUser.setUserName(username);
netiveUser.setCtreatetime(time);
//System.out.println("number---"+number+"username---"+username+"date---"+date);
}
list.add(netiveUser);
}
if(s == 2){
Cursor cursor =cr.query(CallLog.Calls.CONTENT_URI, new String[]{CallLog.Calls.NUMBER,CallLog.Calls.CACHED_NAME
,CallLog.Calls.DATE},
CallLog.Calls.TYPE + "=?",
new String[] { String.valueOf(CallLog.Calls.OUTGOING_TYPE) },
CallLog.Calls.DEFAULT_SORT_ORDER);
String str1 =null;
for (int i = 0; i < cursor.getCount(); i++)
{
cursor.moveToPosition(i);
number = cursor.getString(0);
username = cursor.getString(1);
time = new Date(Long.parseLong(cursor.getString(2)));
netiveUser = new NetiveUser();
netiveUser.setPhoneNumber(number);
netiveUser.setUserName(username);
netiveUser.setCtreatetime(time);
//System.out.println("number---"+number+"username---"+username+"date---"+date);
}
list.add(netiveUser);
}
if(s == 3){
Cursor cursor =cr.query(CallLog.Calls.CONTENT_URI, new String[]{CallLog.Calls.NUMBER,CallLog.Calls.CACHED_NAME
,CallLog.Calls.DATE},
CallLog.Calls.TYPE + "=?",
new String[] { String.valueOf(CallLog.Calls.MISSED_TYPE) },
CallLog.Calls.DEFAULT_SORT_ORDER);
String str1 =null;
for (int i = 0; i < cursor.getCount(); i++)
{
cursor.moveToPosition(i);
number = cursor.getString(0);
username = cursor.getString(1);
time = new Date(Long.parseLong(cursor.getString(2)));
netiveUser = new NetiveUser();
netiveUser.setPhoneNumber(number);
netiveUser.setUserName(username);
netiveUser.setCtreatetime(time);
//System.out.println("number---"+number+"username---"+username+"date---"+date);
}
list.add(netiveUser);
}
return list;
}
/**
* 获取来电 、未接、 已拨电话号码数目
*/
public int[] getCallTimes(String number){
ContentResolver cr = context.getContentResolver();
//来电
int inCallCoutnt=0;
//已拨
int outCallCount =0;
//未接
int missCallCount= 0;
int inCount = 0;
int outCount = 0;
int missCount = 0;
int[] data = new int[3];
Cursor cursor = cr.query(CallLog.Calls.CONTENT_URI,
new String[]{CallLog.Calls.TYPE,CallLog.Calls.NUMBER,CallLog.Calls.NEW},
"number=?", new String[]{number}, CallLog.Calls.DEFAULT_SORT_ORDER);
if( cursor != null){
while(cursor.moveToNext()){
int type = cursor.getInt(cursor.getColumnIndex(CallLog.Calls.TYPE));
switch(type){
case Calls.INCOMING_TYPE:
if(cursor.getInt(cursor.getColumnIndex(Calls.NEW)) == 1){
inCallCoutnt ++;
}
break;
case Calls.OUTGOING_TYPE:
if(cursor.getInt(cursor.getColumnIndex(Calls.NEW)) == 1){
outCallCount ++;
}
break;
case Calls.MISSED_TYPE:
if(cursor.getInt(cursor.getColumnIndex(Calls.NEW)) == 1){
missCallCount ++;
}
break;
}
}
}
inCount = inCallCoutnt;
outCount = outCallCount;
missCount = missCallCount;
data = new int[]{inCount,outCount,missCount };
return data;
}
/**通过电话号码和type查询 来电‘已拨’未接
* 1 来电
* 2 已拨
* 3 未接
* @param number
* @param type
* @return
*/
public String getCallTiems(String number, String type){
ContentResolver cr = context.getContentResolver();
Cursor cur = cr.query(CallLog.Calls.CONTENT_URI, new String[]{CallLog.Calls.NEW},
"number=? AND type=?", new String[]{number ,type}, null);
int typeString = 0;
if(cur !=null){
while(cur.moveToNext()){
typeString = cur.getInt(cur.getColumnIndex(CallLog.Calls.NEW));
}
}
return String.valueOf(typeString);
}
/**
* 删除通话记录(可批量删除数据)
* @param id
*
* id:格式 1 2 3 4
*/
public void delCallRecord(NetiveUser user){
ContentResolver cr = context.getContentResolver();
if(user.getId().length()> 1){
String[] param = user.getId().split(" ");
for(int i= 0; i<param.length; i++){
String paramId = param[i];
String queryStr = "_id='"+paramId+"'";
int m = cr.delete(CallLog.Calls.CONTENT_URI, queryStr, null);
System.out.println("m----"+m);
}
}else{
cr.delete(CallLog.Calls.CONTENT_URI, "_id=?", new String[] {user.getId()});
}
}
/**
* 获取所有短息记录
* _id => 短消息序号 如100
thread_id => 对话的序号 如100
address => 发件人地址,手机号.如+8613811810000
person => 发件人,返回一个数字就是联系人列表里的序号,陌生人为null
date => 日期 long型。如1256539465022
protocol => 协议 0 SMS_RPOTO, 1 MMS_PROTO
read => 是否阅读 0未读, 1已读
status => 状态 -1接收,0 complete, 64 pending, 128 failed
type => 类型 1是接收到的,2是已发出
body => 短消息内容
service_center => 短信服务中心号码编号。如+8613800755500
*
*/
public List<NetiveUser> getSMSRecrod(){
ContentResolver cr = context.getContentResolver();
Uri uri = Uri.parse("content://sms");
Cursor cursor = cr.query(uri, new String[]{"_id","address","person","body","date","type"},
null, null, "date desc");
List<NetiveUser> list = new ArrayList<NetiveUser>();
if(cursor.moveToFirst()){
int id;
String name;
String address;
String body;
String date;
String type;
int idCloum = cursor.getColumnIndex("_id");
int nameCloum = cursor.getColumnIndex("person");
int addressCloum = cursor.getColumnIndex("address");
int bodyCloum = cursor.getColumnIndex("body");
int dateCloum = cursor.getColumnIndex("date");
int typeCloum = cursor.getColumnIndex("type");
do{
id = cursor.getInt(idCloum);
name = cursor.getString(nameCloum);
address = cursor.getString(addressCloum);
body = cursor.getString(bodyCloum);
Date time = new Date(Long.parseLong(cursor.getColumnName(dateCloum)));
type = cursor.getString(typeCloum);
NetiveUser user = new NetiveUser();
user.setId(String.valueOf(id));
user.setUserName(name);
user.setAddress(address);
user.setBody(body);
user.setType(type);
user.setCtreatetime(time);
}while(cursor.moveToNext());
}
return list;
}
/**
* 发送短信息
* @param phoneNumber
* @param message
*/
public void sendSMS(NetiveUser user){
SmsManager sms = SmsManager.getDefault();
PendingIntent pi = PendingIntent.getActivity(context, 0, new Intent(), 0);
if(user.getBody().length() > 70){
ArrayList<String > msgs = sms.divideMessage(user.getBody());
for(String msg : msgs){
sms.sendTextMessage(user.getPhoneNumber(), null, msg, pi, null);
}
}else{
sms.sendTextMessage(user.getPhoneNumber(), null, user.getBody(), pi, null);
}
}
/**
* 删除短信息
* @param id
* @param f
*
* f : ture 单个删除
* f : false 全部删除
*/
public void delSms(NetiveUser user,boolean f){
ContentResolver cr = context.getContentResolver();
if(f == true){
Uri url = Uri.parse("content://sms");
cr.delete(url, "_id = ?", new String[]{user.getId()});
}else{
Uri url = Uri.parse("content://sms");
cr.delete(url, null, null);
}
}
/**
* 获取未读和已读短信数目
* int i
* i : 1接受 已读
* i : 2 接受未读
* i : 3 发送已读
* i : 4 发送未读
*/
public int getSMSCount(int i){
Uri uri = Uri.parse("content://sms");
ContentResolver cr = context.getContentResolver();
int smsCount = 0;
if(i == 1){
Cursor cursor = cr.query(uri, null, "type =1 and read=1", null, null);
smsCount = cursor.getCount();
}
if(i == 2){
Cursor cursor = cr.query(uri, null, "type =1 and read=0", null, null);
smsCount = cursor.getCount();
}
if(i == 3){
Cursor cursor = cr.query(uri, null, "type =2 and read=1", null, null);
smsCount = cursor.getCount();
}
if(i == 4){
Cursor cursor = cr.query(uri, null, "type =2 and read=0", null, null);
smsCount = cursor.getCount();
}
return smsCount;
}
//获取彩信 数目
/*
public int getSMSMCount(int i){
Uri uri = Uri.parse("content://mms/inbox");
ContentResolver cr = context.getContentResolver();
int smsMCount = 0;
if(i == 1){
Cursor cursor = cr.query(uri, null, "type =1 and read=1", null, null);
smsMCount = cursor.getCount();
}
if(i == 2){
Cursor cursor = cr.query(uri, null, "type =1 and read=0", null, null);
smsMCount = cursor.getCount();
}
return smsMCount;
}
*/
}
转载于:https://my.oschina.net/xiahuawuyu/blog/89422