gpt4 book ai didi

Android sendTextMessage 在执行时发送两条相同的消息

转载 作者:太空宇宙 更新时间:2023-11-03 12:40:16 26 4
gpt4 key购买 nike

我一直在开发一款发送 SMS 消息的应用程序。我遇到的问题是 sendTextMessage 方法发送两条内容相同的消息。我该如何解决?

这个类启动进程

public class C2DMMessageReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
//Some stuff
Log.i("C2DMMessageReceiver", "Got C2DM message");
SmsSend message = new SmsSend(context, phonenumber, line);
message.send()
}
}

发送短信的类

public class SmsSend {
SmsSend(Context tcontext, String phoneNumber, String smstext){
context = tcontext;
phone_number = phoneNumber;
message = smstext;
}

protected void send(){

if(foo){
Log.i("SmsSend", "Sending message");
SmsManager sms = SmsManager.getDefault();
String sent = "android.telephony.SmsManager.STATUS_ON_ICC_SENT";
PendingIntent piSent = PendingIntent.getBroadcast(context, 0, new Intent(sent), 0);
sms.sendTextMessage(phone_number, null, message, piSent, null);
}

}
}

上课,看看发生了什么事

public class SmsSentBroadcastReciever extends BroadcastReceiver{
private static final String TAG = "SmsSentBroadcastReciever";

@Override
public void onReceive(Context context, Intent intent) {
switch (getResultCode()){
case Activity.RESULT_OK:
Log.i(TAG,"SMS sent");
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
Log.e(TAG,"Generic failure");
break;
case SmsManager.RESULT_ERROR_NO_SERVICE:
Log.e(TAG,"No service");
break;
case SmsManager.RESULT_ERROR_NULL_PDU:
Log.e(TAG,"PDU NULL");
break;
case SmsManager.RESULT_ERROR_RADIO_OFF:
Log.e(TAG,"Radio off");
break;
}

}

}

LogCat 的输出是

收到C2DM消息

发送消息

短信已发送

短信已发送

所以 sendTextMessage 只触发一次,但它仍然抛出两条消息。怎么办?

我正在调试的设备是装有 Android 4.0 的 Samsung Galaxy S2。我读到一些旧线程,sendTextMessage 在某些 (HTC) 设备上损坏,所以我尝试使用 sendMultipartTextMessage 但它给出了相同的结果。

最佳答案

以下代码工作正常,S2 与 ICS:

void sendMessageGTI9100ICS(String number, String msg) throws Exception {
SmsManager m = SmsManager.getDefault();

Class aclass[] = new Class[9];

aclass[0] = String.class;
aclass[1] = String.class;
aclass[2] = ArrayList.class;
aclass[3] = ArrayList.class;
aclass[4] = ArrayList.class;
aclass[5] = Boolean.TYPE;
aclass[6] = Integer.TYPE;
aclass[7] = Integer.TYPE;
aclass[8] = Integer.TYPE;

Method method = m.getClass().getMethod("sendMultipartTextMessage", aclass);

Object aobj[] = new Object[9];
aobj[0] = number;
aobj[1] = null;
aobj[2] = m.divideMessage(msg);
aobj[3] = null;
aobj[4] = null;
aobj[5] = Boolean.valueOf(false);
aobj[6] = Integer.valueOf(0);
aobj[7] = Integer.valueOf(0);
aobj[8] = Integer.valueOf(0);

method.invoke(m, aobj);
}

关于Android sendTextMessage 在执行时发送两条相同的消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9706039/

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