我在这里有一些代码发送消息:
SmsManager sms = new SmsManager.getDefault();
sms.sendTextMessage("911", null, "HALP!", PendingIntent, null);
Developer.android说这是关于PendingIntent
:
如果不是
NULL
,则在成功发送或失败消息时广播此PendingIntent
。结果代码为Activity.RESULT_OK
表示成功,或其中一个错误:
RESULT_ERROR_GENERIC_FAILURE
RESULT_ERROR_RADIO_OFF
RESULT_ERROR_NULL_PDU
对于
RESULT_ERROR_GENERIC_FAILURE
,sentIntent
可能包含额外的“errorCode”,其中包含无线电技术特定值,通常仅对故障排除有用。基于每个应用程序的SMS控件检查
sentIntent
。如果sentIntent
为NULL
,则将针对所有未知应用程序检查呼叫者,这会导致在检查期间发送较少数量的SMS。
我的问题是:如何将PendingIntent
提供给仅显示sendTextMessage
Toast
的{{1}},说明邮件是否已发送?
谢谢。
答案 0 :(得分:7)
只需将您的PendingIntent
注册为BroadcastReceiver
,即可在邮件发送时以及将其传递给目标收件人时通知您,即可实现此目的。请按照以下步骤_
registerReceiver
将这些PendingIntent注册到相应的 BroadcastReceiver ,以便他们的 BroadcastReceiver 得到通知并做必要的事情。 我已经构建了一个简单的类来完成我上面解释过的工作。你可以通过这门课程来完全理解它_
public class SendDeliverSMSActivity extends Activity {
Button btnSendSMS;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnSendSMS = (Button) findViewById(R.id.btnSendSMS);
btnSendSMS.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Your method that send sms
sendSMS("5556", "Hi You got a message!");
}
});
}
/**
* ---sends an SMS message to another device---
*
* @param phoneNumber
* @param message
*/
private void sendSMS(String phoneNumber, String message) {
// Intent Filter Tags for SMS SEND and DELIVER
String SENT = "SMS_SENT";
String DELIVERED = "SMS_DELIVERED";
// STEP-1___
// SEND PendingIntent
PendingIntent sentPI = PendingIntent.getBroadcast(this, 0, new Intent(
SENT), 0);
// DELIVER PendingIntent
PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0,
new Intent(DELIVERED), 0);
// STEP-2___
// SEND BroadcastReceiver
BroadcastReceiver sendSMS = new BroadcastReceiver() {
@Override
public void onReceive(Context arg0, Intent arg1) {
switch (getResultCode()) {
case Activity.RESULT_OK:
Toast.makeText(getBaseContext(), "SMS sent",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
Toast.makeText(getBaseContext(), "Generic failure",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_NO_SERVICE:
Toast.makeText(getBaseContext(), "No service",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_NULL_PDU:
Toast.makeText(getBaseContext(), "Null PDU",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_RADIO_OFF:
Toast.makeText(getBaseContext(), "Radio off",
Toast.LENGTH_SHORT).show();
break;
}
}
};
// DELIVERY BroadcastReceiver
BroadcastReceiver deliverSMS = new BroadcastReceiver() {
@Override
public void onReceive(Context arg0, Intent arg1) {
switch (getResultCode()) {
case Activity.RESULT_OK:
Toast.makeText(getBaseContext(), "SMS delivered",
Toast.LENGTH_SHORT).show();
break;
case Activity.RESULT_CANCELED:
Toast.makeText(getBaseContext(), "SMS not delivered",
Toast.LENGTH_SHORT).show();
break;
}
}
};
// STEP-3___
// ---Notify when the SMS has been sent---
registerReceiver(sendSMS, new IntentFilter(SENT));
// ---Notify when the SMS has been delivered---
registerReceiver(deliverSMS, new IntentFilter(DELIVERED));
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);
}
}
我希望这会对你有所帮助! :)
答案 1 :(得分:2)
试试这个......
PendingIntent sentPI = PendingIntent.getBroadcast(this, 0, new Intent(SENT), 0);
registerReceiver(new BroadcastReceiver()
{ int resultCode = getResultCode();
switch (resultCode)
{
case Activity.RESULT_OK:
Toast.makeText(getBaseContext(), "SMS sent"+message,Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
Toast.makeText(getBaseContext(), "Generic failure",Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_NO_SERVICE:
Toast.makeText(getBaseContext(), "No service"+message,Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_NULL_PDU:
Toast.makeText(getBaseContext(), "Null PDU",Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_RADIO_OFF:
Toast.makeText(getBaseContext(), "Radio off",Toast.LENGTH_SHORT).show();
break;
}
}
}, new IntentFilter(SENT));
SmsManager smsMgr = SmsManager.getDefault();
smsMgr.sendTextMessage(send_no, null,message, sentPI, null);