收到短信的发送和接收时间

时间:2017-12-31 19:42:00

标签: android sms android-contentprovider

我在这里使用BroadcastReceiver阅读收到的短信。

 @Override
    public void onReceive(Context context, Intent intent) {
        final Bundle bundle = intent.getExtras();
        try {
            if (bundle != null) {
                final Object[] pdusObj = (Object[]) bundle.get("pdus");
                assert pdusObj != null;
                for (int i = 0; i < pdusObj.length; i++) {
                    SmsMessage currentMessage = SmsMessage.createFromPdu((byte[]) pdusObj[i]);
                    String senderNum = currentMessage.getDisplayOriginatingAddress();
                    String message = currentMessage.getDisplayMessageBody();
                    Log.i("SmsReceiver", "senderNum: " + senderNum + "; message: " + message);

                    Intent myIntent = new Intent("sms_received");
                    myIntent.putExtra("message", message);
                    LocalBroadcastManager.getInstance(context).sendBroadcast(myIntent);
                }
            }

        } catch (Exception e) {
            Log.e("SmsReceiver", "Exception smsReceiver" + e);

        }
    }

如何从此发送和接收短信的时间。

1 个答案:

答案 0 :(得分:0)

您的部分问题已经回答here

要获得发送时间,您必须使用getTimestampMillis()方法。

Calendar sendTime = Calendar.getInstance();
calendar.setTimeInMillis(smsMessage.getTimestampMillis());

int date = sendTime.get(CALENDAR.DATE);
int hour = sendTime.get(CALENDAR.HOUR_OF_DAY);
// etc...

要获得接收时间,请使用System.currentTimeMillis()。

Calendar receiveTime = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());

int date = receiveTime.get(CALENDAR.DATE);
int hour = receiveTime.get(CALENDAR.HOUR_OF_DAY);
// etc...