Android realizes SMS transmitter function

Source: Internet
Author: User

1. SMS Interface
  
  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. Business logic
  
  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. }
finally add in Androidmanifest.xml<uses-permission android:name= "android.permission. send_sms "/>

From for notes (Wiz)

Android realizes SMS transmitter function

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.