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 四大组件之 Service(一)
Service是Android中四大组件之一,在Android开发中起到非常重要的作用,它运行在后台,不与用户进行交互. 1.Service的继承关系: java.lang.Object → andr ...
- C#.Net中操作XML方法一
我们知道XML是一种可标记性的语言,用来标记数据.定义数据类型,是一种执行用户对自己的标记语言进行定义的源语言.由于结构好.而且easy理解,就好比一棵树,层次关系分明,因此也经常把一些数据存储到XM ...
- Closing a window
The obvious way to how to close a window is to click on the x mark on the titlebar. In the next exam ...
- sql distinct 不能比较或排序 text、ntext 和 image 数据类型,除非使用 IS NULL 或 LIKE 运算符
有个文章的表内容是列项,类型是text 我查询的是内容相同的文章,把其中的一个删除 select 内容 from 文章 group by 内容 having count(*)>1 查询ID和题目 ...
- 网页中的图片查看器viewjs使用
需求分析: 对于网页中的图片进行连续放大(便于用户清晰查看内容).缩小,旋转等操作,可以使用viewjs图片查看器插件实现. viewjs官方网址:https://github.com/fengyua ...
- FIS3常用配置
FIS3常用配置: //作用:启用 fis-spriter-csssprites 插件,便于对png图片进行雪碧图合并 fis.match('::package', { spriter: fis.pl ...
- Golang配置文件解析-oozgconf
代码地址如下:http://www.demodashi.com/demo/14411.html 简介 oozgconf基于Golang开发,用于项目中配置文件的读取以及加载,是一个轻量级的配置文件工具 ...
- 微信小程序项目实战之天气预报
概述 微信小程序项目实战之天气预报 详细 代码下载:http://www.demodashi.com/demo/10634.html 一.准备工作 1.注册微信小程序 2.注册和风天气账号 3.注册百 ...
- 使用c:forEach 控制5个换行
今天做项目的时候碰到一个问题,我须要显示不确定数目的图片在网页上(图片是从数据库查出来的),用的是<c:forEach>循环取值的.就须要做成一行显示固定个数的图片.代码例如以下(我这里是 ...
- Web Service——CXF
1. 什么是CXF Apache CXF = Celtix + Xfire,开始叫 Apache CeltiXfire,后来更名为 Apache CXF 了,以下简称为 CXF.Apache CXF ...