Android開發之傳送簡訊程式

來源:互聯網
上載者:User
Android開發之傳送簡訊程式

一、建立 Android工程

 

Project name: Message

BuildTarget:Android2.2

Application name:sendMessage

Package name:com.dxw.activity

Create Activity: SendMessageActivity

Min SDK Version:8


二、編輯工程

1.編輯strings.xml檔案內容為:

<?xml version="1.0" encoding="utf-8"?><resources>    <string name="phone">請輸入手機號:</string>    <string name="app_name">簡訊的發送</string>    <string name="massage">請輸入資訊內容:</string>    <string name="content">這裡輸入資訊內容</string>    <string name="send">發送</string></resources>

2.編輯main.xml檔案內容為:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    >    <!-- 請輸入手機號碼標籤 --><TextView      android:layout_width="fill_parent"     android:layout_height="wrap_content"     android:text="@string/phone"    />    <!-- 手機號碼編輯框 -->    <EditText     android:id="@+id/number"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:hint="這裡輸入手機號碼"    />    <!-- 請輸入手機內容標籤 --><TextView      android:layout_width="fill_parent"     android:layout_height="wrap_content"     android:text="@string/massage"    />    <!-- 資訊內容輸入框 -->    <EditText     android:id="@+id/content"    android:layout_width="fill_parent"    android:layout_height="45sp"    android:minLines="3"    android:hint="@string/content"    />    <!-- 發送按紐 -->    <Button     android:id="@+id/send"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:text="send"    /></LinearLayout>

注意,我們在電話號碼輸入框和撥打到電話按鈕中添加了android:id屬性。如電話號碼輸入框的android:id=”@+id/mobile”,@代碼R.java,+id代碼添加id靜態內部類,mobile代表向id類中添加一個常量成員。ADT將自動為我們產生常量值。

android:minLines設定資訊內容編輯框的最小行數


3.編輯SendMessageActivity.java內容:

package com.dxw.intent.activity;import java.util.List;import android.app.Activity;import android.os.Bundle;import android.telephony.gsm.SmsManager;import android.view.View;import android.widget.Button;import android.widget.EditText;public class IntentDemoActivity extends Activity {private Button btn = null;private EditText number = null;private EditText content = null;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);// 擷取按紐ID事件btn = (Button) findViewById(R.id.send);// 擷取按紐的單擊事件btn.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {// 擷取手機號碼輸入框的IDnumber = (EditText) findViewById(R.id.number);// 輸入的擷取手機號碼String sendNumber = number.getText().toString();// 擷取發送資訊的IDcontent = (EditText) findViewById(R.id.content);// 擷取輸入的資訊String sendContent = content.getText().toString();if (sendContent != null) {SmsManager sms = SmsManager.getDefault();// 如果簡訊沒有超過限制長度,則返回一個長度的List。List<String> texts = sms.divideMessage(sendContent);for (String text : texts) {sms.sendTextMessage(sendNumber, null, text, null, null);}}}});}}

sms.sendTextMessage(destinationAddress,scAddress, text, sentIntent, deliveryIntent):

destinationAddress:接收方的手機號碼

scAddress:發送方的手機號碼

text:資訊內容

sentIntent:發送是否成功的回執,以後會詳細介紹。

DeliveryIntent:接收是否成功的回執

4.編輯AndroidManifest.xml內容:

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"      package="com.dxw.intent.activity"      android:versionCode="1"      android:versionName="1.0">    <uses-sdk android:minSdkVersion="8" />    <application android:icon="@drawable/icon" android:label="@string/app_name">        <activity android:name=".IntentDemoActivity"                  android:label="@string/app_name">            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity><!-- 註冊發送資訊的許可權 -->    </application>    <uses-permission     android:name="android.permission.SEND_SMS"    /></manifest>

註冊傳送簡訊的許可權,如果沒有註冊這個,將使用不了系統的傳送簡訊功能。以後在我們的應用程式開發中,有使用到系統功能的必須在這個檔案中進行註冊。我們可以查看Android的協助手冊都有能。(.../android-sdk-windows/docs/reference/android/Manifest.permission.html)

三、啟動模擬器(註:這段代碼不能給自己發 必須要啟動兩個模擬器才能測試)

我們給誰發簡訊?我們可以啟動兩個模擬器。使用一個模擬器給另一個模擬器發資訊。首先我們使用工具列上的手機表徵圖再添加一個Android2.2的模擬器,另記一個名稱。

在啟動兩個模擬器之前,我們需要模擬器能“接收到訊號”。如果我們的機器是連網的,啟動模擬器後,主介面顯示訊號強度的旁邊會有一個3G的字樣,這說明模擬器已經能接收到訊號了。如果我們的機器不能連網,那麼將自己的IP地址、網關和DNS伺服器都設定為相同的值,比如都設定為192.168.0.100。如果我們的機器是在區域網路下,但沒有連網,那麼將自己的網關和DNS設定為路由的IP即可,一般情況下路由的IP是192.168.0.1。

OK,現在我們啟動兩個模擬器!

四、傳送簡訊

我們啟動模擬器後,可以看到模擬器視窗的標題列上有5554和55556的字樣。這是模擬器監聽的連接埠即——127.0.0.1:5554。

在工程上右鍵,Run As AndroidApplication,選擇其中的一個模擬器。比如選擇了連接埠為5554的模擬器。OK,程式被載入到模擬器中了,會被自動運行。

我們在電話號碼編輯框中輸入5556(接收端模擬器的連接埠號碼),在資訊框輸入要發送到餓資訊,點擊發送按鈕!

OK,你看到效果了嗎?5556主介面,顯示訊號強度的旁邊顯示收到新簡訊。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.