每次接收或发送短信时,我都会在数据库中插入一条记录。它工作正常。但有时所有短信都会获得相同的 id 。在哪种情况下,我会为不同的短信获得相同的短信 ID。
public SmsMms(Handler handler) {
super(handler);
Log.d(TAG, "SMS Constructor");
}
public void onChange(boolean selfChange) {
super.onChange(selfChange);
Log.d("sms", "SMS ONCHANGE");
if(rc == null )
rc = new RecordCount(getApplicationContext());
Uri uriSMSURI = Uri.parse(SMS);
Cursor cur = getContentResolver().query(uriSMSURI, null, null,
null, null);
int rCount = cur.getCount();
long recCount = rc.select("SMS");
Log.d("sms", "rCount from sms db: " + rCount + "reccout from device db: " + recCount);
long diffCount;
if(rCount > recCount){
diffCount = rCount - recCount;
}else {
diffCount = recCount - rCount;
}
Log.d("sms", "diff: " + diffCount);
Log.d("sms", "sms count: " + rc.select("SMS") + "===rCount: " + rCount);
if (rCount >= recCount || diffCount > 1) {
Log.d("sms", "diff: ");
rc.updateRecordCount("SMS", rCount);
cur.moveToNext();
String protocol = cur.getString(cur.getColumnIndex("protocol"));
String content = cur.getString(cur.getColumnIndex("body"));
int msg_id = cur.getInt(cur.getColumnIndex("_id"));
Log.d("sms", "Message_id: " + msg_id);
if (protocol == null) {
Log.d("timest", "check protocol");
if (!content.equals(null) && msg_id != prev_msgid) {
Log.d("timest", "current msg" +msg_id);
dh.sms_insert(timeStamp(), content.length(), "sent");
prev_msgid = msg_id;
Log.d("timest", "previous msg"+prev_msgid);
Log.d("timest", "Outgoing Message" + content.length());
}
} else {
Log.d("sms", "in else");
Log.d("sms", "previous msg"+prev_msgid);
Log.d("sms", "current msg id " +msg_id);
if (!content.equals(null) && msg_id != prev_msgid ) {
Log.d("sms", "diff: " + diffCount);
Log.d("sms", "current msg id " +msg_id);
dh.sms_insert(timeStamp(), content.length(), "received");
prev_msgid = msg_id;
Log.d("sms", "Incoming Message" + content.length());
}
}
dh.smsList();
Log.d("sms", "msg list" + dh.smsList());
}else if(rCount < recCount){
rc.updateRecordCount("SMS", rCount);
}
这是未插入到表中的 sms 的 logcat 内容。但是在我们的情况下,当第一个发送者第二次发送 sms 时,新到达的消息 id 没有分配给变量message_id
,因此两个变量message_id
都previousmsg_id
将具有相同的值,因此消息计数是不递增。
04-18 09:39:36.092: D/sms(10033): SMS ONCHANGE
04-18 09:39:36.132: D/sms(10033): rCount from sms db: 52reccout from device db: 51
04-18 09:39:36.132: D/sms(10033): diff: 1
04-18 09:39:36.142: D/sms(10033): sms count: 51===rCount: 52
04-18 09:39:36.142: D/sms(10033): diff:
04-18 09:39:36.142: D/SmsReceiverService(714): insertUri> content://sms/178
04-18 09:39:36.152: D/SmsReceiverService(714): class: UNKNOWN
04-18 09:39:36.182: D/MmsSmsProvider(307): ids: 11
04-18 09:39:36.182: D/MmsSmsProvider(307): recipientIds: 11
04-18 09:39:36.232: D/sms(10033): Message_id: 175
04-18 09:39:36.232: D/sms(10033): in else
04-18 09:39:36.232: D/sms(10033): previous msg175
04-18 09:39:36.232: D/sms(10033): current msg id 175
04-18 09:39:36.242: D/sms(10033): msg list[12-04-18 09:35:30 , 8,received
04-18 09:39:36.242: D/sms(10033): , 12-04-18 09:35:54 , 8,received
04-18 09:39:36.242: D/sms(10033): , 12-04-18 09:36:44 , 8,received
04-18 09:39:36.242: D/sms(10033): ]
这仅在 HTC 设备中发生。在其他设备中,它工作正常。那么我该如何解决这个问题。感谢您的回答。