天天看點

android <初級篇> 發送短信(調用已有接口)——菜鳥的學習之路

對于手機來說,打電話,發短信等基本功能在作業系統的平台中都已經做好了,如果你開發的應用程式需要使用到打電話或者發短信的功能,直接調用android提供的方法或接口即可,無需自己重新寫實作的方法。

下面以一個小小的發送短信的例子練習一下如何調用已封裝好的接口

1. 确定需求。

要發送一封短信,需要以下一些元素:

textView : 收信人

EditText: (收信人位址,即電話号碼)

textView:短信内容

EditView: (短信内容)

Button :(發送确認按鈕)

2. 在strings.xml中将所需要的文本資訊添加上去:

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">短信發送</string>
    <string name="action_settings">Settings</string>
    <string name="txt_sender">發送号碼</string>
    <string name="txt_content">短信内容</string>
    <string name="btn_send">發送</string>

</resources>
           

3. 設定界面原型

将所需元件添加到布局檔案中,并為“發送”按鈕添加監聽事件

main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/txt_sender" />

    <EditText
        android:id="@+id/sender"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="phone" >

        <requestFocus />
    </EditText>

    <TextView
       
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/txt_content" />

    <EditText
        android:id="@+id/content"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="textMultiLine" 
        android:minLines="4"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/btn_send"
        android:onClick="sendSms"
         />

</LinearLayout>
           

4. 在Activity中将布局檔案中的元件顯示出來,設計與實作監聽方法(sendSms)

MainActivity.java

package com.example.study2;

import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.widget.EditText;

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
	}

	public void sendSms(View view){
		
		//擷取使用者輸入的資訊
		EditText sender = (EditText) findViewById(R.id.sender);
		EditText content = (EditText) findViewById(R.id.content);
		
		String sendStr = sender.getText().toString();
		String contentStr = content.getText().toString();
		
		//發送短信
		SmsManager smsmsg = SmsManager.getDefault();
		ArrayList<String> msgs = smsmsg.divideMessage(contentStr);
		for(String msg:msgs){
			smsmsg.sendTextMessage(sendStr, null, msg, null, null);
		}
		
		int i = msgs.size();
		System.out.println("發送了"+i+"條短信!");
		
	}
}
           

注意:在使用SmsManager時,在使用Inport進行導入時,一定要注意導入的包是import android.telephony.SmsManager;

如果導入的是 import android.telephony.gsm.SmsManager;

此時,IED工具會提示該類已過時。

在處理發送短信的方法中,有幾點需要注意:

-->首先必須調用SmsManager的getDefault()靜态方法來擷取一個SmsManager對象,才能進行後續的操作

-->将資訊進行切割。每條短信的字數是固定的,在發送短信前,沒有進行短信切割的操作,如果發送的内容過長,超出的部分将被截斷,收信人無法收到截斷的内容。

是以,需要在發送前将資訊分成多條進行發送(調用divideMessage()方法,傳回的是一個ArrayList數組)。

5. 為應用程式授予發送短信的權限

在進行了上面的操作以後,還不能馬上進行短信的發送(當然,如果此時發送的話,會報錯的)

在AndroidMainfest.xml檔案為應用程式授權

在标簽<application>前添加下面的語句:

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

這樣就可以進行測試了。。。