Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
您现在的位置: Android教程網 >> Android系統教程 >  >> Android 技術 >> Android 開發實例

Android 中簡訊的發送與接收實作

編輯:Android 開發實例

做 Android 開發時經常會用到簡訊的發送與接收,透過 Android 提供的 API 實作起來相當簡單。今天正好用到這個功能,順手研究並記錄下來,以加深記憶。

1、首先建立一個 Android Project。

2、設計一個 main Activity 作為發送簡訊的操作介面,設計好的介面如下圖:

介面布局檔內容如下:

 1: <?xml version="1.0" encoding="utf-8"?>
 2: <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
 3: android:orientation="vertical"
 4: android:layout_width="fill_parent"
 5: android:layout_height="fill_parent" 
 6: android:padding="10sp" 
 7: >
 8: <TextView 
 9: android:layout_width="fill_parent" 
 10: android:layout_height="wrap_content" 
 11: android:text="@string/mobile_label"
 12: 
 13: />
 14: <EditText 
 15: android:layout_width="fill_parent"
 16: android:layout_height="wrap_content"
 17: android:id="@+id/txt_from"
 18: android:hint="請輸入電話號碼"
 19: 
 20: />
 21: <TextView 
 22: android:layout_width="fill_parent" 
 23: android:layout_height="wrap_content" 
 24: android:text="@string/content_label"
 25: />
 26: <EditText 
 27: android:layout_width="fill_parent"
 28: android:layout_height="wrap_content"
 29: android:id="@+id/txt_content"
 30: android:hint="請輸入短信內容"
 31: android:lines="3"
 32: />
 33: <TextView android:layout_width="fill_parent"
 34: android:layout_height="wrap_content"
 35: ></TextView>
 36: <Button 
 37: android:text="發送短信"
 38: android:layout_width="fill_parent"
 39: android:layout_height="wrap_content"
 40: android:gravity="center"
 41: android:id="@+id/btnSend"
 42: android:paddingTop="20sp"
 43: />
 44: </LinearLayout>

3、建立一個 Java 類別檔,匯入以下套件:

 1: import java.util.regex.Matcher;
 2: import java.util.regex.Pattern;
 3: import com.eshore.smsManagers.R;
 4: import android.app.Activity;
 5: import android.telephony.gsm.*;
 6: import android.view.View;
 7: import android.view.View.OnClickListener;
 8: import android.widget.Button;
 9: import android.widget.EditText;
 10: import android.widget.Toast;
 11: import android.os.Bundle;

4、重寫 onCreate 方法:

 1: txtFrom=(EditText)this.findViewById(R.id.txt_from);
 2: txtContent=(EditText)this.findViewById(R.id.txt_content);
 3: btnSend=(Button)this.findViewById(R.id.btnSend);
 4: btnSend.setOnClickListener(new OnClickListener() { 
 5: public void onClick(View v) {
 6: if(!validate())
 7: return;
 8: SmsManager.getDefault().sendTextMessage(txtFrom.getText().toString().trim(),null,txtContent.getText().toString(),null,null);
 9: txtFrom.setText("");
 10: txtContent.setText("");
 11: Toast toast=Toast.makeText(main.this, "短信發送成功!",Toast.LENGTH_LONG);
 12: toast.show();
 13: }
 14: });

相關輔助方法包含號碼合法性驗證與簡訊內容不可為空:

 1: //合法性驗證
 2: private boolean validate(){
 3: String mobile=txtFrom.getText().toString().trim();
 4: String content=txtContent.getText().toString();
 5: if(mobile.equals("")){
 6: Toast toast=Toast.makeText(this, "手機號碼不能為空!",Toast.LENGTH_LONG);
 7: toast.show();
 8: return false;
 9: }
 10: else if(!checkMobile(mobile)){
 11: Toast toast=Toast.makeText(this, "您輸入的電話號碼不正確請重新輸入!",Toast.LENGTH_LONG);
 12: toast.show();
 13: return false;
 14: }
 15: else if(content.equals("")){
 16: Toast toast=Toast.makeText(this, "短信內容不能為空請重新輸入!",Toast.LENGTH_LONG);
 17: toast.show();
 18: return false;
 19: }
 20: else{
 21: return true;
 22: }
 23: 
 24: }
 25: /*
 26: * 中國移動134.135.136.137.138.139.150.151.152.157.158.159.187.188 ,147(數據卡不驗證)
 27: 中國聯通130.131.132.155.156.185.186 
 28: 中國電信133.153.180.189 
 29: CDMA 133,153
 30: 手機號碼驗證 適合目前所有的手機
 31: */
 32: public boolean checkMobile(String mobile){ 
 33: String regex="^1(3[0-9]|5[012356789]|8[0789])\d{8}$";
 34: Pattern p = Pattern.compile(regex); 
 35: Matcher m = p.matcher(mobile); 
 36: return m.find(); 
 37: } 

