android发短信

作者: nick 分类: Android 发布时间: 2011-08-21 11:47 ė 6没有评论

1.XML androidManifest.xml 获取权限。

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

2.SmsManager对象获取

//貌似就这一个方法获得。

SmsManager smsManager = SmsManager.getDefault();

3.获取PendingIntent对象

PendingIntent mpi = PendingIntent.getBroadcast(MainActivity.this, 0, new Intent(), 0);

PendingIntent android.app.PendingIntent.getBroadcast(Context context, int requestCode, Intent intent, int flags)

context The Context in which this PendingIntent should perform the broadcast.一般用当前Activity的实例

requestCode Private request code for the sender (currently not used).

intent The Intent to be broadcast.一般new Intent()就可以了。

flags May be FLAG_ONE_SHOT, FLAG_NO_CREATE, FLAG_CANCEL_CURRENT, FLAG_UPDATE_CURRENT, or any of the flags as supported by Intent.fillIn() to control which unspecified parts of the intent that can be supplied when the actual send happens.

4.发送短信

smsManager.sendTextMessage(“10086”, null, “hello”, mpi, null);

void android.telephony.SmsManager.sendTextMessage(

String destinationAddress,

String scAddress,

String text,

PendingIntent sentIntent,

PendingIntent deliveryIntent)

destinationAddress:对方的手机号

scAddress:is the service center address or null to use the current default SMSC,就是Short
message service center.短消息服务中心,可以为空。

text:你要发送的内容,注意先确定长度。

sentIntent:就刚才获得的PendingIntent

deliveryIntent:if not NULL this PendingIntent is broadcast when the message is delivered to the recipient. The raw pdu of the status report is in the extended data (“pdu”).这个应该是信息传递到收件人的时候的一个状态回复,表示短信已经发送到了。这个可以为空。

5.发送长度超过70个汉字字符的短信。

(1)切分

ArrayList<String> android.telephony.SmsManager.divideMessage(String text)
(2)发送
void android.telephony.SmsManager.sendMultipartTextMessage(
String destinationAddress,
String scAddress,
ArrayList<String> parts,
ArrayList<PendingIntent> sentIntents,
ArrayList<PendingIntent> deliveryIntents)

6.发送二进制信息。

void android.telephony.SmsManager.sendDataMessage(
String destinationAddress,
String scAddress,
short destinationPort,
byte[] data,
PendingIntent sentIntent,
PendingIntent deliveryIntent)

本文出自 传播、沟通、分享,转载时请注明出处及相应链接。

本文永久链接: https://www.nickdd.cn/?p=1688

发表评论

您的电子邮箱地址不会被公开。

Ɣ回顶部