我正在为SMS实施ContentObserver。
我在SMS(Android默认消息应用程序)中的任何更改时调用了onChange
函数。
我已经与LeEco一起测试它工作正常,在发送,接收,失败等时触发。
但是,在三星Galaxy S6中,它不适用于失败条件。其他人,发送/接收短信工作正常。
private static final String CONTENT_SMS = "content://sms";
private static final String MSG_ID = "_id";
private static final String MSG_TYPE = "type";
private static final String MSG_ADDRESS = "address";
private static final String MSG_DATE = "date";
private static final String MSG_BODY = "body";
String lastMessageID = "";
public final Uri CONTENT_SMS_URI = Uri.parse(CONTENT_SMS);
public void onChange(boolean selfChange) {
super.onChange(selfChange);
Cursor cursor = context.getContentResolver().query(CONTENT_SMS_URI, null, null, null, null);
if (null != cursor && cursor.moveToNext()){
// String protocol = cursor.getString(cursor.getColumnIndex("protocol"));
int type = cursor.getInt(cursor.getColumnIndex(MSG_TYPE));
String newMsgId = cursor.getString(cursor.getColumnIndex(MSG_ID));
if (!lastMessageID.equals(newMsgId)){
lastMessageID = newMsgId;
String to = cursor.getString(cursor.getColumnIndex(MSG_ADDRESS));
Date now = new Date(cursor.getLong(cursor.getColumnIndex(MSG_DATE)));
String message = cursor.getString(cursor.getColumnIndex(MSG_BODY));
if (type == Telephony.TextBasedSmsColumns.MESSAGE_TYPE_SENT){
// SMS sent
Log.e("MSG_SENT", to + " - " + now + " - " + message);
}
else if(type == Telephony.TextBasedSmsColumns.MESSAGE_TYPE_FAILED){
// Failed out going message
Log.e("MSG_FAILED", to + " - " + now + " - " + message);
}
}
}
}