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/)。