[小試牛刀之Android]兩種方式下的短信發送

小生最近在學習Android開發,剛看完一段短信發送器的教程,然後突發奇想,想做個對比。

教程裏是調用SmsManager這個API來實現的,而我想調用系統自帶的SMS來實現。

沒錯,你看出來了吧,我就是沒事瞎折騰。


廢話說完了,下面進入正題:


一、layout佈局:兩個TextView、兩個EditText、兩個Button。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/tv_number"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="號碼:"
     
         />

    <EditText
        android:id="@+id/et_number"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@id/tv_number"
        android:numeric="integer">
    </EditText>

    <TextView
        android:id="@+id/tv_content"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@id/et_number"
        android:text="消息內容"
        />

    <EditText
        android:id="@+id/et_content"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/tv_content"
        android:lines="5"
        android:ems="120" />

    <Button
        android:id="@+id/bt_sendType1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/et_content"
        android:text="發送(不留底)" />

    <Button
        android:id="@+id/bt_sendType2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/et_content"
        android:text="發送(留底)" />

</RelativeLayout>


效果如下:



二、代碼實現:很簡單的幾行代碼,是不是很淺顯易懂,還有註釋好麼!


package com.willkey.sms;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener {

	private EditText et_number;
	private EditText et_content;
	private Button bt_sendType1;
	private Button bt_sendType2;

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		// 獲取控件佈局
		et_number = (EditText) findViewById(R.id.et_number);
		et_content = (EditText) findViewById(R.id.et_content);
		bt_sendType1 = (Button) findViewById(R.id.bt_sendType1);
		bt_sendType2 = (Button) findViewById(R.id.bt_sendType2);
		// 監聽控件
		bt_sendType1.setOnClickListener(this);
		bt_sendType2.setOnClickListener(this);

	} 

	@Override
	public void onClick(View v) {

		switch (v.getId()) {
		// 發送方式一:不留底,用SmsManager的API發送
		case R.id.bt_sendType1:
			// 獲取內容
			String number = et_number.getText().toString().trim();
			String content = et_content.getText().toString().trim();

			// 判斷輸入內容是否爲空
			if (TextUtils.isEmpty(content) || TextUtils.isEmpty(number)) {
				Toast.makeText(this, "號碼或內容不能爲空!請重新輸入!", Toast.LENGTH_SHORT)
						.show();
			} else {

				// 調用API發送短信,
				// SmsManager.getDefault()來初始化對象
				SmsManager sm = SmsManager.getDefault();

				// 發送長短信的方法
				ArrayList<String> contents = sm.divideMessage(content);

				for (String str : contents) {
					sm.sendTextMessage(number, null, str, null, null);
					Toast.makeText(this, "發送完畢!", Toast.LENGTH_SHORT).show();
				}
			}
			break;

		// 發送方式2:留底,用intent調用系統短信應用來發送
		case R.id.bt_sendType2:
			// 獲取內容
			String number2 = et_number.getText().toString().trim();
			String content2 = et_content.getText().toString().trim();

			// 判斷輸入內容是否爲空
			if (TextUtils.isEmpty(content2) || TextUtils.isEmpty(number2)) {
				Toast.makeText(this, "號碼或內容不能爲空!請重新輸入!", Toast.LENGTH_SHORT)
						.show();
			} else {

				Uri uri = Uri.parse("sms:" + number2);
				Intent intent = new Intent(Intent.ACTION_SENDTO, uri);
				intent.putExtra("sms_body", content2);
				startActivity(intent);
				Toast.makeText(this, "發送完畢!", Toast.LENGTH_SHORT).show();

			}
			break;
		default:
			break;
		}
	}

}


三、總結:

1)通過這個DEMO的學習,讓我理解了Activity的生命週期,API的使用方法,Intent的使用方法。都瞎扯的,不過大抵如此啊。

2)這點是最重要的,這兩種方式,最大的不同點在於,調用了SmsManager的方式不會在本地留記錄,而調用了系統的短信應用當然會留底了。所以前者基本上是那種無良開發者用來騙錢的方式,後者基本上不會這麼用。


其實還有很多功能可以繼續添加,以後有機會再折騰一下。


結語:開發Android應用就像小時候搭四驅車一樣,有趣又有挑戰,就算完成一個小小的DEMO都如此讓人備有成就感。




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