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. 使用nodejs 访问mongodb

    我使用了 express 框架 目录结构 db.js 文件 function connectionDB(hostname, port) { //注释地方暂时没有使用.是把官方代码照抄下来 // var ...

  2. C和C++中include 搜索路径的一般形式以及gcc搜索头文件的路径

    C和C++中include 搜索路径的一般形式 对于include 搜索的路径: C中可以通过 #include <stdio.h> 和 #include "stidio.h&q ...

  3. swpuctf-web部分学习总结

    1.用优惠码 买个 X ? (1)第一步: 这道题第一步主要知道利用php的随机种子数泄露以后就可以利用该种子数来预测序列,而在题目中会返回15位的优惠码,但是必须要24位的优惠码,因此要根据15位的 ...

  4. 【iptables】规则的试验和查看

    1.存在桥接,查看桥接访客网络规则 ebtables -L 可以看到对不同的链的策略

  5. (四)selenium打开和关闭浏览器

    一.Selenium简介 Selenium3.0主要变更特性: ①移除seleniumRC ②FireFox和Safari推出了自己的driver(geckodriver 和 Safaridriver ...

  6. 强哥的分享--如何使用Spring Boot做一个邮件系统

    http://springboot.fun/ actuator是单机.集群环境下要使用Spring Boot Admin将各个单机的actuator集成越来 mvn clean package -Dm ...

  7. sqlServer游标的使用

    USE [PatPD1]GO/****** Object:  UserDefinedFunction [dbo].[fun_GetConditionInner]    Script Date: 201 ...

  8. Git读档

    $ git config --global user.name "meng kai" $ git config --global user.email 363255751@qq.c ...

  9. HDU 5014 异或之和

    http://acm.hust.edu.cn/vjudge/contest/122814#problem/H 这道题就是求异或之和 知识点: a^b = c 等价于 b^c =a 和 a^c = b ...

  10. Java入门到精通——调错篇之Eclipse Java compiler level dose not match the version of the installed Java project

    一.错误现象. java项目显示红色,并且类中引用包中会报红色错误,在Eclipse下面显示下面错误提示如图: 二.错误原因. 通过字面意思一看就很明白java的版本不对. 三.解决办法. 3.1右键 ...