Android 短信开发学习

短信 sms 
文件 /data/data/com.android.providers.telephony/databases/mmssms.db 
这个数据库有13张表,sms表存了短信信息。

sms表的uri是        
public static final Uri CONTENT_URI =              Uri.parse("content://sms"); 

strColumnName=_id strColumnValue=48 //短消息序号 
strColumnName=thread_id strColumnValue=16 //对话的序号(conversation)
strColumnName=address strColumnValue=+8613411884805 //发件人地址,手机号
strColumnName=person strColumnValue=null //发件人,返回一个数字就是联系人列表里的序号,陌生人为null
strColumnName=date strColumnValue=1256539465022 //日期 long型,
strColumnName=protocol strColumnValue=0 //协议
strColumnName=read strColumnValue=1 //是否阅读
strColumnName=status strColumnValue=-1 //状态
strColumnName=type strColumnValue=1 //类型 1是接收到的,2是发出的
strColumnName=reply_path_present strColumnValue=0 //
strColumnName=subject strColumnValue=null //主题
strColumnName=body strColumnValue=您好 //短消息内容
strColumnName=service_center strColumnValue=+8613800755500 //短信服务中心号码编号,可以得知该短信是从哪里发过来的

/**
	 * 获取所有短息记录
	 * _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/90496

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值