请教android发送短信的问题

benbshmily 2010-05-04 01:03:16
我使用sendTextMessage方法发送短信。如下:
PendingIntent pi = PendingIntent.getBroadcast(context,0,new Intent("myIntentAction"),0);
sManager.sendTextMessage(msg.getDestAddr(), null, msg.getBody(), pi, null);
我的问题是,怎么判断该短信是否发送成功。是不是用一个broadcast来监听呢?
是不是发送成功了才广播?
那怎么判断短信发送失败了呢?就是说,我调用了sendTextMessage方法后,怎么判断短信是发送成功了还是发送失败了?
谢谢。
...全文
970 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
东方絜烁 2011-04-01
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 sodino 的回复:]
引用 10 楼 zengyangtech 的回复:

引用 9 楼 sodino 的回复:

引用 4 楼 partner4java 的回复:
呵呵,我来说一下吧,可以结贴了,给分吧
首先给你说文档里面明明就给你说了,哈哈
sentIntent if not NULL this PendingIntent is broadcast when the message is sucess……
[/Quote]

电信的有吗?
东方絜烁 2011-03-24
  • 打赏
  • 举报
回复
想问一下哦,发短信就下面这句中的 sendPI 和 deliveryPI 是干什么的,如果这两个参数都为null 时,短信也可以发出去,这两个参数是干什么用的啊?

smsMag.sendTextMessage(smsAddress, null, smsBody, sendPI, deliveryPI);
maverickgoose 2011-01-06
  • 打赏
  • 举报
回复
NICE I need this
Sodino 2010-11-10
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 zengyangtech 的回复:]

引用 9 楼 sodino 的回复:

引用 4 楼 partner4java 的回复:
呵呵,我来说一下吧,可以结贴了,给分吧
首先给你说文档里面明明就给你说了,哈哈
sentIntent if not NULL this PendingIntent is broadcast when the message is sucessfully sent, or failed. The ……
[/Quote]

中国移动有delivery
中国联通没有
Zengyangtech 2010-11-03
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 sodino 的回复:]

引用 4 楼 partner4java 的回复:
呵呵,我来说一下吧,可以结贴了,给分吧
首先给你说文档里面明明就给你说了,哈哈
sentIntent if not NULL this PendingIntent is broadcast when the message is sucessfully sent, or failed. The result code will be Ac……
[/Quote]
delivery的消息我怎么没有捕捉到过啊~
Sodino 2010-09-08
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 partner4java 的回复:]
呵呵,我来说一下吧,可以结贴了,给分吧
首先给你说文档里面明明就给你说了,哈哈
sentIntent if not NULL this PendingIntent is broadcast when the message is sucessfully sent, or failed. The result code will be Activity.RESULT_OK for succes……
[/Quote]

我完善一下,给出代码如下:


package lab.sodino.smslistener;

import android.app.Activity;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.telephony.gsm.SmsManager;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

public class SmsAct extends Activity {
private TextView textView;
private static String ACTION_SMS_SEND = "lab.sodino.sms.send";
private static String ACTION_SMS_DELIVERY = "lab.sodino.sms.delivery";
private SMSReceiver sendReceiver;
private SMSReceiver deliveryReceiver;

public class SMSReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
String actionName = intent.getAction();
int resultCode = getResultCode();
if (actionName.equals(ACTION_SMS_SEND)) {
switch (resultCode) {
case Activity.RESULT_OK:
textView.append("\n[Send]SMS Send:Successed!");
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
textView
.append("\n[Send]SMS Send:RESULT_ERROR_GENERIC_FAILURE!");
break;
case SmsManager.RESULT_ERROR_NO_SERVICE:
textView
.append("\n[Send]SMS Send:RESULT_ERROR_NO_SERVICE!");
break;
case SmsManager.RESULT_ERROR_NULL_PDU:
textView.append("\n[Send]SMS Send:RESULT_ERROR_NULL_PDU!");
break;
case SmsManager.RESULT_ERROR_RADIO_OFF:
break;
}
} else if (actionName.equals(ACTION_SMS_DELIVERY)) {
switch (resultCode) {
case Activity.RESULT_OK:
textView.append("\n[Delivery]SMS Delivery:Success!");
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
textView
.append("\n[Delivery]SMS Delivery:RESULT_ERROR_GENERIC_FAILURE!");
break;
case SmsManager.RESULT_ERROR_NO_SERVICE:
textView
.append("\n[Delivery]SMS Delivery:RESULT_ERROR_NO_SERVICE!");
break;
case SmsManager.RESULT_ERROR_NULL_PDU:
textView
.append("\n[Delivery]SMS Delivery:RESULT_ERROR_NULL_PDU!");
break;
case SmsManager.RESULT_ERROR_RADIO_OFF:
textView
.append("\n[Delivery]SMS Delivery:RESULT_ERROR_RADIO_OFF!");
break;
}
}
}
}

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Button btn = new Button(this);
btn.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
sendSMS();
}
});
textView = new TextView(this);
textView.setBackgroundColor(0xffffffff);
textView.setTextColor(0xff0000ff);
textView.setText("SMS processing...");
btn.setText("发送短信");
LinearLayout.LayoutParams textParams = new LinearLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);

