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. Android开发-API指南-设备兼容性

    Device Compatibility 英文原文:http://developer.android.com/guide/practices/compatibility.html 采集日期:2014- ...

  2. EXTJS 密码确认与验证

    extjs 框架是一个非常优秀的前端框架,提供了丰富的功能与炫丽的界面展示,在用 extjs 创建表单时,特别是在注册或修改密码的时候,要对密码进行确认,这里就密码确认一致性验证和大家分享自己的心得与 ...

  3. linux使用flock文件锁解决crontab冲突问题

    * * * * * flock -xn /dev/shm/redis.lock -c "/usr/local/bin/redis-server" 可以用flock命令,配合使用rs ...

  4. mysql中-e用法

    实际应用中,不仅可以先登陆mysql再使用,还可以在链接的时候进行sql操作,此时需要加参数-e 例: >mysql -hlocalhost -P8080 -uroot -p123456 -e' ...

  5. ndk-gdb 对java/native code联合调试(升级版)

    之前写过一篇 关于android native 开发,调试的文章(http://www.cnblogs.com/yaozhongxiao/archive/2012/03/13/2393959.html ...

  6. 华为OJ平台——整数的二进制中1的个数

    题目描述: 输入一个整数,求该整数的二进制表达中有多少个1.例如输入10,由于其二进制表示为1010,有两个1,因此输出2. 思路: 这是一道很基本的考查位运算的面试题.包括微软在内的很多公司都曾采用 ...

  7. js 打开PDF

    用插件打开PDF的一个超连接 但是谷歌了很久 ,发现了一个很好的方法 就是借google 来打开一个PDF ,而不下载PDF: _yourID.setAttribute('onclick'," ...

  8. JQ仿select框

    点击[cy_title]后弹出[cy_list]层,选中里面的元素把值赋给 [cy_title] 在[cy_list] 打开的时候,点击其他地方可以关闭: HTML: <div class=&q ...

  9. verilog实现奇数倍分频

    在学习FPGA的过程中,最简单最基本的实验应该就是分频器了, 同时分频器也是FPGA设计中使用频率非常高的基本设计之一, 尽管在芯片厂家提供的IDE中集成了锁相环IP, 如altera 的PLL,Xi ...

  10. Boost C++: 数据结构---tuple

    #include <boost/tuple/tuple.hpp> #include <boost/tuple/tuple_io.hpp> #include <boost/ ...