从onReceive android获取消息_id

时间:2015-10-04 07:23:23

标签: android sms

我试图获取一条消息ID,该消息ID保存在与每条消息关联的手机中。但我没有得到它。我现有的代码在这里

public void onReceive(Context context, Intent intent) {
        Bundle intentExtras = intent.getExtras();
        if (intentExtras != null) {
            Object[] sms = (Object[]) intentExtras.get(SMS_BUNDLE);
            String smsMessageStr = "";
            for (int i = 0; i < sms.length; ++i) {
                SmsMessage smsMessage = SmsMessage
                        .createFromPdu((byte[]) sms[i]);

                smsBody = smsMessage.getMessageBody().toString();
                address = smsMessage.getOriginatingAddress();
            //  smsMessageId = how to get message id ?
                long millis= smsMessage.getTimestampMillis();

                Date date = new Date(millis);
                Calendar c = Calendar.getInstance();

                // set the calendar to start of today
                c.set(Calendar.HOUR_OF_DAY, 0);
                c.set(Calendar.MINUTE, 0);
                c.set(Calendar.SECOND, 0);
                c.set(Calendar.MILLISECOND, 0);

                // and get that as a Date
                Date today = c.getTime();

                if (date.before(today)) {
                    smsDate = (String) DateFormat.format(" MMMM dd ", new Date(
                            millis));
                } else {
                    smsDate = (String) DateFormat.format("  h:mm ",
                            new Date(millis));
                }

                if (pre_address.equals(address)) {

                    smsMessageStr += "SMS From: " + address + "\n";
                    smsMessageStr += smsBody + "\n";
                }
            }

            SMSItem smsItem = new SMSItem();
            smsItem.sms = smsBody ; 
            smsItem.status = false;
            smsItem.time = smsDate;

            Toast.makeText(context, smsMessageStr, Toast.LENGTH_SHORT).show();

            // this will update the UI with message
            SmsActivity inst = SmsActivity.instance();
            if (inst != null) {
                inst.updateList(smsBody);
                if (pre_address.equals(address))
                    inst.Notify(inst.getMessageCountUnread()
                            + " unread message", address);
            }
        }

通过这个我得到短信的身体和发件人号码。但我知道android手机中的每条消息都有一个唯一的号码,我检索了它。但无法在onReceive方法中检索。如何在onReceive方法中检索消息ID?

1 个答案:

答案 0 :(得分:0)

Uri myMessage = Uri.parse("content://sms/");
ContentResolver cr = getContentResolver();
Cursor c = cr.query(myMessage, new String[] { "_id", "address", "date", "body","read" },"_id = "+smsID, null, null);

c.moveToFirst();

String Number = c.getString(c.getColumnIndexOrThrow("address")).toString();

String ReadStatus = c.getString(c.getColumnIndex("read"));
String Body = c.getString(c.getColumnIndexOrThrow("body")).toString();

c.close();