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  "/>
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值