简体   繁体   中英

timestamp from Incoming SMS android

How can I get time stamp of incoming sms. Here is my code.

 public void onReceive(Context context, Intent intent) {
    Log.i("SMS Reciever", "smsReceiver: SMS Received");
    Bundle bundle = intent.getExtras();
    if (bundle != null) {
        Log.i("SMS Reciever", "smsReceiver : Reading Bundle");
        Object[] pdus = (Object[])bundle.get("pdus");
        for (int i = 0; i < pdus.length; i++) {
        SmsMessage sms = SmsMessage.createFromPdu((byte[])pdus[i]);
        SmsMessage currentMessage = SmsMessage.createFromPdu((byte[]) pdus[i]);
        String phoneNumber = currentMessage.getDisplayOriginatingAddress();
        String senderNum = phoneNumber;
        String message = currentMessage.getDisplayMessageBody();
        Log.i("SmsReceiver", "Send From: "+ senderNum + "; message: " + message);

try as to get Timestamp from Incoming SMS :

SmsMessage currentMessage = SmsMessage.createFromPdu((byte[]) pdus[i]);
 // get Timestamp
long timstamp=currentMessage.getTimestampMillis();

Soon as you receive an SMS on your device, Broadcast receiver handles it, as you've codded it. so to get time at which SMS is received, you may use:

long smsReceiveTime = System.currentTimeMillis();

after your

String message = currentMessage.getDisplayMessageBody();

and same way, for date:

SimpleDateFormat df = new SimpleDateFormat("dd-MMM-yyyy");
String formattedDate = df.format(c.getTime());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM