博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android 的通话代码练习
阅读量:6833 次
发布时间:2019-06-26

本文共 9692 字,大约阅读时间需要 32 分钟。

hot3.png

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
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
getCallRecord(Cursor cursor){ List
list = new ArrayList
(); 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
getCallInfo(int s){ List
list = new ArrayList
(); 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
短消息序号 如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
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
list = new ArrayList
(); 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
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

你可能感兴趣的文章
Guava并发:ListenableFuture与RateLimiter示例
查看>>
hdoj_1042_N!_大數優化_模擬
查看>>
美团扫码付小程序的优化实践
查看>>
Spark 批量存取 HBase
查看>>
将应用的数据库复制到SD卡上面
查看>>
注意string类型的比较,string当做数据类型去比较
查看>>
第六天 Java的数据溢出应对方式
查看>>
Ubuntu缺少libjawt.so
查看>>
ASP.NET Core2.0 学习笔记(一)
查看>>
创建一个简单的PHP扩展
查看>>
Mastering Nginx 笔记一----Nginx基础及location解释
查看>>
第一天开始写博客
查看>>
(翻译)Quartz官方教程——第六课:CronTrigger
查看>>
squid windows 配置日志
查看>>
wordpress 安装主题
查看>>
linux磁盘管理及文件系统
查看>>
梭子鱼垃圾邮件网关-Barracuda Spam & Virus Firewall Email Alert: outQueueHigh
查看>>
mysql性能调优my.cnf详解
查看>>
Java基础知识回顾-7
查看>>
Django 使用分页
查看>>