android短信数据库监听

1、内容监听类

public class SMSContentObserver extends ContentObserver {

public static final int READ_MSG_SUCC = 2133;
private static String TAG = "SMSContentObserver";
private Context mContext;
private Handler mHandler;//更新UI线程
public SMSContentObserver(Context context,Handler handler) {
	super(handler);
	mContext = context;
	mHandler = handler;
}
/**
* 当所监听的Uri发生改变时,就会回调此方法
* @param selfChange 此值意义不大 一般情况下该回调值false
*/
@Override
public void onChange(boolean selfChange){
	try {
		Log.i(TAG, "the sms table has changed");
		String[] projection = new String[] { "_id", "address", "person",
		"body", "date", "type" };
		Uri uri = Uri.parse("content://sms");
		Cursor cusor = mContext.getContentResolver().query(uri, projection, null, null,
		"date desc");
		if(cusor != null){
		cusor.moveToFirst();
		SmsBean bean = new SmsBean();
		bean.setContent_(cusor.getString(3));
		bean.setSrcAddr_(cusor.getString(1));
		bean.setTime_(cusor.getString(4));
		bean.setName_(cusor.getString(2));
		LogUtil.showPrint("SmsBean="+bean.toString());
		Message msg = mHandler.obtainMessage(READ_MSG_SUCC, bean);
		msg.sendToTarget();
	}
	} catch (Exception e) {
		// TODO: handle exception
		e.printStackTrace();
	}
}
}

2、短信bean

public class SmsBean implements Serializable{

private static final long serialVersionUID = 4425719463747374158L;
private String srcAddr_ = null;
private String content_ = null;
private String time_ = null;
private String name_ = null;

public String getName_() {
	return name_;
}
public void setName_(String name_) {
	this.name_ = name_;
}
public String getSrcAddr_() {
	return srcAddr_;
}
public void setSrcAddr_(String srcAddr_) {
	this.srcAddr_ = srcAddr_;
}
public String getContent_() {
	return content_;
}
public void setContent_(String content_) {
	this.content_ = content_;
}
public String getTime_() {
	return time_;
}
public void setTime_(String time_) {
	this.time_ = time_;
}
@Override
public String toString() {
	// TODO Auto-generated method stub
	return "srcAddress="+srcAddr_+" name="+name_+" time="+time_+" content="+content_;
}
}

原创内容转载请保留出处GEEK笔记(https://www.geekapp.cn/)。

原创博客,转载请标明出处:https://www.geekapp.cn/archives/198.html
暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