LinearLayout linear = new LinearLayout(this);
linear.setOrientation(LinearLayout.VERTICAL);
linear.setLayoutParams(textParams);
linear.addView(btn);
linear.addView(textView);
setContentView(linear);
sendReceiver = new SMSReceiver();
IntentFilter sendFilter = new IntentFilter(ACTION_SMS_SEND);
registerReceiver(sendReceiver, sendFilter);
deliveryReceiver = new SMSReceiver();
IntentFilter deliveryFilter = new IntentFilter(ACTION_SMS_DELIVERY);
registerReceiver(deliveryReceiver, deliveryFilter);
}

private void sendSMS() {
String smsAddress = "10086";
String smsBody = "bylcx";
SmsManager smsMag = SmsManager.getDefault();
Intent sendIntent = new Intent(ACTION_SMS_SEND);
PendingIntent sendPI = PendingIntent.getBroadcast(this, 0, sendIntent,
0);
Intent deliveryIntent = new Intent(ACTION_SMS_DELIVERY);
PendingIntent deliveryPI = PendingIntent.getBroadcast(this, 0,
deliveryIntent, 0);
smsMag.sendTextMessage(smsAddress, null, smsBody, sendPI, deliveryPI);
}

protected void onPaused() {
unregisterReceiver(sendReceiver);
unregisterReceiver(deliveryReceiver);
}
}
computerclass 2010-05-10
  • 打赏
  • 举报
回复
hshm20517 2010-05-06
  • 打赏
  • 举报
回复
发送成功状态,监听不到吧
haierjodn 2010-05-05
  • 打赏
  • 举报
回复
mark
partner4java 2010-05-05
  • 打赏
  • 举报
回复
呵呵,我来说一下吧,可以结贴了,给分吧
首先给你说文档里面明明就给你说了,哈哈
sentIntent if not NULL this PendingIntent is broadcast when the message is sucessfully sent, or failed. The result code will be Activity.RESULT_OK for success, or one of these errors:
RESULT_ERROR_GENERIC_FAILURE
RESULT_ERROR_RADIO_OFF
RESULT_ERROR_NULL_PDU
For RESULT_ERROR_GENERIC_FAILURE the sentIntent may include the extra "errorCode" containing a radio technology specific value, generally only useful for troubleshooting.
The per-application based SMS control checks sentIntent. If sentIntent is NULL the caller will be checked against all unknown applications, which cause smaller number of SMS to be sent in checking period.

如果没看懂英文的话,我给你解释一下,实现步骤:
一:sentIntent = PendingIntent.getBroadcast(this, 0,
intent, PendingIntent.FLAG_UPDATE_CURRENT);
这里面的intent,要给一个广播,当然一个activity也可以,广播如下:
Intent intent = new Intent(this, ReceiveSMSBroadcast.class);
知道广播吧?
public class ReceiveSMSBroadcast extends BroadcastReceiver
别忘记配置
<receiver android:name=".activity.receiver.ReceiveSMSBroadcast"/>
二:发送后自动调用广播,利用广播接收数据:
@Override
public void onReceive(Context context, Intent intent) {
用这个方法接收数据,哈哈,我真废话
int resultCode = getResultCode()
这是你接收的状态,状态就如上面的英文文档那几种状态
Activity.RESULT_OK
SmsManager.RESULT_ERROR_GENERIC_FAILURE:
SmsManager.RESULT_ERROR_RADIO_OFF
SmsManager.RESULT_ERROR_NULL_PDU
SmsManager.RESULT_ERROR_NO_SERVICE

当然你也可以利用intent Extra 传递携带一些其它你想要的数据
无限666 2010-05-05
  • 打赏
  • 举报
回复
定一个~
vclongking 2010-05-04
  • 打赏
  • 举报
回复
恩 我之前做过一个短信程序 也是在这里有问题 本来想做一个发送对话框提示的 但还真没找到相应的接口
关注 学习
Sodino 2010-05-04
  • 打赏
  • 举报
回复
这个还真没有想过,看了下Api也没找着相关方法,帮顶...
benbshmily 2010-05-04
  • 打赏
  • 举报
回复
没有人指点一下我么?

80,359

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