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()方法依次发送拆分后的短信,该方式有个弊端就是用户会分条收到短 ...
随机推荐
- vb中adOpenKeyset, adLockOptimistic
adOpenStatic 向前游标adOpenKeyset 键集游标adLockOptimistic设置窗口为固定的大小 附带一个小资料: ------------------------------ ...
- SQL入门教程
SQL SELECT DISTINCT 语句 在表中,可能会包含重复值.这并不成问题,不过,有时您也许希望仅仅列出不同(distinct)的值. 关键词 DISTINCT 用于返回唯一不同的值. 语法 ...
- PHP MySQL 连接数据库
PHP连接MySQL的小实例 <?php /*时间:2014-09-14 *作者:葛崇 *功能:PHP连接MySQL小实例 * */ /* SQL 脚本.直接贴到命令行运行. DROP ...
- STS 自动生成 getter 和 setter
1.Source -- Generate Getters and Setters
- C++ union使用注意
union在我们敲代码的时候的使用概率远远小于struct.所以我们常常不太关心她.就知道他是使用内存复用技术.同一个时刻,他仅仅能存在一个成员的值. C中,我们在union中能够包括struct的, ...
- CF MVC3此操作要求连接到 'master' 数据库。无法创建与 'master' 数据库之间的连接,这是因为已打开原始数据库连接,并且已从连接字符串中删除凭据。请提供未打开的连接 解决方法
<add name="ProwebEntities" connectionString ="Data Source=.;Integrated Security=tr ...
- idea 配置多个tomcat
1.打开设置窗口 File-->Settings 2.启用tomcat 3.run-->edit configuration: 3.点击左上角+号-->找到Tomcat Serv ...
- plsql 表数据中文显示乱码(配置环境变量)
plsql 表数据中文显示乱码(配置环境变量) CreateTime--2018年4月23日19:32:37 Author:Marydon 1.情景再现 2.解决方案 配置环境变量 变量名:NLS ...
- 【BIEE】清除缓存
清除缓存步骤: 1.管理→管理会话→关闭所有游标 2.管理→发出SQL语句:CALL SAPURGEALLCACHE(); 点击发送SQL语句后
- 使用openssl进行证书格式转换
各类证书由于存储的内容不同(如是否包含公钥/私钥是否加密存储/单一证书或多证书等).采用编 码不同(DER/BASE64).标准不同(如PEM/PKCS),所以尽管X.509标准规定了证书内容规范,但 ...