android: getting current message received
How do I get the current message that is been received in the method:
public class RecieveSMS 开发者_C百科extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(SMS_RECEIVED)) {
try {
How would I retrieve the number, time and body? As I am trying to save the messages then abort the broadcast I've tried querying the content://sms but if they isn't already a message from that user it seams to receive the first message then block the rest.
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
SmsMessage[] msgs = null;
String messageContent = "";
String number = "";
if (bundle != null)
{
Object[] pdus = (Object[]) bundle.get("pdus");
msgs = new SmsMessage[pdus.length];
for (int i=0; i<msgs.length; i++){
msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
number = msgs[i].getOriginatingAddress();
messageContent = msgs[i].getMessageBody().toString();
}
}
}
Check this out : examples for creating broadcast reciever for SMS :
sms-messaging-in-android-send-and-receive
精彩评论