0

I initially used the below code to read the receiving messages so that I can use it in my application for tracking a phone.

            Bundle bundle = intent.getExtras();
            SmsMessage[] msgs = null;
            String str = "";
            if (bundle != null) {
                // ---retrieve the SMS message received---
                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]);

                    str += msgs[i].getMessageBody().toString();

                }

                for (SmsMessage msg : messages) {
                        if (msg.getMessageBody().contains("SMSLOCATE:")) {
                                String[] tokens = msg.getMessageBody().split(":");
                                if (tokens.length >= 2) {
                                        String md5hash = PhoneFinder.getMd5Hash(tokens[1]);
                                        if (md5hash.equals(correctMd5)) {
                                                String to = msg.getOriginatingAddress();
                                                LocationManager lm =
                                                        (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);

                                                SmsManager sm = SmsManager.getDefault();

                                                sm.sendTextMessage(to, null, lm.getLastKnownLocation("gps").toString(),
                                                                null, null);

                                                Toast.makeText(context, context.getResources().getString(R.string.notify_text) + to,
                                                                Toast.LENGTH_SHORT).show();
                                        }

I get an error message saying "messages cannot be resolved to a variable" in the line

for(SmsMessage msg : messages) {

I do not know what to do here. Please help me out guys. Thanks in advance. :-)

UPDATE #1:

The response from hannanessay did the trick. BUT now, for createFromPdu, getMessageBody() , getOriginatingAddress() , getDefault() and sendTextMessage , I get an error saying "Call requires API level 4 (current min is 1)". Any help on this?

4

1 回答 1

1

从代码中的下面一行,我们可以看到使用的变量是 msgs。

  msgs = new SmsMessage[pdus.length];

当您在下面的行中使用消息时。将其更改为 msgs(它可能会起作用)。

  for (SmsMessage msg : messages)  //Old

  for (SmsMessage msg : msgs)   //New
于 2013-06-16T14:30:38.483 回答