我正在创建一个应用程序,它会向一个收件人发送多条短信 - 每分钟一封。这些消息包含GPS坐标,作为附加到对象的跟踪系统的一部分。
对象将进出信号,因此可能会或可能不会发送消息。我想存储这些消息并在以后发送它们。这意味着当对象下一个信号时我需要发送大量的SMS消息。然而。当我尝试发送存储的消息时遇到麻烦。有没有办法确定什么SMS消息属于什么BroadcastReceiver?知道这一点将允许我保存(以便稍后发送)任何未发送的消息,同时删除任何已发送的消息。
IE对象走出信号。每分钟检索一次GPS坐标。消息未发送,因此存储在向量中。 5分钟后,对象返回信号,发送5条消息。如果在发送这5条消息的过程中,对象会丢失我想要的信号,并且只能发送3条消息(没有特定的顺序)我想知道哪3条消息被发送,哪2条不发送。
下面是一些代码。
public class FixingmsgsActivity extends Activity {
private static final int MAX_SMS_MESSAGE_LENGTH = 160;
private static final String SMS_DELIVERED = "SMS_DELIVERED";
private static final String SMS_SENT = "SMS_SENT";
private String SendTo = "02102980174";
private String SendToFull = "+642102980174";
private Vector<String> testmsg = new Vector<String>();
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
registerReceiver(sendreceiver, new IntentFilter(SMS_SENT));
registerReceiver(deliveredreceiver, new IntentFilter(SMS_DELIVERED));
// registerReceiver(smsreceiver, new IntentFilter("android.provider.Telephony.SMS_RECEIVED"));
testmsg.addElement("one");
testmsg.addElement("two");
testmsg.addElement("three");
testmsg.addElement("four");
testmsg.addElement("five");
testmsg.addElement("six");
testmsg.addElement("seven");
testmsg.addElement("eight");
testmsg.addElement("nine");
for(int i=0;i < testmsg.size();i++){
sendSms(SendTo,testmsg.elementAt(i));
}
}
private void sendSms(String phonenumber,String message){
SmsManager manager = SmsManager.getDefault();
Intent sendIntent = new Intent("SMS_SENT");
sendIntent.putExtra("message", message);
PendingIntent piSend = PendingIntent.getBroadcast(this, 0, sendIntent, PendingIntent.FLAG_UPDATE_CURRENT);
PendingIntent piDelivered = PendingIntent.getBroadcast(this, 0, new Intent(SMS_DELIVERED), 0);
int length = message.length();
if(length > MAX_SMS_MESSAGE_LENGTH)
{
ArrayList<String> messagelist = manager.divideMessage(message);
manager.sendMultipartTextMessage(phonenumber, null, messagelist, null, null);
}
else
{
//Toast.makeText(getBaseContext(), message, Toast.LENGTH_SHORT).show();
manager.sendTextMessage(phonenumber, null, message, piSend, piDelivered);
}
}
private BroadcastReceiver sendreceiver = new BroadcastReceiver()
{
@Override
public void onReceive(Context context, Intent intent)
{
switch(getResultCode())
{
case Activity.RESULT_OK: //Message has been sent
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE: //message not sent. Which one? I would like to store this SMS so I can send it later.
break;
case SmsManager.RESULT_ERROR_NO_SERVICE: //message not sent. Which one?
break;
case SmsManager.RESULT_ERROR_NULL_PDU: //message not sent. Which one?
break;
case SmsManager.RESULT_ERROR_RADIO_OFF: //message not sent. Which one?
break;
}
}
};
private BroadcastReceiver deliveredreceiver = new BroadcastReceiver()
{
@Override
public void onReceive(Context context, Intent intent)
{
String info = "Delivery information: ";
switch(getResultCode())
{
case Activity.RESULT_OK: info += "delivered";
break;
case Activity.RESULT_CANCELED: info += "not delivered";
break;
}
}
};
}
我已尝试将Intent与putExtra一起使用,但这只会在广播接收器中提供最后一条SMS。例如在上面的代码中,onrecieve方法中的额外“消息”将输出“九”“九”“九”“九”“九”“九”“九”“九”“九”
当我发送多条消息时,我希望它类似于
“one”“two”等。这将允许我识别哪些消息未被发送以及哪一个消息
很抱歉阅读不久。谢谢 丹尼尔H
答案 0 :(得分:0)
我想我现在已经弄清楚了。
我的错误在于创建一个for循环来发送所有短信。这将创建多个接收方,或者会导致接收方丢失有关Onrecieve呼叫属于什么SMS的信息。
我做了什么......
public void sendSms(){
//messagev = vector or arraylist of messages to be sent
//So that there are no over run errors.
//IE not sending blank messages
if (messagev.isEmpty()){
return;
}
SmsManager manager = SmsManager.getDefault();
//only send the last element in the array. This will allow the latest message to be sent
//due to adding messages to messagev being appended on the end.
String message = messagev.lastElement();
PendingIntent sentPI = PendingIntent.getBroadcast(this, 0,new Intent(SMS_SENT),0);
//register a new reciever for the sms to be sent.
registerReceiver(new BroadcastReceiver(){
@Override
public void onReceive(Context arg0, Intent arg1) {
switch (getResultCode())
{
case Activity.RESULT_OK:
arg0.unregisterReceiver(this); //remove reciever for this sms
messagev.removeElementAt(messagev.size()-1); //remove the element from the message array
if (!messagev.isEmpty()){ //if there are more messages to send then cann the sendSms function again.
sendSms();
}
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
arg0.unregisterReceiver(this);
break;
case SmsManager.RESULT_ERROR_NO_SERVICE:
arg0.unregisterReceiver(this);
break;
case SmsManager.RESULT_ERROR_NULL_PDU:
arg0.unregisterReceiver(this);
break;
case SmsManager.RESULT_ERROR_RADIO_OFF:
arg0.unregisterReceiver(this);
break;
}
}
},new IntentFilter(SMS_SENT));
manager.sendTextMessage(SendTo, null, message, sentPI, null); //send the sms
}
调用此函数将通过所有要发送的消息进行迭代(通过类似于递归的方式),如果发送它将从集合中删除该短信。
如果未发送SMS,则它会取消注册接收方,并且SMS不会从SMS阵列中删除,并且会在下次调用sendSms方法时尝试发送。
该方法应该有一个默认的unregister接收器,这样如果它全部进入底池,接收器就会被取消注册。否则它似乎工作正常。 干杯 - 丹尼尔H