我使用http://mobiforge.com/developing/story/sms-messaging-android代码逐字示例,除了我更改了以下内容:
import android.telephony.gsm.SmsManager;至:
import android.telephony.SmsManager;SMS发送正常,但是我没有收到已发送(发送到网络)的TOAST消息。我正在尝试将SMS集成到我的应用程序中,这一点很重要。我相信这是可以做到的,因为否则股票短信应用程序如何知道何时停止显示“发送圆圈”。相关代码部分如下:
//---when the SMS has been sent---
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));
//---when the SMS has been delivered---
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)); 我使用的是Jelly Bean ROM,但我相信不久前我在姜饼和ICS上测试了相同的部分,结果相同。API是否已更改,或者示例是否有问题?我正在测试Sasmsung,如果有帮助的话。我以前的测试是在史诗上进行的。
发布于 2015-03-28 16:58:10
对于现在有这个问题的人来说,如果你在Android 4.4或更高版本上测试它,它将无法工作,除非你的应用程序是默认的短信应用程序。
从文档中看-
从Android4.4开始,系统设置允许用户选择“默认短信应用”。一旦选择,只有默认短信应用程序能够写入短信提供商,并且只有默认短信应用程序在用户接收短信时接收SMS_DELIVER_ACTION广播或当用户接收彩信时接收WAP_PUSH_DELIVER_ACTION广播。默认的短信应用程序负责在接收或发送新消息时将详细信息写入短信提供商。
参见here。
发布于 2014-10-14 03:58:03
你的代码看起来很好用。也许问题是你的运营商没有向你提供送货报告(可能有订阅服务的选项),或者你没有在手机上打开送货报告。play商店上有一个有用的应用程序,名为Delivery Reports,您可以从该应用程序中查看每条短信的状态。
https://stackoverflow.com/questions/12701254
复制相似问题