Android实现短信发送器功能


1.短信界面
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. android:layout_width="match_parent"
  3. android:layout_height="match_parent"
  4. android:orientation="vertical" >
  5. <EditText
  6. android:layout_width="match_parent"
  7. android:layout_height="wrap_content"
  8. android:layout_marginTop="10dp"
  9. android:hint="请输入手机号"
  10. android:inputType="phone" />
  11. <EditText
  12. android:layout_width="match_parent"
  13. android:layout_height="wrap_content"
  14. android:layout_marginTop="10dp"
  15. android:hint="请输入短信内容"
  16. android:inputType="text"
  17. android:lines="5" />
  18. <Button
  19. android:layout_width="wrap_content"
  20. android:layout_height="wrap_content"
  21. android:layout_marginTop="10dp"
  22. android:onClick="sendSms"
  23. android:text="发送" />
  24. </LinearLayout>
2.业务逻辑
  1. public void sendSMS(View v) {
  2. // 1.取出手机号
  3. EditText et_input_num = (EditText) findViewById(R.id.et_input_num);
  4. // trim: 过滤用户输入的空格
  5. String num = et_input_num.getText().toString().trim();
  6. // 2. 取出用户输入的短信内容
  7. EditText et_input_content = (EditText) findViewById(R.id.et_input_content);
  8. String content = et_input_content.getText().toString().trim();
  9. // 3. 校验
  10. Pattern pattern = Pattern.compile("^1[3578]\\d{9}$");
  11. Matcher matcher = pattern.matcher(num);
  12. if (matcher.matches()) {
  13. if (content != null && !content.equals("")) {
  14. // 4. 校验成功,发送短信
  15. SmsManager smsManager = SmsManager.getDefault();
  16. smsManager.sendTextMessage(num, null, content, null, null);
  17. } else {
  18. Toast.makeText(this, "请检查输入的内容", Toast.LENGTH_LONG).show();
  19. }
  20. } else {
  21. Toast.makeText(this, "请检查手机号", Toast.LENGTH_LONG).show();
  22. }
  23. }
最后在在AndroidManifest.xml里面添加<uses-permission android:name="android.permission.SEND_SMS"/>

来自为知笔记(Wiz)

优质内容筛选与推荐>>
1、博客园插入超链接时如何取消下划线
2、主机性能监控之wmi 获取进程信息
3、关于Android开发的editText控件怎… 分类: Android界面和组件 2014-05-30 10:59 82人阅读 评论(0) 收藏
4、几个Ext的学习站点 [转]
5、软键盘android:windowSoftInputMode属性详解


长按二维码向我转账

受苹果公司新规定影响,微信 iOS 版的赞赏功能被关闭,可通过二维码转账支持公众号。

    阅读
    好看
    已推荐到看一看
    你的朋友可以在“发现”-“看一看”看到你认为好看的文章。
    已取消,“好看”想法已同步删除
    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号