Android--发送短信,并且通知发送方
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--发送短信,并且通知发送方的更多相关文章
- C# 通过串口发送短信
手机短信群发作为企业日常通知,公告,天气预报等信息的一个发布平台,在于成本低,操作方便等诸多特点,成为企业通讯之首选.本文介绍短信的编码方式,AT指令以及用C#实现串口通讯的方法. 前言目前,发送短信 ...
- WPF MVVM下做发送短信小按钮
最近做一个项目,因为涉及到注册,因此需要发送短信,一般发送短信都有一个倒计时的小按钮,因此,就做了一个,在此做个记录. 一.发送消息 没有调用公司的短信平台,只是模拟前台生成一串数字,将此串数字输出一 ...
- iOS摇一摇功能、震动功能、简单的摇动动画、生成二维码图片与发送短信等几个功能
有一个开锁的功能,具体的需求就类似于微信的"摇一摇"功能:摇动手机,手机震动,手机上的锁的图片摇动一下,然后发送开锁指令.需求简单,但用到了许多方面的知识. 1.摇一摇 相对这是最 ...
- iOS中发送短信/发送邮件的实现 韩俊强的博客
需要引入框架: MessageUI.framework 布局如下: 短信和邮件: #import "ViewController.h" #import <MessageUI/ ...
- 如何使用微信小程序云函数发送短信验证码
其实微信小程序前端和云端都是可以调用短信平台接口发送短信的,使用云端云函数的好处是无需配置域名,也没有个数限制. 本文使用的是榛子云短信平台(http://smsow.zhenzikj.com) ,S ...
- iOS几个功能:1.摇一摇;2.震动;3.简单的摇动动画;4.生成二维码图片;5.发送短信;6.播放网络音频等
有一个开锁的功能,具体的需求就类似于微信的“摇一摇”功能:摇动手机,手机震动,手机上的锁的图片摇动一下,然后发送开锁指令.需求简单,但用到了许多方面的知识. 1.摇一摇 相对这是最简单的功能了. 在v ...
- android: 接收和发送短信
8.2 接收和发送短信 收发短信应该是每个手机最基本的功能之一了,即使是许多年前的老手机也都会具备这 项功能,而 Android 作为出色的智能手机操作系统,自然也少不了在这方面的支持.每个 A ...
- Android学习笔记(二十二)——短信接收与发送
//此系列博文是<第一行Android代码>的学习笔记,如有错漏,欢迎指正! 当手机接收到一条短信的时候, 系统会发出一条值为 android.provider.Telephony.SMS ...
- java攻城师之路(Android篇)--搭建开发环境、拨打电话、发送短信、布局例子
一.搭建开发环境 1.所需资源 JDK6以上 Eclipse3.6以上 SDK17, 2.3.3 ADT17 2.安装注意事项 不要使用中文路径 如果模拟器默认路径包含中文, 可以设置android_ ...
- Android开发之发送短信
本实例通过SmsManager的sendTextMessage方法实现发送短信关于SmsManager的具体解释大家能够參照:Android开发之SmsManager具体解释 实例执行效果图: 程序代 ...
随机推荐
- SQL 表值函数
表值函数返回的是一张表. 情况:把传入的字符串按指定分隔符转换成数组 理解:把字符串打散,逐个插入表,这个表就是需要的数据 Create Function [dbo].[Split] ( ), ) ) ...
- Oracle 删表前验证表名是否存在并且删除
DECLARE num NUMBER; BEGIN SELECT COUNT(1) INTO num FROM USER_TABLES WHERE TABLE_NAME = UPPER('tableN ...
- Oracle之别名小结
今天在写一个简单的SQL语句并执行时抛出了如下图所示的一个错误提示信息! 恩,此异常信息很明显,在SQL语句中标示符的长度过长了,简短一些就应该没问题了,我查看了一下我的SQL语句发现是我的查询字段的 ...
- 学习记录 彻底搞清 C#中a++与++a的区别
首先 a++和++a 的定义:看个例子A: a=5; b=++a; // 相当于a=a+1;b=a; 结果是a=6,b=6B: a=5; b=a++; // 相当于b=a;a=a+1; 结果是a=6, ...
- Android基础总结(7)——异步消息处理
服务(Service)是Android中实现程序后台运行的解决方案,它非常适合用于去执行哪些不需要和用户交互而且还要长期运行的任务.服务的运行不依赖任何用户界面,即使当程序被切换到后台,或者用户打开了 ...
- VC 运行时库 /MD、/MDd 和 /MT、/MTd
这里总结下他们的区别,后面的那个'd'是代表DEBUG版本,没有'd'的就是RELEASE版本了. 首先说/MT /MT是 "multithread, static version ” 意思 ...
- ubuntu多网卡绑定
这是最近碰到这个问题,需要将两张网卡绑定,共用一个IP,实现冗余效果.实际上linux双网卡的绑定模式有7中,而在这里常用的是 active-backup linux有七种网卡绑定模式:0. roun ...
- python 多行匹配
content = ''' abcdefg hijklmn opq rst uvw xyz ''' r = re.compile('\S+cde\S+|\S+klm\S+|^xyz$', re.MUL ...
- HTML中,按钮button与submit区别是什么?
type=button 单纯是按钮功能:type=submit 是发送表单:既然是这样,那么能否全部用submit代替button? Sumit提交表单,Button需要绑定事件才可以用提交数据不可以 ...
- shell脚本定时操作数据库
一.数据库脚本(mysql.sh) Linux环境下,定时将数据库A的表格复制到数据库B中 #!/bin/bash mysql_host="127.0.0.1" mysql_use ...