我在这里使用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);
}
}
如何从此发送和接收短信的时间。
答案 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...