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()方法依次发送拆分后的短信,该方式有个弊端就是用户会分条收到短 ...
随机推荐
- 重命名IDEA14项目名
Project Settings / Project ->">工程结构(ctrl-alt-shift-s)->设置->项目/项目Project name: 请注意,这 ...
- (LeetCode 83)Remove Duplicates from Sorted Lists
Given a sorted linked list, delete all duplicates such that each element appear only once. For examp ...
- Succession
Description The king in Utopia has died without an heir. Now several nobles in the country claim the ...
- 查找树ADT——二叉查找树
二叉查找树:对于树中的每个节点X,它的左子数种所有关键字值小于X的关键字,而它的右子树种所有关键字值大于X的关键字值. /* 二叉查找树声明 */ #ifndef _TREE_H struct Tre ...
- VB 中定义FileSystemObject对象,要先添加对象
存取文件的方法有很多种,可以使用上述VB提供的函数,使用Windows API函数等等,但是最简单的方法是使用FileSystemObject对象. 1.使用FileSystemObject对象 F ...
- Spring <context:annotation-config/> 说明
在基于主机方式配置Spring的配置文件中,你可能会见到<context:annotation-config/>这样一条配置,他的作用是式地向 Spring 容器注册AutowiredAn ...
- Ubuntu系统安装VMware Tools的简单方法
不少网友反映在VMWare虚拟机下安装Ubuntu系统后无法安装VMware Tools,这里给出一个简单方法,只需要几步即可解决. 第一步:进入系统后,点击虚拟机上的安装vmware tools,回 ...
- Python之L.reverse()和L.sort()
# -*- coding: utf-8 -*- #python 27 #xiaodeng #Python之L.reverse()和L.sort() #http://python.jobbole.com ...
- keychain实现ssh对秘钥免登陆免输入密码
Linux同一网段实现密码认证,管理. 项目:https://github.com/funtoo/keychain 01.生成秘钥 ssh-keygen -t rsa # -t rsa | ds ...
- springmvc最优化
java代码 package com.tgb.web.controller.annotation; import javax.servlet.http.HttpServletRequest; impo ...