android studio发短信

在AndroidManifest.xml中添加权限

 <uses-permission android:name="android.permission.SEND_SMS"/>

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <TextView
            android:id="@+id/tv_phnum"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/tv_phnum" />

        <EditText
            android:id="@+id/et_phnum"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:inputType="phone" />

        <TextView
            android:id="@+id/tv_text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/tv_text" />

        <EditText
            android:id="@+id/et_text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:inputType="textPersonName"
            android:text="" />

        <Button
            android:id="@+id/bt_send"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/bt_send" />
    </LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

MainActicity.java

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button bt_send=(Button)this.findViewById(R.id.bt_send);
        bt_send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                EditText et_phnum=(EditText)findViewById(R.id.et_phnum);
                EditText et_text=(EditText)findViewById(R.id.et_text);
                String phnum=et_phnum.getText().toString();
                String text=et_text.getText().toString();

                SmsManager smsManager=SmsManager.getDefault();
//                获取一个默认实例,根据描述是将smsManager与一个subscription id连接

                ArrayList<String> texts=smsManager.divideMessage(text);
//                将短信内容以70个字为界限分隔开

                for(String Text:texts) {
                    smsManager.sendTextMessage(phnum,null,Text,null,null);
//                   sendTextMessage的参数分别为destinationAdddress 目标号码,scAddress使用的短信服务中心(使用默认的就用null),
//                   text短信内容,sentIntent默认为null,deliveryIntent可以设置查看对方接受的状态(是否成功接收),
                }
//                用foreach循环将texts遍历,然后存放在数组Text中

                Toast.makeText(MainActivity.this,"success",Toast.LENGTH_LONG).show();
//                发送是否成功的提示,context指在哪个页面显示信息,MainActivity.this指在当前页面显示
//                text是指信息的内容
//                Toast.LENGTH_LONG指显示的时间长短

            }
        });

    }
}

foreach循环与for循环的区别就是foreach可以自动过滤数组中的空字符,当数组中某个空间为null时,可以过滤掉。
假如在发短信的时候,短信内容的数组中有一个空间为null,发送短信就会失败,程序会崩溃。

后面做了一个实验,将foreach循环改为

                String Texts = "abcdefg";
                for(int i=0;i<texts.size();i++){
                    Texts=texts.get(i);
                }
                Texts.replace("b",null);
                smsManager.sendTextMessage(phnum,null,Texts,null,null);

程序就不能运行
当然,如果把Texts.replace("b",null);去掉的话与foreach循环的结果是一样。但是用foreach循环会更加方便

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。