Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can use the below for reference and modify the same. i tested it on my phone works. Please post the logcat details of the crash wihtout which we can only guess the error.</p> <p>you are calling finish() inside a button click which will finish the activity. Not sure what you are trying to do.</p> <p>MainActivity.java</p> <pre><code>public class SendSmsActivity extends Activity { Button buttonSend,get; private static final int CONTACT_PICKER_RESULT = 1001; EditText phoneNo; EditText textSMS; int columnIndex_number; static String stringNumber; String sms; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.send); phoneNo = (EditText)findViewById(R.id.editText1); textSMS = (EditText) findViewById(R.id.editText2); get = (Button) findViewById(R.id.button1); get.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Intent contactPickerIntent = new Intent(Intent.ACTION_PICK, Contacts.CONTENT_URI); startActivityForResult(contactPickerIntent, CONTACT_PICKER_RESULT); } }); buttonSend = (Button) findViewById(R.id.button2); buttonSend.setOnClickListener(new OnClickListener() { @Override public void onClick(View v){ try { String sms = textSMS.getText().toString().trim(); Toast.makeText(getApplicationContext(), phoneNo.getText().toString()+sms, Toast.LENGTH_LONG).show(); SmsManager smsManager = SmsManager.getDefault(); smsManager.sendTextMessage(phoneNo.getText().toString(), null, sms, null, null); Toast.makeText(getApplicationContext(), "SMS Sent!", Toast.LENGTH_LONG).show(); } catch (Exception e) { Toast.makeText(getApplicationContext(), "SMS faild, please try again later!", Toast.LENGTH_LONG).show(); e.printStackTrace(); } } }); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (resultCode == RESULT_OK) { switch (requestCode) { case CONTACT_PICKER_RESULT: Cursor cursor = null; String phoneNumber = ""; List&lt;String&gt; allNumbers = new ArrayList&lt;String&gt;(); int phoneIdx = 0; try { Uri result = data.getData(); String id = result.getLastPathSegment(); cursor = getContentResolver().query(Phone.CONTENT_URI, null, Phone.CONTACT_ID + "=?", new String[] { id }, null); phoneIdx = cursor.getColumnIndex(Phone.DATA); if (cursor.moveToFirst()) { while (cursor.isAfterLast() == false) { phoneNumber = cursor.getString(phoneIdx); allNumbers.add(phoneNumber); phoneNo.setText(cursor.getString(phoneIdx)); cursor.moveToNext(); } } else { //no results actions } } catch (Exception e) { //error actions } finally { if (cursor != null) { cursor.close(); } } break; } } else { //activity result error actions } } } </code></pre> <p>send.xml</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" &gt; &lt;TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_marginTop="35dp" android:layout_toRightOf="@+id/button1" android:text="Phone No" android:textSize="25sp" /&gt; &lt;EditText android:id="@+id/editText1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_marginTop="78dp" android:padding="20dp" android:ems="10" &gt; &lt;requestFocus /&gt; &lt;/EditText&gt; &lt;TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="30dp" android:text="SMS Content" android:textSize="25sp" /&gt; &lt;EditText android:id="@+id/editText2" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="20dp" android:padding="20dp" /&gt; &lt;LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" android:paddingTop="20dp" &gt; &lt;Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="16dp" android:layout_marginLeft="34dp" android:text="Get" /&gt; &lt;Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="60dp" android:paddingLeft="20dp" android:text="Send" /&gt; &lt;/LinearLayout&gt; &lt;/LinearLayout&gt; </code></pre> <p>add the permissions in manifest</p> <pre><code> &lt;uses-permission android:name="android.permission.READ_CONTACTS"/&gt; &lt;uses-permission android:name="android.permission.SEND_SMS" /&gt; </code></pre> <p>Snapshot from emulator. If you need to send sms you will have to use real device</p> <p>If the number had spaces in between you can trim the space</p> <p><img src="https://i.stack.imgur.com/mJILD.png" alt="enter image description here"></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload