Android学习之短信发送器

今天,继续android学习,一个小小的例子来记录一下。

布局文件如下:

<RelativeLayout 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"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/tv_number"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/please_input_number"/>
    <EditText 
        android:id="@+id/et_number"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:singleLine="true"
        android:layout_below="@id/tv_number"/>
    <TextView
        android:id="@+id/tv_content"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/et_number"
        android:text="@string/please_input_content"/>
    <EditText 
        android:id="@+id/et_content"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:lines="5"
        android:layout_below="@id/tv_content"/>
    <Button
        android:id="@+id/send_sms"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/et_content"
        android:text="@string/send_sms"/>"

</RelativeLayout>
xml文件的相关参数,我已经在之前的电话拨号器里面有简单的介绍到,这里就不做详细的介绍了。

接下来就是相关的操作了,具体代码如下所示:

package com.example.sms;

import java.util.ArrayList;

import android.os.Bundle;
import android.app.Activity;
import android.telephony.SmsManager;
import android.telephony.SmsMessage;
import android.text.TextUtils;
import android.view.Menu;
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 = null;
	private EditText et_content = null;
	private Button send_sms = null;
	@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);
		send_sms = (Button) findViewById(R.id.send_sms);
		send_sms.setOnClickListener(this);
	}
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.send_sms:
			String number = et_number.getText().toString().trim();
			String content = et_content.getText().toString();
			if(TextUtils.isEmpty(number) || TextUtils.isEmpty(content)) {
				Toast.makeText(this, "号码或者内容不能为空",Toast.LENGTH_SHORT).show();
			}
			SmsManager smsManager = SmsManager.getDefault();
			ArrayList<String> contents = smsManager.divideMessage(content);
			for(String str : contents) {
				smsManager.sendTextMessage(number, null,str,null,null);
			}
			break;
		default:
			break;
		}
	}
}

这里采用的是之前所说的4中响应方式的第四种。

在监听器里面主要是通过注册监听的控件的ID来区分是属于哪种事件的。这里通过相关方法获取到文本框输入的电话号码和要发送的内容,如果有一项为空,通会通过Toast(吐司)输出一个提示信息,显示“号码或者内容不能为空”。

接下来,发送信息的时候也会出现权限的错误,需要添加SEND_SMS权限。如下所示:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.sms"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />
    <uses-permission android:name="android.permission.SEND_SMS"/>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.sms.MainActivity"
            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>

</manifest>
好了,所有的工作已经完成,接下来就可以输入电话号码和内容进行发送给了,而且可以在超出信息长度的情况下,把信息分成多段来艾段发送。

今天的学习就先到这里。之后我会继续把学习心得记录在这里,以便大家和自己学习。


你可能感兴趣的:(Android学习之短信发送器)