简体   繁体   English

如何从Android中的特定SIM发送短信

[英]How to send Sms from particular SIM in android

Hi i create an SMS 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 particular SIM before send the message and also i want to send Sms automatically please help me.. 仅从SIM2发送消息。如何在发送消息之前选择特定的SIM卡 ,并且我也想自动发送短信,请帮助我。

this is my java file 这是我的java文件

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

}

and this is my Xml file 这是我的Xml文件

activity_main.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>

and this is my manifest file 这是我的清单文件
message Manifest.xml 消息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>

This is not the answer, but this is the only solution for your search Dual SIM card Android . 这不是答案,但这是您搜索Android Dual SIM卡的唯一解决方案。 Don't forget to read the comments 别忘了阅读评论

Also add required permissions SEND_SMS 同时添加所需的权限SEND_SMS

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM