您的位置:首页 > 移动开发 > Android开发

Android---23---发送短信

2015-04-20 19:05 197 查看
SmsManager:管理短信操作

通过调用静态方法 SmsManager.getDefault() 获取此对象。

SmsManager

方法摘要

boolean copyMessageToSim(byte[] smsc, byte[] pdu, int status)

Copy a raw SMS PDU to the SIM.

boolean deleteMessageFromSim(int messageIndex)

Delete the specified message from the SIM.

ArrayList<String> divideMessage(String text)

Divide a text message into several messages, none bigger than the maximum SMS message size.

ArrayList<SmsMessage> getAllMessagesFromSim()

Retrieves all messages currently stored on SIM.

static SmsManager getDefault()

Get the default instance of the SmsManager

void sendDataMessage(String destinationAddress, String scAddress, short destinationPort, byte[] data, PendingIntent

sentIntent, PendingIntent deliveryIntent)

Send a data based SMS to a specific application port.

void sendMultipartTextMessage(String destinationAddress, String scAddress, ArrayList<String> parts,

ArrayList<PendingIntent> sentIntents, ArrayList<PendingIntent> deliveryIntents)

Send a multi-part text based SMS.

void sendTextMessage(String destinationAddress, String scAddress, String text, PendingIntent sentIntent, PendingIntent

deliveryIntent)

Send a text based SMS.

boolean updateMessageOnSim(int messageIndex, int newStatus, byte[] pdu)

Update the specified message on the SIM.

manager.sendTextMessage(destinationAddress, scAddress, text, sentIntent, deliveryIntent);



destinationAddress —— 消息的目标地址

scAddress —— 服务中心的地址 or 为空使用当前默认的 SMSC 3)

text —— 消息的主体,即消息要发送的数据

sentIntent —— 如果不为空,当消息成功发送或失败这个 PendingIntent 就广播。结果代码是 Activity.RESULT_OK 表示成功

,或 RESULT_ERROR_GENERIC_FAILURE 、 RESULT_ERROR_RADIO_OFF 、 RESULT_ERROR_NULL_PDU 之一表示错误。对应

RESULT_ERROR_GENERIC_FAILURE , sentIntent 可能包括额外的 “ 错误代码 ” 包含一个无线电广播技术特定的值,通常只在修复故障

时有用。

每一个基于 SMS 的应用程序控制检测 sentIntent 。如果 sentIntent 是空,调用者将检测所有未知的应用程序,这将导致在检测的时

候发送较小数量的 SMS 。

deliveryIntent —— 如果不为空,当消息成功传送到接收者这个 PendingIntent 就广播。

MainActivity.java:

import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.telephony.gsm.SmsManager;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

/**
 * 发送短信
 * 
 * @author Caesar
 * 
 */
public class MainActivity extends Activity implements OnClickListener {

	private EditText numText;
	private EditText contentText;
	private Button button;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		numText = (EditText) findViewById(R.id.number);
		contentText = (EditText) findViewById(R.id.content);
		button = (Button) findViewById(R.id.button);
		button.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		String number = numText.getText().toString();
		String content = contentText.getText().toString();
		// SmsManager:管理短信操作,通过静态方法SmsManager.getDefault()获取此对象
		SmsManager manager = SmsManager.getDefault();
		ArrayList<String> texts = manager.divideMessage(content);
		for (String text : texts) {
			manager.sendTextMessage(number, null, text, null, null);
		}
		Toast.makeText(MainActivity.this, "发送成功", Toast.LENGTH_LONG).show();

	}
}


activity_main.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_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="com.example.sendmessagedemo.MainActivity" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="输入手机号" />

    <EditText
        android:id="@+id/number"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="输入短信内容" />

    <EditText
        android:id="@+id/content"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:minLines="3" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="发送" />

</LinearLayout>


需要添加权限:

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


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: