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进行测试

Android--发送短信,并且通知发送方的更多相关文章

  1. C# 通过串口发送短信

    手机短信群发作为企业日常通知,公告,天气预报等信息的一个发布平台,在于成本低,操作方便等诸多特点,成为企业通讯之首选.本文介绍短信的编码方式,AT指令以及用C#实现串口通讯的方法. 前言目前,发送短信 ...

  2. WPF MVVM下做发送短信小按钮

    最近做一个项目,因为涉及到注册,因此需要发送短信,一般发送短信都有一个倒计时的小按钮,因此,就做了一个,在此做个记录. 一.发送消息 没有调用公司的短信平台,只是模拟前台生成一串数字,将此串数字输出一 ...

  3. iOS摇一摇功能、震动功能、简单的摇动动画、生成二维码图片与发送短信等几个功能

    有一个开锁的功能,具体的需求就类似于微信的"摇一摇"功能:摇动手机,手机震动,手机上的锁的图片摇动一下,然后发送开锁指令.需求简单,但用到了许多方面的知识. 1.摇一摇 相对这是最 ...

  4. iOS中发送短信/发送邮件的实现 韩俊强的博客

    需要引入框架: MessageUI.framework 布局如下: 短信和邮件: #import "ViewController.h" #import <MessageUI/ ...

  5. 如何使用微信小程序云函数发送短信验证码

    其实微信小程序前端和云端都是可以调用短信平台接口发送短信的,使用云端云函数的好处是无需配置域名,也没有个数限制. 本文使用的是榛子云短信平台(http://smsow.zhenzikj.com) ,S ...

  6. iOS几个功能:1.摇一摇;2.震动;3.简单的摇动动画;4.生成二维码图片;5.发送短信;6.播放网络音频等

    有一个开锁的功能,具体的需求就类似于微信的“摇一摇”功能:摇动手机,手机震动,手机上的锁的图片摇动一下,然后发送开锁指令.需求简单,但用到了许多方面的知识. 1.摇一摇 相对这是最简单的功能了. 在v ...

  7. android: 接收和发送短信

    8.2    接收和发送短信 收发短信应该是每个手机最基本的功能之一了,即使是许多年前的老手机也都会具备这 项功能,而 Android 作为出色的智能手机操作系统,自然也少不了在这方面的支持.每个 A ...

  8. Android学习笔记(二十二)——短信接收与发送

    //此系列博文是<第一行Android代码>的学习笔记,如有错漏,欢迎指正! 当手机接收到一条短信的时候, 系统会发出一条值为 android.provider.Telephony.SMS ...

  9. java攻城师之路(Android篇)--搭建开发环境、拨打电话、发送短信、布局例子

    一.搭建开发环境 1.所需资源 JDK6以上 Eclipse3.6以上 SDK17, 2.3.3 ADT17 2.安装注意事项 不要使用中文路径 如果模拟器默认路径包含中文, 可以设置android_ ...

  10. Android开发之发送短信

    本实例通过SmsManager的sendTextMessage方法实现发送短信关于SmsManager的具体解释大家能够參照:Android开发之SmsManager具体解释 实例执行效果图: 程序代 ...

随机推荐

  1. BZOJ1996 合唱队 区间DP

    OJ地址:http://www.lydsy.com/JudgeOnline/problem.php?id=1996 设dp(i,j,k)代表在理想结果中[i,j]段最后添加的是i或j(k=0or1) ...

  2. ASPxCallback控件

    ASPxCallback控件简单来的来说是一个数据回调控件,即不刷新事个页面来展现数据,主要是通过注册客户端事件与服务器端的事件来相互通信完成任务. 如何使用ASPXCallback: 向页面添加Ca ...

  3. asp.net(c#)修改时的赋值操作

    赋值操作方法(将信息显示至文本框中): public void Show_Infobase(int _peoid) { DataSet ds = new DataSet(); ds = platbll ...

  4. TortoiseSVN文件夹图标不显示的解决方法

    是否遇到过TortoiseSVN安装好后,发现文件夹的图标还是Windows默认的图标? 下面通过简单几步解决图标不显示的问题. 1/6 "Win + R"打开运行框,输入&quo ...

  5. MYSQL--事务处理

    1.用begin,rollback,commit来实现 begin 开始一个事务 rollback 事务回滚 commit  事务确认 2.直接用set来改变mysql的自动提交模式 MYSQL默认是 ...

  6. JS常用的设计模式(17)—— 状态模式

    状态模式主要可以用于这种场景 1 一个对象的行为取决于它的状态 2 一个操作中含有庞大的条件分支语句 回想下街头霸王的游戏. 隆有走动,攻击,防御,跌倒,跳跃等等多种状态,而这些状态之间既有联系又互相 ...

  7. console数据

  8. Java 单测 回滚

    Java 在单测的时候 需要做回滚 设置如下: 需要添加以下 注解 在类上 defaultRollback = true : 为 默认全部回滚 defaultRollback = false : 为 ...

  9. ruby中symbol

    Symbol 是什么 Ruby 是一个强大的面向对象脚本语言(本文所用 Ruby 版本为1.8.6),在 Ruby 中 Symbol 表示“名字”,比如字符串的名字,标识符的名字. 创建一个 Symb ...

  10. Count and Say [LeetCode 38]

    1- 问题描述 The count-and-say sequence is the sequence of integers beginning as follows: 1, 11, 21, 1211 ...