我有一个问题,不是那个主要问题,但是因为我在我的代码上实现了sendMultipartTextMessage,所以“SMS Sent”或任何其他Toast的吐司都不会出现。如果短信成功发送,我不知道。我该怎么办?这是代码:
private void sendSMS(String phoneNo, String message, boolean split)
{
String SENT = "SMS_SENT";
String DELIVERED = "SMS_DELIVERED";
PendingIntent sentPI = PendingIntent.getBroadcast(this, 0,
new Intent(SENT), 0);
PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0,
new Intent(DELIVERED), 0);
SmsManager sms = SmsManager.getDefault();
if (!split) {
Log.d("SMSTest", "Sending single message: " + message);
} else {
Log.d("SMSTest", "Sending '" + message + "' in multiple parts.");
ArrayList<String> parts = sms.divideMessage(message);
Log.d("SMSTest", parts.size() + " parts:");
for (String string : parts) {
Log.d("SMSTest", string);
}
ArrayList<PendingIntent> sentList = new ArrayList<PendingIntent>();
sentList.add(sentPI);
ArrayList<PendingIntent> deliveredList = new ArrayList<PendingIntent>();
deliveredList.add(deliveredPI);
sms.sendMultipartTextMessage(phoneNo, null, parts, sentList,
deliveredList);
registerReceiver(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;
}
}
}, new IntentFilter(SENT));
registerReceiver(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;
}
}
}, new IntentFilter(DELIVERED));
}
}
}
答案 0 :(得分:0)
在没有默认情况下,您永远不应该实现切换。创建一个并显示getResultCode()
的值,而不是至少看到返回的值。这可以帮助您进一步调查行为。
样品:
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;
default:
Toast.makeText(getBaseContext(), "Default... " + getResultCode(), Toast.LENGTH_SHORT).show();
}