Android实战-短信发送器

Android实战--短信发送器

首先设计界面

<LinearLayout 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" 
    android:orientation="vertical"
    >

    <EditText 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请输入号码"
        />
     <EditText 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请输入内容"
        />

</LinearLayout>

Android实战-短信发送器

如果希望输入内容框变大变宽

可以设置

android:lines="6"

Android实战-短信发送器

最后再添加button按钮

完整的代码:

<LinearLayout 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" 
    android:orientation="vertical"
    >

    <EditText 
        android:id="@+id/et_phone"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请输入号码"
        />
     <EditText 
         android:id="@+id/et_content"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请输入内容"
        android:lines="6"
        />
     <Button 
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:onClick="send"
         android:text="发送"
         />

</LinearLayout>

界面如下:

Android实战-短信发送器

点击发送后会发现报错,权限没设置

Android实战-短信发送器

再新建一个模拟器avd2.3(版本低,运行快)

Android实战-短信发送器

同时启动两个模拟器

Android实战-短信发送器

注意:短信发送器的号码为5556

以上就完成了简单的发送短信功能

但是上面的操作还是存在一个小问题,那就是当短信的长度超过运营商限制的长度的时候,短信发送不出去

修改代码如下:

package com.wuyudong.smssender;

import java.util.List;

import android.os.Bundle;
import android.app.Activity;
import android.telephony.SmsManager;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;

public class MainActivity extends Activity {

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

    public void send(View v) {
        EditText et_phone = (EditText) findViewById(R.id.et_phone);
        EditText et_content = (EditText) findViewById(R.id.et_content);

        String phone = et_phone.getText().toString();
        String content = et_content.getText().toString();

        // 获取短信管理器的对象
        SmsManager sm = SmsManager.getDefault();

        // 如果短信过长,分割短信,不能超过运营商限定的最长字符数
        List<String> smss = sm.divideMessage(content);

        for (String sms : smss) {
            // 发送短信
            // arg0:对方的号码
            // arg1:短信服务中心的号码,不要设置
            // arg2:短信内容
            sm.sendTextMessage(phone, null, sms, null, null);
        }
    }
}

搞定

1楼1445579000
你可以在添加一个提醒性,判断是否为空,还有最后要提醒一下用户发送成功,我用你的这个方法猛点发送手机瞬间接了n个短信,,,//判断是否为空,if(quot;quot;.equals(sms)||quot;quot;.equals(number)){,Toast.makeText(MainActivity.this, quot;手机号码或电话号码不能为空quot;, 1).show();