[小试牛刀之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都如此让人备有成就感。




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值