經過以上幾個步驟,發送簡訊的功能基本完成,但現在運行程式還無法工作,主要是因為設定檔 AndroidManifest.xml 中的權限還沒設定。要發送簡訊就得設定發送權限,Android 才會送出訊息,否則發不出去;同樣地,接收訊息也需要相應的接收權限。由於後面還要做接收簡訊的內容,這裡順便把收發權限都設定好:

 1: <uses-permission android:name="android.permission.SEND_SMS"/>
 2: <uses-permission android:name="android.permission.RECEIVE_SMS"/>

可以看出第一行是發送簡訊的權限,第二行是接收簡訊的權限。

運行程式,填入正確的手機號與簡訊內容並點擊發送,就能把內容傳到對應的號碼。

5、接收簡訊稍有點複雜,首先建立一個接收簡訊的 Java 類別檔「ReceiverDemo.java」並繼承「BroadcastReceiver」。

6、重寫以下方法:

 1: public void onReceive(Context context, Intent intent)

重寫內容如下:

 1: private static final String strRes = "android.provider.Telephony.SMS_RECEIVED";
 2: @Override
 3: public void onReceive(Context context, Intent intent) {
 4: /*
 5: * 判斷是否是SMS_RECEIVED事件被觸發
 6: */
 7: if (intent.getAction().equals(strRes)) {
 8: StringBuilder sb = new StringBuilder();
 9: Bundle bundle = intent.getExtras();
 10: if (bundle != null) {
 11: Object[] pdus = (Object[]) bundle.get("pdus");
 12: SmsMessage[] msg = new SmsMessage[pdus.length];
 13: for (int i = 0; i < pdus.length; i++) {
 14: msg[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
 15: }
 16: for (SmsMessage currMsg : msg) {
 17: sb.append("您收到了來自:【");
 18: sb.append(currMsg.getDisplayOriginatingAddress());
 19: sb.append("】\n的信息,內容:");
 20: sb.append(currMsg.getDisplayMessageBody());
 21: }
 22: Toast toast = Toast.makeText(context, "收到了短消息: " + sb.toString(),Toast.LENGTH_LONG); 
 23: toast.show(); 
 24: }
 25: }
 26: }

注意:

A:第 7 行用於判斷當前監聽的是否為接收簡訊這個事件,若是則進行後續處理,否則不處理。

B:第 12 到 22 行解析出發信人的號碼與簡訊內容,並組成一個可讀的字串。

C:第 22、23 行把組成的字串顯示給使用者。

7、在 AndroidManifest.xml 中設定一個 Receiver,如下:

 1: <receiver android:name=".ReceiverDemo" android:enabled="true"> 
 2: <intent-filter> 
 3: <action android:name="android.provider.Telephony.SMS_RECEIVED" /> 
 4: </intent-filter> 
 5: </receiver> 

經過以上七個步驟,就能實現簡訊的發送與接收了。編譯並安裝到手機上,就能發送簡訊;當手機收到簡訊時,會自動彈出一段提示字串給使用者。當然,你也可以修改接收的程式碼,在 onReceive 裡實作一些有趣的功能,例如收到簡訊時播放一首喜歡的歌,或在收到簡訊後自動轉發給指定號碼等,玩法相當自由。

轉自:https://www.cnblogs.com/vaiyanzi/archive/2010/05/06/1728465.html

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved