如何从android中的特定SIM发送Sms

时间:2013-08-18 15:40:36

标签: android

您好我创建一个短信应用程序。在这个应用程序中我给出了电话号码和消息。当我点击发送按钮。消息仅从SIM2发送。如何在发送消息前选择特定SIM ,我也想自动发送Sms请帮帮我..

这是我的java文件

MainActivity.java

    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;
    }

}

这是我的Xml文件

activity_main.xml中

<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" >

    <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" />

    <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" />

    <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" >

        <requestFocus /></EditText>

    <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" >

    </EditText>

</RelativeLayout>

这是我的清单文件
消息Manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.message"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="5"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.message.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

1 个答案:

答案 0 :(得分:1)

这不是答案,但这是您的搜索Dual SIM card Android的唯一解决方案。 不要忘记阅读评论

还要添加所需的权限SEND_SMS