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 {@Overridepublic 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 {@Overridepublic 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;//对方是否已经接收的广播
@Overrideprotected 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");//满足条件后执行Intentfinal 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() {@Overridepublic 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"><EditTextandroid:layout_width="match_parent"android:layout_height="wrap_content"android:id="@+id/editText1"android:hint="手机号"android:inputType="phone"/><EditTextandroid: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="内容"/><Buttonandroid: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进行测试

转载于:https://www.cnblogs.com/819158327fan/p/4932788.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/404790.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

gcc编译选项-Os的用法

From: http://hi.baidu.com/caosicong/item/e61fa700581980d11ff0469c **********************gcc/egcs 的主要选项********* gcc 命令的常用选项 选项 解释 -ansi 只支持 ANSI 标准的 C 语法。这一选项将禁止 GNU C 的某些特色&#xff0c; 例如 asm 或 typeof 关键词。…

Sharepoint2007个人网站不能同步域信息的处理方法

如果在AD上改变的帐户的信息&#xff0c;比如邮箱地址&#xff0c;可能sharepoint不能自动更新。 sharepoint管理中心&#xff0d;共享服务管理&#xff0d;sharedServices1&#xff0d;用户配置文件和属性 在些处可导入&#xff0c;编辑帐户信息。 当然也可以直接开始完全导入…

相干采样要求

引自&#xff1a;使用AD7779 24位同步采样Σ-Δ型ADC实现电能质量测量的相干采样 http://www.chinaaet.com/tech/solution/3000020192 0、相干采样要求 要使电能计量和电能质量设备的谐波数据和计量参数获得要求的精度&#xff0c;应确保ADC采样速率和电力线频率之间的相干性…

[react] 在React中组件和元素有什么区别?

[react] 在React中组件和元素有什么区别&#xff1f; 组件首字母大写 组件是由元素构成的。元素数据结构是普通对象&#xff0c;而组件数据结构是类或纯函数 我是歌谣&#xff0c;欢迎和大家一起交流前后端知识。放弃很容易&#xff0c; 但坚持一定很酷。欢迎大家一起讨论 主…

默认库“LIBCMTD”与其他库的使用冲突;请使用 /NODEFAULTLIB:library

From: http://blog.csdn.net/pgmsoul/article/details/4203941 以前经常遇到这个警告信息&#xff0c;因为运行并没有什么问题&#xff0c;所以也没深究。但是耿耿于怀那个“ 0 个错误&#xff0c;0 个警告”的成功提示&#xff0c;在网上搜了一下。原来问题出在默认库的引用选…

ASP.NET和PHP全面对比

谁是速度之王&#xff1f; 刚刚在9月编程语言排行榜上取得历史性突破的PHP在Web开发领域最到的对手可能就是基于微软.NET技术的ASP.NET。近日&#xff0c;微软的 Joe Stagner在博客上发表了一系列文章比较了PHP和ASP.NET性能方面的文章&#xff0c;引起了来自双方程序员的大量回…

40个漂亮的单页网站设计案例(上篇)

有很多人喜欢单页网站&#xff0c;这种网站只有一个页面&#xff0c;设计都很精美&#xff0c;通过锚记进行平滑的导航。不过&#xff0c;并不是每个网站都适合做成单页&#xff0c;一般都是内容比较少而且将来内容也不怎么增加的情况才适合这样做。如果你打算做一个这样的网站…

电能计量常用架构

电能计量仪表一般采取一下三种方案&#xff1a; 单芯片SOCADCDSPMCU高精度专用计量芯片&#xff0b;MCU 第一种方案降低了仪表成本&#xff0c;但开发难度大且后期维护成本高&#xff1b;第二种方案测量精度高&#xff0c;但电路相对复杂且成本高。而第三种方案测量精度较高、…

jsoncpp之初体验

参考文章 http://blog.csdn.net/JoeBlackzqq/article/details/9043315 , 我将里面的代码做在了一个工程亲自体验了下。 1. 下载jsoncpp源码&#xff0c; 地址: http://nchc.dl.sourceforge.net/project/jsoncpp/jsoncpp/0.5.0/jsoncpp-src-0.5.0.tar.gz 2. 编译json&#xff0…

[react] React怎么判断什么时候重新渲染组件呢

[react] React怎么判断什么时候重新渲染组件呢 componentWillReceiveProps 个人简介 我是歌谣&#xff0c;欢迎和大家一起交流前后端知识。放弃很容易&#xff0c; 但坚持一定很酷。欢迎大家一起讨论 主目录 与歌谣一起通关前端面试题

Swift入门 新浪微博

##新浪微博 - storyboard 代码 - 项目主体框架: - 主视图控制器 UITabbarController - 包含四个UINavigationController,分别是首页 消息 发现 我 - 特殊之处,UITabbarController中间的加号 - 配置源代码管理工具 SVN or Git or OSChina - 将项目部署到git.oschina.net - 注册…

让Windwos Server 2008 R2 SP1的FTP真正能访问

今天把FTP配置好后&#xff0c;在本机上匿名访问没有问题了&#xff0c;以为万事大吉&#xff0c;于是把需要的文件放入后&#xff0c;到另一台机子上去试试&#xff0c;结果发现出现下面的画面。访问不通&#xff01;什么原因&#xff1f;于是把防火墙完全关闭了&#xff0c;发…

几款用于电能质量测量的芯片

1、ATT7022B ATT7022B 是专用的智能电表计量芯片&#xff0c;ATT7022B 包含六路二阶 sigma-delta ADC、电能质量参数数字信号处理等电路。ATT7022B 可以测量系统的电压、电流、频率、有功功率、无功功率、有功能量、无功能量等参数。ATT7022B 通过 SPI 通信接口进行数据通信。…

[react] 在React中声明组件时组件名的第一个字母必须是大写吗?为什么?

[react] 在React中声明组件时组件名的第一个字母必须是大写吗&#xff1f;为什么&#xff1f; 必须&#xff0c;React根据首字母是否大写来区分react组件还是dom元素 个人简介 我是歌谣&#xff0c;欢迎和大家一起交流前后端知识。放弃很容易&#xff0c; 但坚持一定很酷。欢…

将Application按钮从任务栏中去掉

SetWindowLong(Application.Handle, GWL_EXSTYLE, WS_EX_TOOLWINDOW);转载于:https://www.cnblogs.com/liaj/p/4943352.html

我能够为大家提供什么内容呢?

我能够为大家提供什么内容呢&#xff1f; 这真是一个让我很伤脑筋的问题。在这个内容为王的时代&#xff0c;即使是写自己的博客&#xff0c;如果没有在其中加入好的内容&#xff0c;那还不如不写来得省事&#xff0c;又不占用网站存储空间。 这就好比一个人占着厕所又不拉一个…

Qt 生成bin文件

QFile file("flash_data.bin"); //定义一个文件 file.open(QIODevice::WriteOnly); //打开文件 QDataStream ds(&file); //定义一个数据流 for (int idx 0; idx < 8192; i){ds << (uint8_t)data[i]; //把数组中的数据写到数据…

[react] 在React中组件的state和setState有什么区别?

[react] 在React中组件的state和setState有什么区别&#xff1f; state&#xff1a;初始化状态 setState:对状态进行更新 个人简介 我是歌谣&#xff0c;欢迎和大家一起交流前后端知识。放弃很容易&#xff0c; 但坚持一定很酷。欢迎大家一起讨论 主目录 与歌谣一起通关前…

LeetCode 79. Word Search

原题链接在这里&#xff1a;https://leetcode.com/problems/word-search/ 题目&#xff1a; Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from letters of sequentially adjacent cell, where "adjacent" cells…