我使用sendTextMessage方法发送短信。如下:
PendingIntent pi = PendingIntent.getBroadcast(context,0,new Intent("myIntentAction"),0);
sManager.sendTextMessage(msg.getDestAddr(), null, msg.getBody(), pi, null);
我的问题是,怎么判断该短信是否发送成功。是不是用一个broadcast来监听呢?
是不是发送成功了才广播?
那怎么判断短信发送失败了呢?就是说,我调用了sendTextMessage方法后,怎么判断短信是发送成功了还是发送失败了?
谢谢。
PendingIntent pi = PendingIntent.getBroadcast(context,0,new Intent("myIntentAction"),0);
sManager.sendTextMessage(msg.getDestAddr(), null, msg.getBody(), pi, null);
我的问题是,怎么判断该短信是否发送成功。是不是用一个broadcast来监听呢?
是不是发送成功了才广播?
那怎么判断短信发送失败了呢?就是说,我调用了sendTextMessage方法后,怎么判断短信是发送成功了还是发送失败了?
谢谢。
解决方案 »
- 现在android开发在XML布局时能直接拖控件改属性吗?
- android 远程连接电脑桌面原理,有谁知道和我说下
- "VPad-Q71" 几个字符在 android img 文件中的顺序是怎么样?
- sdcard上的文件如何转换成ContentProvider格式供其它功能使用?
- android上的webview缩放问题!急求解答!!
- android listview 分隔线 显示不全 ---- 求正确解决方法
- 联网时获取响应码时卡住该怎么办
- android-ui页面设计工具
- Android libusb.so是干嘛用的
- retrofit2的http请求Android 9.0 报错
- 弹出对话框问题
- android:如何开发一个带有闹钟提醒功能的便签
首先给你说文档里面明明就给你说了,哈哈
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 传递携带一些其它你想要的数据
我完善一下,给出代码如下:
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);
}
}
中国移动有delivery
中国联通没有