1、首先需要在AndroidManifest.xml文件中加入发送短信的权限

<uses-permission android:name="android.permission.SEND_SMS"/>

2、使用相对布局方式进行布局

  hint:表示编辑框的提示信息

  layout_below:在那个视图的下方

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/tvPhone"
android:id="@+id/tv1" /> <EditText
android:id="@+id/editPh"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/tv1"
android:layout_below="@+id/tv1"
android:hint="@string/phoneMsg"
android:ems="10" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editPh"
android:layout_below="@+id/editPh"
android:text="@string/tvSMS" />
<EditText
android:id="@+id/etSMS"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:ems="10" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/etSMS"
android:layout_below="@+id/etSMS"
android:text="@string/send" />
</RelativeLayout>

3、string的资源文件

<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">SMS</string>
<string name="action_settings">Settings</string>
<string name="tvPhone">请输入手机号</string>
<string name="tvSMS">请输入短信内容</string>
<string name="success">发送成功</string>
<string name="fail">发送失败</string>
<string name="phoneMsg">Please a phone number</string>
<string name="send">发送</string>
</resources>

4、activity类的实现

public class MainActivity extends Activity {
private EditText etPhone;
private EditText etSMS;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etPhone = (EditText)this.findViewById(R.id.editPh);//号码
etSMS = (EditText)this.findViewById(R.id.etSMS);//短信内容
Button btnSend = (Button)this.findViewById(R.id.button1);
//注册事件
btnSend.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
String phone = etPhone.getText().toString();
String sms = etSMS.getText().toString();
SmsManager manager = SmsManager.getDefault();
//如果短信太长,就分割开来在进行发送
ArrayList<String> msgs = manager.divideMessage(sms);
for (String msg : msgs) {
manager.sendTextMessage(phone, null, msg, null, null);
}
//参数分别表示上下文对象,显示消息,停留时间长短
Toast.makeText(MainActivity.this, R.string.success, Toast.LENGTH_LONG).show();
}
}); } @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
} }

Android学习笔记_2_发送短信的更多相关文章

  1. Android学习--写一个发送短信的apk,注意布局文件的处理过程!!!

    刚开始写Android程序如图发现使用了findViewById方法之后输出的话居然是null(空指针错误),也就是说这个方法没有成功.网上说这样写是在activity_main .xml去找这个ID ...

  2. Android(java)学习笔记86:案例短信发送器

    1.一般我们第一步都是先创建这个main.xml布局文件,这是良好的习惯: <?xml version="1.0" encoding="utf-8"?&g ...

  3. Android(java)学习笔记69:短信发送器

    1. 一般我们第一步都是先创建这个main.xml布局文件,这是良好的习惯: <?xml version="1.0" encoding="utf-8"?& ...

  4. 【Android学习】调用系统短信、电话

    今天全心投入Android学习已经有一段时间了,从起初的啥也不懂,到现在半知半解状态,随笔记录些简单且常用的系统功能代码. 调用Android系统短信,其实调用短信非常简单,一个方法就可以搞定.我们可 ...

  5. android 中调用接口发送短信

    android中可以通过两种方式发送短信 第一:调用系统短信接口直接发送短信:主要代码如下: //直接调用短信接口发短信 SmsManager smsManager = SmsManager.getD ...

  6. Android软件开发之发送短信与系统短信库解析

    今天我和同学们讨论一下Android平台下如何调用系统方法发送短信.接收短信.系统的短信库相关的问题.进入正题,我们先使用Eclipse工具模拟给自己的模拟器发送一条短信.在Eclipse下打开DDM ...

  7. 【Android】Android6.0发送短信Demo

    整理一下使用SmsManager类发送短信的方法. https://developer.android.com/reference/android/telephony/SmsManager.html ...

  8. phoneGap的Android下编写phonegap 发送短信插件

    一.前端代码的编写 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> < ...

  9. [APP] Android 开发笔记 006-使用短信验证SDK进行短信验证

    1. 下载SDK (http://www.mob.com/#/download) SMS For Android Studio: http://www.mob.com/download/sms/and ...

随机推荐

  1. div内容溢出

    前几天遇到一个问题,代码是这样一个层次: <div class="province"> <ul> <li>1</li> <li ...

  2. Thrift笔记(二)--Thrift框架分层设计

    thrift架构设计使用了分层设计,类似TCP/IP分层,上次使用下层提供的服务.分层设计在计算机中是一个常用的设计,上层和下层定义好接口或者说协议,下层实现可以随意更换,只要实现好定义的接口和协议就 ...

  3. python数据类型(数字\字符串\列表)

    一.基本数据类型——数字 1.布尔型 bool型只有两个值:True和False 之所以将bool值归类为数字,是因为我们也习惯用1表示True,0表示False. (1)布尔值是False的各种情况 ...

  4. JS基础学习——作用域

    JS基础学习--作用域 什么是作用域 变量的作用域就是变量能被访问到的代码范围,比如在下面的这个js代码中,变量a的作用域就是函数foo,因此在全局作用域内的console.log(a)语句不能访问到 ...

  5. HTML <frameset> 标签

    <frameset></frameset>:框架标签,可以将页面分割,被frameset标签分割的页面,不允许使用body标签;frameset标签页面内只能出现framese ...

  6. Steps of source code change to executable application

    程序运行的整个过程,学习一下 源代码 (source code) → 预处理器 (preprocessor) → 编译器 (compiler) → 汇编程序 (assembler) → 目标代码 (o ...

  7. 使用putty远程登录Ubuntu时,报Network error:Connection refused错误

    putty远程登录Ubuntu,弹出Network error:Connection refused的错误提示框,就是因为Ubuuntu没有安装ssh服务.执行命令: sudo apt-get ins ...

  8. input file 类型为excel表格

    以下为react写法,可自行改为html的 <div className="flag-tip"> 请上传excel表格, 后缀名为.csv, .xls, .xlsx的都 ...

  9. Spyder更改默认工作路径已经文件路径

    打开spyder,选择菜单栏中的Tools--->Preferences--->Current working directory   然后选择最下面的单选按钮The following ...

  10. DataBase Migration 使用笔记

    Add-Migration 新建数据库迁移版本 Update-Database -Verbose 更新到数据库(-Verbose 显示详细信息) Update-Database –TargetMigr ...