Android实战教程第四篇之简单实现短信发送器
2016-11-10 11:54
591 查看
本文实例为大家分享了Android发短信功能的实现方法,供大家参考,具体内容如下
首先配置一个布局:
<LinearLayout 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" android:orientation="vertical" > <EditText android:id="@+id/et_phone" android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="phone" android:hint="请输入对方号码" /> <EditText android:id="@+id/et_content" android:layout_width="match_parent" android:layout_height="wrap_content" android:lines="5" android:hint="请输入短信内容" android:gravity="top" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="发送" android:onClick="send" /> </LinearLayout>
然后在activity中把发短信的代码写出来:
package com.ydl.smssender; import java.util.ArrayList; //省略导包 public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void send(View v){ //拿到用户输入的号码和内容 EditText et_phone = (EditText) findViewById(R.id.et_phone); EditText et_content = (EditText) findViewById(R.id.et_content); String phone = et_phone.getText().toString(); String content = et_content.getText().toString(); //1.获取短信管理器 SmsManager sm = SmsManager.getDefault(); //2.切割短信,把长短信分成若干个小短信 ArrayList<String> smss = sm.divideMessage(content);//an ArrayList of strings that, in order, comprise the original message //3.for循环把集合中所有短信全部发出去 for (String string : smss) { sm.sendTextMessage(phone, null, string, null, null);//Send a text based SMS. } } }
发短信是需要系统权限的:
效果:
开了两个模拟器,实现了发短信功能。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
您可能感兴趣的文章:
相关文章推荐
- Android实战教程第三篇之简单实现拨打电话功能
- Android实战简易教程<十四>(介绍SwipeMenuListView-简单实现类似QQ的滑动删除效果)
- Android实战简易教程-第十四枪(介绍SwipeMenuListView-简单实现类似QQ的滑动删除效果)
- Android简易实战教程--第四话《最简单的短信发送器》
- Android实战简易教程-第三枪(实现简单绘图组件)
- Android简易实战教程--第四话《最简单的短信发送器》
- Android实战简易教程<三>(实现简单绘图组件)
- Android实战教程第二篇之简单实现两种进度条效果
- Android实战简易教程-第四十八枪(App引导页面效果实现)
- Android实战简易教程-第十五枪(实现ListView中Button点击事件监听)
- Android实战简易教程-第二十八枪(基于Bmob实现头像图片设置和网络上传功能!)
- Android实战简易教程-第三十六枪(监听短信-实现短信验证码自动填入)
- Android实战简易教程-第三十二枪(自定义View登录注册界面EditText-实现一键清空)
- Android实战简易教程-第五十一枪(ListView实现子控件的动态显示和隐藏、checkbox全选和反选)
- Android实战简易教程-第五十三枪(通过实现OnScrollListener接口实现上拉加载更多功能)
- Android实战简易教程-第二十三枪(基于Baas的用户注册和登录模块实现!)