我开发发送短信和I'able得到回应知道,如果短信发送(或没有)的应用程序。 但因为我要确定哪些短信发送(由接收器和文本)我使用extras
这些信息添加到intent
。广播接收器发送的消息
这工作正常,但只为我发的第一条消息。在第一条消息之后,我收到了相同的附加内容,而且我似乎总是发送相同的短信。 其实每个短信都发送正确,所以问题只涉及“发送通知”。 奇怪的是,当我reiceive另一个短信它再次正常工作(我得到正确的演员),但只有一个短信。看起来,新的短信事件“重置”发送消息的广播接收器。
这是我的代码:
class SmsSender{
private final String SENT = "SMS_SENT";
private static SmsSender instance=null;
private BroadcastReceiver br;
SmsSender(){}
//SINGLETON
public static SmsSender getSmsSender(Context ctx, AddressBook ab, DBManager dbm){
if(instance==null && ctx!=null){
instance=new SmsSender();
instance.ctx=ctx;
instance.ab=ab;
instance.dbm=dbm;
instance.setBR();
}
return instance;
}
private void setBR(){
br=new BroadcastReceiver(){
public void onReceive(Context arg0, Intent arg1)
{
br=null;
switch (getResultCode())
{
case Activity.RESULT_OK:
if(arg1.hasExtra("text") && arg1.hasExtra("receiver")){
Log.i("SMS","SMS sent to "+arg1.getStringExtra("receiver")+": "+arg1.getStringExtra("text"));
}
break;
.....
}
}
};
ctx.registerReceiver(br, new IntentFilter(SENT));
}
public void send(String phoneNumber, String text) {
String nums[]=phoneNumber.split(",");
for(int i=0; i<nums.length; ++i){
if(nums[i]!="" && text!=""){
SmsManager sms = SmsManager.getDefault();
ArrayList<String> parts = sms.divideMessage(text);
messageCount = parts.size();
ArrayList<PendingIntent> sentIntents = new ArrayList<PendingIntent>(messageCount);
Intent myIntent=new Intent(SENT);
**myIntent.putExtra("receiver", nums[i]);
myIntent.putExtra("text", text);**
for (int j = 0; j < messageCount; j++){
sentIntents.add(PendingIntent.getBroadcast(ctx, 0, myIntent,0));
}
sms.sendMultipartTextMessage(nums[i].trim(), null, parts, sentIntents, null);
}else{
Notifica.notify(ab.getContactName(nums[i])+": Error sending SMS",ctx);
}}
}}
有什么不对?
你多次调用传递不同文字'发送()',并得到了错误的文字后面或将邮件发送到多个接收者和意图始终第一个数字? – zapl 2012-04-29 12:17:04
第一个... – supergiox 2012-04-29 12:19:52
我在代码中看不到任何错误。也许这是Android中的一个错误。 – zapl 2012-04-29 12:42:35