title: How to send Sms from particular SIM in android
body: <p>Hi i create an <i>SMS</i> Application.in this app i give phone number and message.when i click the  send button. the message send from SIM2 only.how do i select the <b> particular SIM </b> before send the message and also  i want to send Sms automatically please help me..</p>
  
  <p>this is my java file<br></p>
  
  <p><i>MainActivity.java</i></p>
  
  <pre><code>    package com.example.message;
  
        import android.os.Bundle;
        import android.app.Activity;
        import android.content.Context;
        import android.telephony.SmsManager;
        import android.view.Menu;
        import android.view.View;
        import android.view.View.OnClickListener;
        import android.widget.Button;
         import android.widget.EditText;
        import android.widget.Toast;
  
  
  
        public class MainActivity extends Activity 
  {
      Button snd;
      EditText no;
      EditText msg;
      String message,phoneno;
      protected Context context;
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          snd=(Button)findViewById(R.id.button1);
          no=(EditText)findViewById(R.id.editText1);
          msg=(EditText)findViewById(R.id.editText2);
          final SmsManager smsManager = SmsManager.getDefault();
       snd.setOnClickListener(new OnClickListener()
          {
  
              @Override
              public void onClick(View v) {
                  // TODO Auto-generated method stub
                  phoneno=String.valueOf(no.getText());
                  message=String.valueOf(msg.getText());
                  try
                  {
                  smsManager.sendTextMessage(phoneno,null,message,null,null);
                  Toast.makeText(context, "SMS Sent to " + phoneno,Toast.LENGTH_LONG).show();
              } 
                  catch (Exception e) {
              Toast.makeText(context,"SMS faild, please try again later!",Toast.LENGTH_LONG).show();
              e.printStackTrace();
              }
                  }
              }
                  );
  
      }
  
      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
          // Inflate the menu; this adds items to the action bar if it is present.
          getMenuInflater().inflate(R.menu.main, menu);
          return true;
      }
  
  }
  </code></pre>
  
  <p>and this is my Xml file<br></p>
  
  <p><i>activity_main.xml</i></p>
  
  <pre><code>&lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:paddingBottom="@dimen/activity_vertical_margin"
      android:paddingLeft="@dimen/activity_horizontal_margin"
      android:paddingRight="@dimen/activity_horizontal_margin"
      android:paddingTop="@dimen/activity_vertical_margin"
      tools:context=".MainActivity" &gt;
  
      &lt;Button
          android:id="@+id/button1"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_alignParentBottom="true"
          android:layout_marginBottom="140dp"
          android:layout_marginLeft="38dp"
          android:text="@string/btn" /&gt;
  
      &lt;TextView
          android:id="@+id/textView1"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_alignLeft="@+id/editText1"
          android:text="@string/title"
          android:textSize="@dimen/tsize" /&gt;
  
      &lt;EditText
          android:id="@+id/editText1"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_above="@+id/editText2"
          android:layout_alignLeft="@+id/button1"
          android:layout_marginBottom="24dp"
          android:ems="10"
          android:inputType="number" &gt;
  
          &lt;requestFocus /&gt;&lt;/EditText&gt;
  
      &lt;EditText
          android:id="@+id/editText2"
          android:layout_width="wrap_content"
          android:layout_height="@dimen/msgheight"
          android:layout_above="@+id/button1"
          android:layout_alignLeft="@+id/editText1"
          android:layout_marginBottom="60dp"
          android:ems="10"
          android:inputType="text"
          android:maxLines="10"
          android:scrollHorizontally="true" &gt;
  
      &lt;/EditText&gt;
  
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p>and this is my manifest file<br>
  <i>message Manifest.xml</i></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.example.message"
      android:versionCode="1"
      android:versionName="1.0" &gt;
  
      &lt;uses-sdk
          android:minSdkVersion="5"
          android:targetSdkVersion="17" /&gt;
  
      &lt;application
          android:allowBackup="true"
          android:icon="@drawable/ic_launcher"
          android:label="@string/app_name"
          android:theme="@style/AppTheme" &gt;
          &lt;activity
              android:name="com.example.message.MainActivity"
              android:label="@string/app_name" &gt;
              &lt;intent-filter&gt;
                  &lt;action android:name="android.intent.action.MAIN" /&gt;
  
                  &lt;category android:name="android.intent.category.LAUNCHER" /&gt;
              &lt;/intent-filter&gt;
          &lt;/activity&gt;
      &lt;/application&gt;
  
  &lt;/manifest&gt;
  </code></pre>