Android 自製傳送簡訊程式

來源:互聯網
上載者:User

 1 package com.turboradio.googlesdk;
2
3 import java.util.regex.Matcher;
4 import java.util.regex.Pattern;
5
6 import android.app.Activity;
7 import android.app.PendingIntent;
8 import android.content.Intent;
9 import android.os.Bundle;
10 import android.telephony.SmsManager;
11 import android.view.View;
12 import android.widget.Button;
13 import android.widget.EditText;
14 import android.widget.Toast;
15
16 public class Ex_5_3 extends Activity {
17 private Button send;
18 private EditText phoneNumber;
19 private EditText content;
20 @Override
21 protected void onCreate(Bundle savedInstanceState) {
22 super.onCreate(savedInstanceState);
23 setContentView(R.layout.ex_5_3);
24 phoneNumber =(EditText) findViewById(R.id.phoneNumber);
25 content = (EditText)findViewById(R.id.content);
26 send = (Button)findViewById(R.id.send);
27 phoneNumber.setOnClickListener(new Button.OnClickListener(){
28
29 public void onClick(View v) {
30 phoneNumber.setText("");
31 }});
32 content.setOnClickListener(new Button.OnClickListener(){
33
34 public void onClick(View v) {
35 content.setText("");
36 }});
37 send.setOnClickListener(new Button.OnClickListener(){
38
39 public void onClick(View v) {
40 /**簡訊收信人電話**/
41 String strDestAddress = phoneNumber.getText().toString();
42 /**擷取簡訊的內容**/
43 String strMessage = content.getText().toString();
44 /**構建一個SmsManager對象**/
45 SmsManager smsManager = SmsManager.getDefault();
46 /**檢查收件者的電話號碼和簡訊字數是否超過70個字元**/
47 if(isPhoneNumberValid(strDestAddress) && iswithin70(strMessage)){
48 /**兩個條件都滿足的條件下,傳送簡訊**/
49 PendingIntent mPi = PendingIntent.getBroadcast(Ex_5_3.this, 0, new Intent(), 0);
50 smsManager.sendTextMessage(strDestAddress, null, strMessage+"", mPi, null);
51 Toast.makeText(Ex_5_3.this, "發送成功!", Toast.LENGTH_LONG).show();
52 }else{
53 /**電話格式和簡訊文字不符合時**/
54 if(!isPhoneNumberValid(strDestAddress)&&!iswithin70(strMessage)){
55 Toast.makeText(Ex_5_3.this, "電話號碼錯誤 + 簡訊內容超過70個,請檢查", Toast.LENGTH_LONG).show();
56 }
57 if(!isPhoneNumberValid(strDestAddress)&&iswithin70(strMessage)){
58 Toast.makeText(Ex_5_3.this, "電話號碼錯誤,請檢查 ", Toast.LENGTH_LONG).show();
59 }
60 if(isPhoneNumberValid(strDestAddress)&&!iswithin70(strMessage)){
61 Toast.makeText(Ex_5_3.this, "簡訊內容超過70個字元!!", Toast.LENGTH_LONG).show();
62 }
63 }
64
65 }});
66
67 }
68 /**檢查字串是否為電話號碼 並返回 true false**/
69 public boolean isPhoneNumberValid(String phoneNumber){
70 boolean isValid = false;
71 String expression = "^(?(\\d{3})\\)?(\\d{3})[-]?(\\d{5})$";
72 String expression2 = "^\\(?\\d{3}\\)?[-]?(\\d{4})$";
73 CharSequence inputStr = phoneNumber;
74 /**建立Pattern**/
75 // Pattern pattern = Pattern.compile(expression);
76 // Matcher matcher = pattern.matcher(inputStr);
77 // Pattern pattern2 = Pattern.compile(expression2);
78 // Matcher matcher2 = pattern2.matcher(inputStr);
79 // if(matcher.matches() || matcher2.matches()){
80 // isValid = true;
81 // }
82 // return isValid;
83 return true;
84 }
85 public boolean iswithin70(String text){
86 if(text.length()<=70){
87 return true;
88 }else{
89 return false;
90 }
91 }
92 }

ex5_3.xml

 1 <?xml version="1.0" encoding="utf-8"?>
2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 android:layout_width="fill_parent"
4 android:layout_height="fill_parent"
5 android:background="#FFFFFF"
6 >
7 <TextView
8 android:id="@+id/text1"
9 android:layout_width="wrap_content"
10 android:layout_height="wrap_content"
11 android:text="收件者:"
12 android:textSize="8pt"
13 android:layout_marginTop="20dip"
14 />
15 <EditText
16 android:id="@+id/phoneNumber"
17 android:layout_width="match_parent"
18 android:layout_height="wrap_content"
19 android:hint="請輸入電話號碼"
20 android:layout_toRightOf="@id/text1"
21 android:layout_alignParentTop="true"
22 />
23 <EditText
24 android:id="@+id/content"
25 android:layout_width="fill_parent"
26 android:layout_height="200dip"
27 android:hint="請輸入簡訊內容"
28 android:background="#dcdcdcdc"
29 android:layout_below="@id/phoneNumber"
30 />
31 <Button
32 android:id="@+id/send"
33 android:layout_width="wrap_content"
34 android:layout_height="wrap_content"
35 android:layout_below="@id/content"
36 android:layout_marginLeft="100dip"
37 android:layout_marginTop="50dip"
38 android:text=" 撥我傳送簡訊 "
39 />
40 </RelativeLayout>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.