Android--发送短信,并且通知发送方

时间:2021-12-22 08:57:42

1、发送短信涉及到权限,我们需要把权限加上

2、当我们发送短信时,不管发送是否成功,接收方是否接收到,系统都会发送广播

3、这时我们注册广播去接收一下就可以了

4、布局文件很简单,里面就两个EditText和一个button

下面上代码,里面有注释:

发送广播接收器:

 

package com.example.fanlei.cutnotedemo;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.SmsManager;
import android.widget.Toast;

/**
 * 监听短信是否已经发送成功
 */
public class MySmsSendReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals("SMS_SEND")){
            switch(getResultCode())
            {
                case Activity.RESULT_OK:
                    Toast.makeText(context,"发送成功",Toast.LENGTH_SHORT).show();
                    break;
                case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                    Toast.makeText(context,"发送失败",Toast.LENGTH_SHORT).show();
                    break;
                case SmsManager.RESULT_ERROR_NO_SERVICE:

                    break;
                case SmsManager.RESULT_ERROR_NULL_PDU:

                    break;
                case SmsManager.RESULT_ERROR_RADIO_OFF:

                    break;
            }
        }
    }
}

接收方的广播

package com.example.fanlei.cutnotedemo;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

/**
 * 监听对方是否发已经接收到短信
 */
public class MySmsDeliverReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals("SMS_DELIVER")){
            switch(getResultCode())
            {
                case Activity.RESULT_OK:
                    Toast.makeText(context,"对方已接收",Toast.LENGTH_SHORT).show();
                    break;
                case Activity.RESULT_CANCELED:

                    break;
            }
        }
    }
}

主函数

package com.example.fanlei.cutnotedemo;

import android.app.PendingIntent;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.telephony.SmsManager;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import java.util.List;


public class MainActivity extends ActionBarActivity {

    private EditText editText1,editText2;

    private MySmsSendReceiver mySmsSendReceiver;      //发送短信的广播
    private MySmsDeliverReceiver mySmsDeliverReceiver;//对方是否已经接收的广播

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mySmsSendReceiver = new MySmsSendReceiver();
        mySmsDeliverReceiver = new MySmsDeliverReceiver();

        editText1 = (EditText) findViewById(R.id.editText1);//手机号
        editText2 = (EditText) findViewById(R.id.editText2);//内容

        //初始化启动意图
        Intent sendIntent = new Intent();
        sendIntent.setAction("SMS_SEND");
        Intent deliverIntent = new Intent();
        deliverIntent.setAction("SMS_DELIVER");

        //满足条件后执行Intent
        final PendingIntent send = PendingIntent.getBroadcast(MainActivity.this,0 ,sendIntent,0);
        final PendingIntent deliver = PendingIntent.getBroadcast(MainActivity.this,0,deliverIntent,0);

        //发送短信按钮
        findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String phoneNumber = editText1.getText().toString();
                String content = editText2.getText().toString();
                if (phoneNumber.equals("")){
                    Toast.makeText(MainActivity.this,"手机号码为空",Toast.LENGTH_SHORT).show();
                }else {
                    //获得短信的管理者
                    SmsManager sm = SmsManager.getDefault();
                    //短信字数限制,如果>70,则需要拆分短信发送
                    if (content.length() > 70){
                        List<String> contents = sm.divideMessage(content);
                        for (String sms : contents){
                            sm.sendTextMessage(phoneNumber,null,sms,send,deliver);//发送短信
                        }
                    }else {
                        sm.sendTextMessage(phoneNumber,null,content,send,deliver);//发送短信
                    }

                }
            }
        });

        //注册广播
        registerReceiver(mySmsSendReceiver,new IntentFilter("SMS_SEND"));
        registerReceiver(mySmsDeliverReceiver,new IntentFilter("SMS_DELIVER"));

    }
}

布局文件

<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:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".MainActivity">
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/editText1"
        android:hint="手机号"
        android:inputType="phone"/>
    <EditText
        android:id="@+id/editText2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/editText1"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:hint="内容"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/button"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        android:text="发送短信" />

</RelativeLayout>

小伙伴们可以用10086进行测试