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步调试执行:

【源代码下载】http://www.code4apk.com/android-code/202

android发送短信样例的更多相关文章

  1. android 发送短信 怎样做到一条一条的发送,仅仅有在上一条发送成功之后才发送下一条短信

    android发送短信截获上一条发送是否成功,然后再来发送下一条短信 1.问题:在项目中遇到例如以下要求:待发短信有N条,实现一条一条的发送并在上一条短信发送成功之后再来发送下一条. for(int ...

  2. android 发送短信的两种方式,以及接收报告和发送报告

               android发送短信,以及接收报告和发送报告          android中发送短信其实有两种方式,这个和打电话类似,大家可以了解一下:    一.调起系统发短信功能    ...

  3. android发送短信验证码并自动获取验证码填充文本框

    android注册发送短信验证码并自动获取短信,截取数字验证码填充文本框. 一.接入短信平台 首先需要选择短信平台接入,这里使用的是榛子云短信平台(http://smsow.zhenzikj.com) ...

  4. 【Android】Android 发送短信和打电话的方法

    发送短信的方法 有两种方法可以实现发送短信,其一是使用intent-startActivity,URI数据格式为"smsto:num",调用的action为Intent.ACTIO ...

  5. Android 发送短信总结

    SMS涉及的主要类SmsManager 实现SMS主要用到SmsManager类,该类继承自java.lang.Object类,下面我们介绍一下该类的主要成员. 公有方法: ArrayList< ...

  6. 关于Android发送短信获取送达报告的问题

    最近公司开发一个项目,要求app能够发送短信并获取送达报告.这本不是一个什么难题,实现这一功能的代码一搜一大把,那么这么简单的一个问题,为什么我要在这里提出来呢?那是因为我在写代码的时候掉入了一个坑, ...

  7. Android发送短信核心代码

    核心代码:(1)SmsManager manager = SmsManager.getDefault(); //获得默认的消息管理器(2)ArrayList<String> list = ...

  8. Android发送短信

    // 发送短信 public void sendMsg(){ String content = edtSend.getText().toString(); SmsManager smsManager ...

  9. android发送短信代码(短信内容超长处理)

    一条短信只可容纳70个中文,所以当短信长度超过70个中文字符时程序就要特殊处理了. 有两种方式: 1.通过sendTextMessage()方法依次发送拆分后的短信,该方式有个弊端就是用户会分条收到短 ...

随机推荐

  1. “/”应用程序中的server错误

    前几天敲ASP.NET有关验证控件的样例的时候,出现了这个问题: 可是对于这个问题我们应该都不陌生,之前敲牛腩的时候也出现过,当时的解决方法是: 在web.config中找         <a ...

  2. centos+apache+mod_ssl

    参考网站:配置Apache建立openssl证书实现SSL访问:http://blog.51yip.com/apachenginx/958.html用ca工具生成证书的方法:http://hi.bai ...

  3. iOS 设置UILabel 的内边距

    iOS 设置UILabel 的内边距 - (void)drawTextInRect:(CGRect)rect { UIEdgeInsets insets = {, , , }; [super draw ...

  4. wepy - 与原生有什么不同(x.wpy)使用实例

    源码 <template> <view class='mark' wx:if="{{showMark}}"> <view animation=&quo ...

  5. js 暂时性死区

    1.概念 在代码块内,使用let.const命令声明变量之前,该变量都是不可用的.这在语法上,称为“暂时性死区”(temporal dead zone,简称 TDZ). 2.注意 “暂时性死区”也意味 ...

  6. jquery插件开发通用框架

    2017-07-24 更新:增加单例模式. jquery插件开发框架代码: /* * 插件编写说明: * 1.插件命名:jquery.[插件名].js,如jquery.plugin.js * 2.对象 ...

  7. spring mysql多数据源配置

    spring mysql多数据源配置 @Configuration public class QuartzConfig { @Autowired private AutowireJobFactory ...

  8. Redis学习(3)-redis启动

    前端启动 tomcat,redis,mysql的端口号: mysql 3306 tomcat 8088 redis 6379 一,启动redis服务: 例如当前位置在redis安装目录下面: 启动re ...

  9. iOS-APP启动页加载广告

    概述 加载广告页, 展现跳过按钮实现倒计时功能, 并判断广告页面是否更新. 详细 代码下载:http://www.demodashi.com/demo/10698.html 目前市场上很多APP(如淘 ...

  10. MYSQL查询一周内的数据(最近7天的)

    select * from wap_content where week(created_at) = week(now) 如果你要严格要求是某一年的,那可以这样 查询一天: select * from ...