2

I wrote a program to send and received SMS in Android. Sending an SMS is successful, but when an SMS is received at the destination phone, a force close dialog is shown. After clicking it, the received SMS is opened.

I want the received SMS to be shown in a textview that I put into a page.

SmsReceiver.java

public class SmsReceiver extends BroadcastReceiver
    {
        public String str = "";
         @Override
            public void onReceive(Context context, Intent intent) 
            {
                //---get the SMS message passed in---
                Bundle bundle = intent.getExtras();        
                SmsMessage[] msgs = null;

                if (bundle != null)
                {
                     //Object[] smsExtra = (Object[]) bundle.getSMS_EXTRA_NAME);
                    //---retrieve the SMS message received---
                    Object[] pdus = (Object[]) bundle.get("pdus");
                    msgs = new SmsMessage[pdus.length]; 
                    //for put sms in database---------------------
                   // ContentResolver contentResolver = context.getContentResolver();
                    //End for put sms in database---------------------
                    for (int i=0; i<msgs.length; i++){
                        msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);    
                       // SmsMessage sms = SmsMessage.createFromPdu((byte[])smsExtra[i]);

                        str += "SMS from " + msgs[i].getOriginatingAddress();                     
                        str += " :";
                        str += msgs[i].getMessageBody().toString();
                        str += "\n";  
                    }

                    //---display the new SMS message---
                // Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
                   Intent act=new  Intent(context,MainActivity.class);
                   act.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                   act.putExtra("message",str);
                   context.startActivity(act);

                }                  
            }
        }

MainActivity.java

public class MainActivity extends Activity {
SmsReceiver _smsReceiver =new SmsReceiver();
private TextView showSms;
private String ReceivedSms;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // _smsReceiver.onReceive(getBaseContext(), getIntent());
        setContentView(R.layout.activity_main);
        Intent _intent =getIntent();
        ReceivedSms=_intent.getStringExtra("message");
          showSms=(TextView)this.findViewById(R.id.txt_Show);
          showSms.setText(ReceivedSms);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}
jonsca
  • 10,218
  • 26
  • 54
  • 62
user1770370
  • 267
  • 2
  • 7
  • 18

1 Answers1

0

try to test this code, i have implement a sample project code based on your code:

public class MainActivity extends Activity {
    private TextView showSms;
    private String   ReceivedSms;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        super.onCreate(savedInstanceState);
        // _smsReceiver.onReceive(getBaseContext(), getIntent());
        setContentView(R.layout.activity_main);
        Intent _intent = getIntent();
        ReceivedSms = _intent.getStringExtra("message");
        showSms = (TextView) this.findViewById(R.id.txt_Show);
        showSms.setText(ReceivedSms);

    }


}

public class SmsReceiver extends BroadcastReceiver {
    public String str = "";

    @Override
    public void onReceive(Context context, Intent intent) {
        // ---get the SMS message passed in---
        Bundle bundle = intent.getExtras();
        SmsMessage[] msgs = null;

        if (bundle != null) {
            // Object[] smsExtra = (Object[]) bundle.getSMS_EXTRA_NAME);
            // ---retrieve the SMS message received---
            Object[] pdus = (Object[]) bundle.get("pdus");
            msgs = new SmsMessage[pdus.length];
            // for put sms in database---------------------
            // ContentResolver contentResolver = context.getContentResolver();
            // End for put sms in database---------------------
            for (int i = 0; i < msgs.length; i++) {
                msgs[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
                // SmsMessage sms =
                // SmsMessage.createFromPdu((byte[])smsExtra[i]);

                str += "SMS from " + msgs[i].getOriginatingAddress();
                str += " :";
                str += msgs[i].getMessageBody().toString();
                str += "\n";
            }

            // ---display the new SMS message---
            // Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
            Intent act = new Intent(context, MainActivity.class);
            act.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            act.putExtra("message", str);
            context.startActivity(act);

        }
    }

On the manifest ensure adding permission:

<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />

android the receiver declaration:

        <receiver
            android:name="com.example.sms.SmsReceiver"
            class="com.example.sms.SmsReceiver" >
            <intent-filter android:priority="100" >
                <action android:name="android.provider.Telephony.SMS_RECEIVED" />
            </intent-filter>
        </receiver>

PS: delete the SmsReceiver _smsReceiver =new SmsReceiver(); from your activity.

Anis BEN NSIR
  • 2,555
  • 20
  • 29
  • It was yet as before!!! when sms is received in destination phone, show force close and close program in phone!!!!!!!!!!!!! – user1770370 Oct 24 '12 at 10:16
  • the sample project is working fine, please edit your question, add your manifest.xml file and delete: SmsReceiver _smsReceiver =new SmsReceiver(); from the Activity. – Anis BEN NSIR Oct 24 '12 at 10:25
  • I copy and paste your sample in my program!!!! I am really very Tired!!!! please help me!!!!!!!!! it was yet as befor, and it show force closed!!!! – user1770370 Oct 24 '12 at 10:29
  • How can i received sms in program, not in inbox message android phone? – user1770370 Oct 24 '12 at 12:09
  • see this link to prevent message box to receive sms message: http://stackoverflow.com/questions/11259729/sms-cannot-be-prevented-using-abortbroadcast – Anis BEN NSIR Oct 24 '12 at 12:58
  • thank you very much...you solved many problem....;) only one question: this program(send & received sms)when closed, although sms received from any one, this program open and running!!!! I want to that if sms received from special number, program open and running!!! – user1770370 Oct 24 '12 at 13:10
  • I would prefer to be payed for solving problems :) you have to compare the received number with your number on the onReceive method. – Anis BEN NSIR Oct 24 '12 at 13:29