gpt4 book ai didi

android - SMS 通知的行为

转载 作者:太空狗 更新时间:2023-10-29 16:07:54 25 4
gpt4 key购买 nike

现在我正在尝试使用以下代码以编程方式发送 SMS,但我不了解 SMS SENT 接收器的行为。

1) for example if i send one SMS then Activity.RESULT_OK inside registerReceiver is being called 3 times. and if i send 3 SMS using for loopby calling sendSMS then Activity.RESULT_OK is being called 9 times. now i really don't know for one sms to send why this registerReceiver is being called so many times?

2) Moreover , when i run this code over emulator i passed emulator port to send SMS to other emulators which is natural but when i try to send SMS to a real number then i am not getting SMS delivery failure notification , as it notifies Activity.RESULT_OK only

发送短信的代码

private void sendSMS(String phoneNumber, String message)
{

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);
Log.d("SMS Service", "SMS SEND CALLED");
//---when the SMS has been sent---
registerReceiver(new BroadcastReceiver(){
@Override
public void onReceive(Context arg0, Intent arg1) {

Log.d("SMS Service", "RECEIVE CALLED");
switch (getResultCode())
{
case Activity.RESULT_OK:
Toast.makeText(SMSService.this, "SMS sent",
Toast.LENGTH_SHORT).show();
System.out.println("SMSService " + "SMS SENT");
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
Toast.makeText(SMSService.this, "Generic failure",
Toast.LENGTH_SHORT).show();
System.out.println("SMSService " + "GENERIC FAILURE");
break;
case SmsManager.RESULT_ERROR_NO_SERVICE:
Toast.makeText(SMSService.this, "No service",
Toast.LENGTH_SHORT).show();
System.out.println("SMSService " + "NO SERVICE");
break;
case SmsManager.RESULT_ERROR_NULL_PDU:
Toast.makeText(SMSService.this, "Null PDU",
Toast.LENGTH_SHORT).show();
System.out.println("SMSService " + "Null PDU");
break;
case SmsManager.RESULT_ERROR_RADIO_OFF:
Toast.makeText(SMSService.this, "Radio off",
Toast.LENGTH_SHORT).show();
System.out.println("SMSService " + "Radio Off");
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();
System.out.println("SMSService " + "SMS Delivered");
break;
case Activity.RESULT_CANCELED:
Toast.makeText(getBaseContext(), "SMS not delivered",
Toast.LENGTH_SHORT).show();
System.out.println("SMSService " + "SMS not delivered");
break;
}
}
}, new IntentFilter(DELIVERED));

SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);
}

最佳答案

一般来说,这取决于您发送的消息的大小——如果消息超过了单条消息限制(在您的情况下,这听起来像是单条消息限制的 3 倍左右),那么您将收到一个发送和消息每个部分的发送报告。因为您不是手动拆分消息,所以您无法为每个部分指定不同的 Intent 。

值得一看SmsManager.divideMessage()自己拆分消息,然后在 SmsManager.sendMultiPartTextMessage()做实际的发送。这允许您为消息的不同部分指定不同的待定 Intent ,因此您可以确定消息最终发送的时间。

我认为模拟器将所有消息目的地视为准确的,因为没有网络可以返回并说否则你可能不会从模拟器发送失败(除非你做了类似将模拟器置于飞行模式的操作).根据经验,您肯定会在真实设备上得到这些错误代码。

编辑:想一想,每次发送消息时你也在注册接收者,我使用的代码有一个 list 注册接收者。有可能你已经多次注册它(它会在你注册的上下文中存在)这会多次给你 - 这也会让它重复 3 条消息可能 9 次(假设注册第 3 次在第一次发送完成之前成功) - 可能,但我不知道可能性有多大。您可以通过在已注册的接收器中记录对象来进行相对较好的测试。

这是我用来发送短信的代码的精简版,它不会收到重复的短信回复:

ArrayList<String> split = SmsManager.getDefault().divideMessage(message);
ArrayList<PendingIntent> success = new ArrayList<PendingIntent>(partInfo.length);
Intent sendInt = null;
for (int i = 0; i < partInfo.length; i++)
{
sendInt = new Intent(context.getPackageName() + RELAY_INTERNAL_RESPONSE);
sendInt.putExtra(KEY_MESSAGEID, messageID);
sendInt.putExtra(KEY_PART_NUMBER, i);
sendInt.putExtra(KEY_REPLY_SEND_INTENT, sendIntAction);
sendInt.putExtra(KEY_NUMBER, number);
PendingIntent sendResult = PendingIntent.getBroadcast(context, i, sendInt, PendingIntent.FLAG_ONE_SHOT); //You have to use an incrementing request code to ensure you don't just get the same pending intent.
success.add(sendResult);
}
ArrayList<PendingIntent> receipt = new ArrayList<PendingIntent>(partInfo.length);
sendInt = new Intent(context.getPackageName() + RELAY_INTERNAL_RECEIPT);
sendInt.putExtra(KEY_MESSAGEID, messageID);
sendInt.putExtra(KEY_REPLY_RECEIPT_INTENT, receiptIntAction);
sendInt.putExtra(KEY_NUMBER, number);
PendingIntent sendResult = PendingIntent.getBroadcast(context, nextReceiptCounter(context), sendInt, PendingIntent.FLAG_ONE_SHOT);
for (int i = 0; i < partInfo.length; i++)
{
receipt.add(sendResult);
}
SmsManager sm = SmsManager.getDefault();
sm.sendMultipartTextMessage(target, null, split, success, receipt);

还有我的接收器定义:

<receiver android:name="<package>.SMSBroadcastModule" 
android:enabled="true"
android:exported="false">
<intent-filter>
<action android:name="<package>.RELAY_INTERNAL_RESPONSE" />
<action android:name="<package>.RELAY_INTERNAL_RESPONSE_RECEIPT" />
</intent-filter>
</receiver>

关于android - SMS 通知的行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9680004/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com