我在我的应用程序中注册了 SMS ContentObserver。我想用 MessageID 更新我的 ArrayList onChange。这是我的 ObServer 代码..
public class SmsObserver extends ContentObserver {
private Context mContext;
private String contactId = "", contactName = "";
private String smsBodyStr = "", phoneNoStr = "";
private long smsDatTime = System.currentTimeMillis();
static final Uri SMS_STATUS_URI = Uri.parse("content://sms");
public SmsObserver(Handler handler, Context ctx) {
super(handler);
mContext = ctx;
}
public boolean deliverSelfNotifications() {
return true;
}
public void onChange(boolean selfChange) {
try{
Cursor sms_sent_cursor = mContext.getContentResolver().query(SMS_STATUS_URI, null, null, null, null);
if (sms_sent_cursor != null) {
if (sms_sent_cursor.moveToFirst()) {
String protocol = sms_sent_cursor.getString(sms_sent_cursor.getColumnIndex("protocol"));
if(protocol == null){
int type = sms_sent_cursor.getInt(sms_sent_cursor.getColumnIndex ("type"));
if(type == 2){
smsBodyStr = sms_sent_cursor.getString(sms_sent_cursor.getColumnIndex("body")).trim();
phoneNoStr = sms_sent_cursor.getString(sms_sent_cursor.getColumnIndex("address")).trim();
smsDatTime = sms_sent_cursor.getLong(sms_sent_cursor.getColumnIndex("date"));
}
}
}
} else {
Log.e("Info","Send Cursor is Empty");
}
} catch(Exception sggh) {
Log.e("Error", "Error on onChange : "+sggh.toString());
}
super.onChange(selfChange);
}
}
但是,我正在发送两条消息。第一个是手机关机。二是手机打开。第一次更新是后者。第二次更新是我第一次发送手机打开。有了这个 ObServer,我只能访问最新的帖子。仅显示最新数据。如何获取新的更新数据?