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"/>

 

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

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章