Android 从pdus对象获取messageID
我的应用程序收听新的短信。现在,当我收到一条新的短信时,我会创建一条短信。为了对此进行进一步的操作,我需要消息的Id。因此,我对内容解析器进行如下查询:Android 从pdus对象获取messageID,android,Android,我的应用程序收听新的短信。现在,当我收到一条新的短信时,我会创建一条短信。为了对此进行进一步的操作,我需要消息的Id。因此,我对内容解析器进行如下查询: final Bundle bundle = intent.getExtras(); try { if (bundle != null) { //get the protocol discription unit ( SMSs) final Object[] pdusO
final Bundle bundle = intent.getExtras();
try {
if (bundle != null) {
//get the protocol discription unit ( SMSs)
final Object[] pdusObj = (Object[]) bundle.get("pdus");
for (int i = 0; i < pdusObj.length; i++) {
SmsMessage currentMessage = SmsMessage.createFromPdu((byte[]) pdusObj[i]);
String phoneNumber = currentMessage.getDisplayOriginatingAddress();
String message = currentMessage.getDisplayMessageBody();
long time_rcv = currentMessage.getTimestampMillis();
String [] relevantColumns = {"_id", "address", "body", "date_sent"};
Cursor cursor = context.getContentResolver().query(
Uri.parse("content://sms/inbox"),
relevantColumns,
"address = '" + phoneNumber + "' AND body = '" + message
+ "' AND date_sent = '" + time_rcv + "'",
null,
null);
if (cursor != null && cursor.moveToFirst()) {
int id = Integer.valueOf(cursor.getString(0));
Log.d(TAG,"ID SMS: " + id);
}
else
Log.e(TAG,"Failed to find ID");
Log.i(TAG, "FROM: " + phoneNumber + "; MSG: " + message);
Toast.makeText(context, "From: " + phoneNumber + ", MSG: " + message, Toast.LENGTH_LONG).show();
}
}
} catch (Exception e) {
Log.e(TAG, "Exception receiver" + e);
}
不管发生什么,它都找不到信息 我的代码是正确的。唯一的问题是,在我的receivebroadcast完成之前,短信不在短信/收件箱中。因此,下次您运行代码并手动填充字段时,代码会找到sms并为您提供id