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. 【Linux相识相知】计算机的组成、linux发行版和哲学思想、基础命令和目录结构(FHS)

    从今天开始,Frank将开始在博客上记录自己学习linux的点点滴滴,F初来乍到,还望各位大佬多多指教.本次博客的主要内容如下: 计算机基础:简要的描述了计算机的组成及其功能: linux初识:介绍了 ...

  2. Unity3D第二课之通过键盘、鼠标移动物体

    public class xuanzhuan : MonoBehaviour { //平移速度变量 public float MoveSpeed;// Use this for initializat ...

  3. SpringSecurity 3.2入门(1)框架介绍

    关于Spring Security Spring Security,这是一种基于Spring AOP和Servlet过滤器 [7] 的安全框架.它提供全面的安全性解决方案,同时在 Web 请求级和方法 ...

  4. MVC页面简单post提交

    页面代码 <script src="~/Scripts/jquery-1.10.2.js"></script> <script> $(funct ...

  5. VS2015自定义类模板的方法

    在前一段时间忽然想给自己电脑上的vs新建类的时候添加一个自定义个注释,但是在网上搜了很久都是说vs2012之类的方法系统也都是win7.XP之类的独独没有win8的.故此自己不断的尝试修改发现方法如下 ...

  6. scss-@for 指令

    此指令用于循环输出,具有两种循环方式,下面分别做一下介绍. (1).@for $var from <start> through <end>: 此种方式的遍历索引区间是[sta ...

  7. IE8按F12开发人员工具不显示

    一直不喜欢用IE,不过为了项目需要,不得不使用IE 而且此功能只支持IE8,(⊙﹏⊙)b) 当按F12调出开发人员工具的时候发现显示不出来了,在任务栏里有显示,将鼠标放在任务栏的开发人员工具上, 背景 ...

  8. 在Eclipse安装Genymotion插件的经验心得

    个人心得分享,不当之处还请指正. Eclipse自带的Android模拟器已经无力吐槽了,新手刚上手时或许配置完环境已经精疲力尽了,或许还沉浸在开发成功的喜悦当中,对AVD模拟器的运行情况关注不大,渐 ...

  9. Android自定义之ScrollView下拉刷新

    公司项目,需要用到ScrollView的下拉刷新,一开始使用的时候PullToRefresh三方库的下拉刷新,我比较纠结第三档库,很强大,但是,公司项目的需求,PullToRefresh就不能做到了, ...

  10. Android应用开发详解

    目录结构 1.Android概述 2.Android开发基础 未完待续……