Android学习(8) -- 短信发送器
2016-04-08 22:55
477 查看
短信发送器
功能:用户输入号码和短信内容,点击发送按钮,调用短信api把短信发送给指定号码1. 定义布局
输入框的提示android:hint="请输入号码"
2. 完成点击事件
先给Button组件设置onClick属性onClick="send"
在Activity中定义此方法
public void send(View v){}
3. 获取到用户输入的号码和内容
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();
4. 调用发送短信的api
//调用发送短信的api SmsManager sm = SmsManager.getDefault(); //发送短信 sm.sendTextMessage(phone, null, content, null, null);
* 添加权限
<uses-permission android:name="android.permission.SEND_SMS"/>
* 如果短信过长,需要拆分
List<String> smss = sm.divideMessage(content);
完整代码
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); //3.for循环把集合中所有短信全部发出去 for (String string : smss) { sm.sendTextMessage(phone, null, string, null, null); } }
查询手机的短信中心号码等等:
*#*#4636#*#*
相关文章推荐
- Android Context简介
- Android设备标识符的使用
- 使用Android studio 出现的问题解决
- Android Studio 2.0 新版模拟器
- Android Studio如何发布APK
- Android API讲解之:BitmapFactory
- 使用 Intel HAXM 为 Android 模拟器加速
- android studio运行时报错the selected device is incompatible
- vlc for android中的数据绑定(Data Binding)
- Android矩形Bitmap转为圆形
- android RelativeLayout属性和使用, 实现上面view叠加在下面view之上的效果
- 关于Android的ActionBar不能显示左上角的图标解决方案
- Android中深入理解 LayoutInflater.inflate()
- Android-使用ViewFlipper制作图片轮播器
- Android文件下载之进度检测
- Android EditText 编辑框的使用
- Android 关于 OnScrollListener 事件顺序次数的简要分析
- Android 6.0 运行时权限处理完全解析
- Android编程之LocalBroadcastManager源码详解
- Android笔记---RGB颜色查询对照表