android发送短信样例
Android应用开发中我们经常须要发送手机短信。这对于android平台来说,是最简单只是的功能了,无需太多代码,也无需自己定义代码,仅仅须要调用android提供的消息管理类SmsManager就能够了。
【源代码下载】http://www.code4apk.com/android-code/202
核心就是使用SmsManager的sendTextMessage方法加上PendingIntent跳转。
核心代码例如以下:
SmsManager sms=SmsManager.getDefault(); PendingIntent intent=PendingIntent.getBroadcast(MainActivtiy.this,0, new Intent(), 0); sms.sendTextMessage(phone.getText().toString(), null, text.getText().toString(), intent, null);
以下一起来实现这个功能:
第1步:新建一个activity :MainActivtiy
import android.app.Activity; import android.app.PendingIntent; import android.content.Intent; import android.os.Bundle; import android.telephony.SmsManager; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class MainActivtiy extends Activity { EditText text; EditText phone; Button send; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); send=(Button)findViewById(R.id.send); text=( EditText)findViewById(R.id.text); phone=( EditText)findViewById(R.id.phone); send.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { SmsManager sms=SmsManager.getDefault(); PendingIntent intent=PendingIntent.getBroadcast(MainActivtiy.this,0, new Intent(), 0); sms.sendTextMessage(phone.getText().toString(), null, text.getText().toString(), intent, null); Toast.makeText( MainActivtiy.this, "发送成功.....", Toast.LENGTH_LONG).show(); }
});
}
}
第2步:改动配置文件:main.xml
<? xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <EditText android:id="@+id/phone" android:layout_width="fill_parent" android:layout_height="wrap_content" android:hint="请输入电话号码" android:inputType="phone" android:text="" > </EditText> <EditText android:id="@+id/text" android:inputType="text" android:hint="请输入消息" android:layout_width="fill_parent" android:layout_height="wrap_content" > </EditText> <Button android:id="@+id/send" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="发送消息" > </Button> </LinearLayout>
第3步:在配置文件AndroidManifest.xml中加入发送短信支持
<uses-permission android:name="android.permission.SEND_SMS"/>
第4步调试执行:
android发送短信样例的更多相关文章
- android 发送短信 怎样做到一条一条的发送,仅仅有在上一条发送成功之后才发送下一条短信
android发送短信截获上一条发送是否成功,然后再来发送下一条短信 1.问题:在项目中遇到例如以下要求:待发短信有N条,实现一条一条的发送并在上一条短信发送成功之后再来发送下一条. for(int ...
- android 发送短信的两种方式,以及接收报告和发送报告
android发送短信,以及接收报告和发送报告 android中发送短信其实有两种方式,这个和打电话类似,大家可以了解一下: 一.调起系统发短信功能 ...
- android发送短信验证码并自动获取验证码填充文本框
android注册发送短信验证码并自动获取短信,截取数字验证码填充文本框. 一.接入短信平台 首先需要选择短信平台接入,这里使用的是榛子云短信平台(http://smsow.zhenzikj.com) ...
- 【Android】Android 发送短信和打电话的方法
发送短信的方法 有两种方法可以实现发送短信,其一是使用intent-startActivity,URI数据格式为"smsto:num",调用的action为Intent.ACTIO ...
- Android 发送短信总结
SMS涉及的主要类SmsManager 实现SMS主要用到SmsManager类,该类继承自java.lang.Object类,下面我们介绍一下该类的主要成员. 公有方法: ArrayList< ...
- 关于Android发送短信获取送达报告的问题
最近公司开发一个项目,要求app能够发送短信并获取送达报告.这本不是一个什么难题,实现这一功能的代码一搜一大把,那么这么简单的一个问题,为什么我要在这里提出来呢?那是因为我在写代码的时候掉入了一个坑, ...
- Android发送短信核心代码
核心代码:(1)SmsManager manager = SmsManager.getDefault(); //获得默认的消息管理器(2)ArrayList<String> list = ...
- Android发送短信
// 发送短信 public void sendMsg(){ String content = edtSend.getText().toString(); SmsManager smsManager ...
- android发送短信代码(短信内容超长处理)
一条短信只可容纳70个中文,所以当短信长度超过70个中文字符时程序就要特殊处理了. 有两种方式: 1.通过sendTextMessage()方法依次发送拆分后的短信,该方式有个弊端就是用户会分条收到短 ...
随机推荐
- Android Studio中安装Genymotion模拟器
Genymotion的安装: Genymotion无疑是目前最快最好用的模拟器.官网下载地址:https://www.genymotion.com/ 进到官网却找不到免费下载地址了,都需要money, ...
- 腾讯云兑现存储获取临时授权C#版
腾讯官方没有提供C#版的,没办法自己根据java版改写了一个,这里面的坑花了我20多个小时,所以记录下 <%@ WebHandler Language="C#" Class= ...
- wepy - Cannot read property 'Promise' of undefined
正当我们准备试探示例时,突然.... 造成这个错误有两个原因 (wepy以前的版本默认启动了Promise,自1.4.x以后需要手动开启) 1.未下载Promise 详情见启用文档:Promise ...
- 谈一谈chrome浏览器使用
F3或Ctrl+F5: 查找本网页里面的内容,匹配到即可高亮. F6:回到地址栏 Ctrl+J:显示下载内容 Ctrl+N: 新建一个标签页 F10:选项 F11:显示全屏 F12:调试网页 大家有什 ...
- 文件的上传Commons FileUpload(web基础学习笔记十六)
一.表单设置 <form action="<%=request.getContextPath()%>/jsp/admin/doAdd.jsp" enctype=& ...
- linux下查看线程数的几种方法
1. cat /proc/${pid}/status 2.pstree -p ${pid} 3.top -p ${pid} 再按H 或者直接输入 top -bH -d 3 -p ${pid} t ...
- JSTL fmt:formatNumber 数字、货币格式化
<fmt:formatNumber value="12.34" pattern="#0.00" /> 12.34 保留小数点后两位数 <fmt ...
- python之函数用法round()
# -*- coding: utf-8 -*- #python 27 #xiaodeng #python之函数用法round() #http://www.cnblogs.com/hongfei/p/3 ...
- 21、java中和日期相关的类
一.Data及其常用API 1.简介 Java中的时间使用标准类库的java.util.Date,其表示特定的瞬间,精确到毫秒.是用距离一个固定时间点的毫秒数(可正可负,long类型)表达一个特定的时 ...
- maven profiles、filters、resources学习笔记 及 常用 plugin demo
这里只记了学习以下博客后,自己做的一个总结. 来源:http://blog.csdn.net/fengchao2016/article/details/72726101 profiles定义了一些不同 ...