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. MSP430F149学习之路——捕获/比较模式

    1.捕获模式 #include <msp430x14x.h> unsigned ,last1=; unsigned ,j=; void mian(void) { WDTCTL = WDTP ...

  2. Messenger 弹窗的使用

    关于Messenger 弹窗的文档及详细的说明请参考 Messenger官网,这时只介绍 Messenger 弹窗的使用  messenger依赖与jquery和Backbone.js,可以和  Bo ...

  3. Devexpress中WebChartControl控件柱状统计图的做法(数据为调用存储过程)

    //前台控件代码:WebChartControl控件: <%-- 月采购量统计--%> <dxchartsui:WebChartControl ID="WebChartCo ...

  4. Ajax-(get/post/jQuery方式请求)

    < !DOCTYPE html > < html xmlns = "http://www.w3.org/1999/xhtml" > < head &g ...

  5. Win8系统安装NET Framework 3.5的方法

    1)找到下载过的Win8系统盘,解压到某个目录下,例如 F:, 找到解压的系统盘目录中"sources\sxs"文件夹 2)以管理员身份运行cmd命令,执行下面的命令: dism. ...

  6. 凡聊过必留下痕迹-破解加密的WeChat数据库

    有个朋友上门寻求协助,带着她朋友的朋友的手机,说是手机硬件有问题,想把手机内的资料都备份出来,尤其是WeChat的聊天内容…我跟她说,那iTool等工具不就可以帮上忙了吗?没想到她早就试过了, 说iT ...

  7. JSF的ui标签

    在使用自己的tag时,首先需要在web.xml里面进行注册,注册方式是在web.xml开头加上:  <context-param>        <param-name>fac ...

  8. 缓存一致性(Cache Coherency)入门

    作者: Fabian “ryg” Giesen  来源: infoq 参考原文:http://fgiesen.wordpress.com/2014/07/07/cache-coherency/ 本文是 ...

  9. SMTP sendMail 失败解决办法

    If you are seeing messages like this in your message log when running a process through the process ...

  10. 解决DataGridView在多线程中无法显示滚动条的问题

    在多线程中对DataGridView指定 DataSource 来填充数据,更新数据的时候,会导致DataGridView出现假死,显示错误或者滚动条无法显示的问题,在保证了DataGridView的 ...