我需要在我的Android应用程序中实现一个函数来发送Sms消息。我找到了许多关于此的教程,但无法获得交付状态(失败或确定)。以下是我的短信方式。
private void sendSmsMessageWithStatus(String phoneNumber, String Msg)
{
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);
registerReceiver(new BroadcastReceiver()
{
@Override
public void onReceive(Context context, Intent intent)
{
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));
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(phoneNumber, null, Msg, sentPI, deliveredPI);
}
我正在使用我的google nexus 4手机查看并正确传送短信。
我收到消息“短信发送”消息,但即使我将成功的消息发送到其他号码,也从未收到“短信已发送”消息。
当我使用模拟器检查时也是如此(显示“短信发送”消息但未发送状态消息“未发送短信”)。
在清单文件中,我添加了权限
<uses-permission android:name="android.permission.RECEIVE_SMS" />
另外我的开发SDK版本如下。
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
如果没有获得交付状态,nayone是否知道我做错了什么。根据我的代码“短信发送”或“短信未发送”消息。在此先感谢!!!
EDITS
此外,短信发送权限已添加到清单中。错过提及有问题...
<uses-permission android:name="android.permission.SEND_SMS" />
答案 0 :(得分:0)
将此权限添加到您的清单:
<uses-permission android:name="android.permission.SEND_SMS"/>