0

I'm trying to build a project that shows how you can send SMS via Android Application. I have built the project (in eclipse) using the SmsManager object, according to the code here: http://www.tutorialspoint.com/android/android_sending_sms.htm

the project is running good, but the SMS isn't sent to me. I guess that there is something else I need to add in order that the SMS actually be sent - so what am i missing? Or maybe I'm not inserting the number correctly? how should I write it? I tried a few ways...

Any help will be deeply appriciated! Thank you.

Bramat
  • 979
  • 4
  • 24
  • 40

1 Answers1

0

Here is the code for selecting the contact from the contact list on button click:

Button x;
String phn_no, msg,c;

    x.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                Intent intent = new Intent(Intent.ACTION_PICK,
                        ContactsContract.Contacts.CONTENT_URI);
                startActivityForResult(intent, PICK_CONTACT);
            }

        });


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);

        switch (requestCode) {
        case (1):
            if (resultCode == Activity.RESULT_OK) {
                Uri contactData = data.getData();
                Cursor c = getContentResolver().query(contactData, null, null,
                        null, null);
                if (c.moveToFirst()) {
                    String id = c
                            .getString(c
                                    .getColumnIndexOrThrow(ContactsContract.Contacts._ID));

                    String hasPhone = c
                            .getString(c
                                    .getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));

                    if (hasPhone.equalsIgnoreCase("1")) {
                        Cursor phones = getContentResolver()
                                .query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                                        null,
                                        ContactsContract.CommonDataKinds.Phone.CONTACT_ID
                                        + " = " + id, null, null);
                        phones.moveToFirst();
                        phn_no = phones.getString(phones
                                .getColumnIndex("data1"));

                        // Toast.makeText(getApplicationContext(), phn_no,
                        // Toast.LENGTH_LONG).show();
                        contact_num.setText(phn_no);

                        // String name =
                        // c.getString(c.getColumnIndex(StructuredPostal.DISPLAY_NAME));
                        // Toast.makeText(this, "contact info : "+
                        // phn_no+"\n"+name, Toast.LENGTH_LONG).show();
                    }

                }
            }
        }
    }

This is the code for sending SMS:

    void send_sms(final String phn_no, final String msg) {
        final SmsManager smsManager = SmsManager.getDefault();
        smsManager.sendTextMessage(phn_no, null, msg, null, null);
    }
ZygD
  • 22,092
  • 39
  • 79
  • 102