Tecnologia do Blogger.
RSS

[androidbrasil-dev] Re: SMS em lote com retorno de status

Problema solucionado, era pura noobagem minha, o código que eu postei
aqui foi todo baseado no de um livro que tenho e eu me prendi a este
código, achando que as duas strings "SMS_SENT" e "SMS_DELIVERED" eram
contantes que não poderia ser mudadas, quando na verdade elas
representam um código.
Dessa forma, cada vez que meu sendSms é chamado ao invés de eu ter
sempre as duas strings "SMS_SENT" e "SMS_DELIVERED", eu crio uma nova
intenção com o ID do SMS que preciso enviar, não precisei nem tirar o
escutador de dentro do método sendSms.
Segue abaixo o método sendSms com a solução:
public void sendSms(final ConfiguracaoPendente cp){
PendingIntent enviadoPI = PendingIntent.getBroadcast(this, 0, new
Intent(""+cp.getIdConfiguracaoRastreador()), 0); //Cria um ID que será
usado como parâmetro pelo escutador que será criado mais a frente
PendingIntent entreguePI = PendingIntent.getBroadcast(this, 0, new
Intent(""+cp.getIdConfiguracaoRastreador()), 0); //Cria um ID que será
usado como parâmetro pelo escutador que será criado mais a frente

//--quando o sms tiver sido enviado--
registerReceiver(new BroadcastReceiver() {
@SuppressWarnings("deprecation")
@Override
public void onReceive(Context context, Intent intent) {
final PreferenciasGateway p = new
PreferenciasGatewayDAO().getPreferenciasGateway(prefs);
SoapObject request2 = new SoapObject(p.getNamespace(),
MetodosWebService.ATUALIZAR_ST_ENVIO);
PropertyInfo propInfo21 = new PropertyInfo();
PropertyInfo propInfo22 = new PropertyInfo();
PropertyInfo propInfo23 = new PropertyInfo();
PropertyInfo propInfo24 = new PropertyInfo();

SoapSerializationEnvelope envelope2 = new
SoapSerializationEnvelope(SoapEnvelope.VER11);
HttpTransportSE androidHttpTransport2 = new
HttpTransportSE(p.getUrl());

switch(getResultCode()){
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
propInfo21.name = MetodosWebService.ATUALIZAR_ST_ENVIO__SENHA_WS;
propInfo21.type = PropertyInfo.STRING_CLASS;
request2.addProperty(propInfo21, p.getSenhaWebService());

propInfo22.name =
MetodosWebService.ATUALIZAR_ST_ENVIO__ID_CONFIGURACAO_RASTREADOR;
propInfo22.type = PropertyInfo.STRING_CLASS;
request2.addProperty(propInfo22,
cp.getIdConfiguracaoRastreador());

propInfo23.name = MetodosWebService.ATUALIZAR_ST_ENVIO__ST_ENVIO;
propInfo23.type = PropertyInfo.STRING_CLASS;
request2.addProperty(propInfo23, StEnvioTP.FALHA_GENERICA);

propInfo24.name =
MetodosWebService.ATUALIZAR_ST_ENVIO__NR_TENTATIVA;
propInfo24.type = PropertyInfo.STRING_CLASS;
request2.addProperty(propInfo24, cp.getNrTentativa()+1);


envelope2.setOutputSoapObject(request2);
try {
androidHttpTransport2.call(p.getNamespace()+p.getSoapAction()
+"/"+MetodosWebService.ATUALIZAR_ST_ENVIO
+MetodosWebService.REQUISICAO, envelope2);
envelope2.getResponse();
}catch(Exception e){
e.printStackTrace();
}
break;
case SmsManager.RESULT_ERROR_NO_SERVICE:
propInfo21.name = MetodosWebService.ATUALIZAR_ST_ENVIO__SENHA_WS;
propInfo21.type = PropertyInfo.STRING_CLASS;
request2.addProperty(propInfo21, p.getSenhaWebService());

propInfo22.name =
MetodosWebService.ATUALIZAR_ST_ENVIO__ID_CONFIGURACAO_RASTREADOR;
propInfo22.type = PropertyInfo.STRING_CLASS;
request2.addProperty(propInfo22,
cp.getIdConfiguracaoRastreador());

propInfo23.name = MetodosWebService.ATUALIZAR_ST_ENVIO__ST_ENVIO;
propInfo23.type = PropertyInfo.STRING_CLASS;
request2.addProperty(propInfo23, StEnvioTP.SEM_SERVICO);

propInfo24.name =
MetodosWebService.ATUALIZAR_ST_ENVIO__NR_TENTATIVA;
propInfo24.type = PropertyInfo.STRING_CLASS;
request2.addProperty(propInfo24, cp.getNrTentativa()+1);


envelope2.setOutputSoapObject(request2);
try {
androidHttpTransport2.call(p.getNamespace()+p.getSoapAction()
+"/"+MetodosWebService.ATUALIZAR_ST_ENVIO
+MetodosWebService.REQUISICAO, envelope2);
envelope2.getResponse();
}catch(Exception e){
e.printStackTrace();
}
break;
case SmsManager.RESULT_ERROR_NULL_PDU:
propInfo21.name = MetodosWebService.ATUALIZAR_ST_ENVIO__SENHA_WS;
propInfo21.type = PropertyInfo.STRING_CLASS;
request2.addProperty(propInfo21, p.getSenhaWebService());

propInfo22.name =
MetodosWebService.ATUALIZAR_ST_ENVIO__ID_CONFIGURACAO_RASTREADOR;
propInfo22.type = PropertyInfo.STRING_CLASS;
request2.addProperty(propInfo22,
cp.getIdConfiguracaoRastreador());

propInfo23.name = MetodosWebService.ATUALIZAR_ST_ENVIO__ST_ENVIO;
propInfo23.type = PropertyInfo.STRING_CLASS;
request2.addProperty(propInfo23, StEnvioTP.PDU_NULO);

propInfo24.name =
MetodosWebService.ATUALIZAR_ST_ENVIO__NR_TENTATIVA;
propInfo24.type = PropertyInfo.STRING_CLASS;
request2.addProperty(propInfo24, cp.getNrTentativa()+1);


envelope2.setOutputSoapObject(request2);
try {
androidHttpTransport2.call(p.getNamespace()+p.getSoapAction()
+"/"+MetodosWebService.ATUALIZAR_ST_ENVIO
+MetodosWebService.REQUISICAO, envelope2);
envelope2.getResponse();
}catch(Exception e){
e.printStackTrace();
}
break;
case SmsManager.RESULT_ERROR_RADIO_OFF:
propInfo21.name = MetodosWebService.ATUALIZAR_ST_ENVIO__SENHA_WS;
propInfo21.type = PropertyInfo.STRING_CLASS;
request2.addProperty(propInfo21, p.getSenhaWebService());

propInfo22.name =
MetodosWebService.ATUALIZAR_ST_ENVIO__ID_CONFIGURACAO_RASTREADOR;
propInfo22.type = PropertyInfo.STRING_CLASS;
request2.addProperty(propInfo22,
cp.getIdConfiguracaoRastreador());

propInfo23.name = MetodosWebService.ATUALIZAR_ST_ENVIO__ST_ENVIO;
propInfo23.type = PropertyInfo.STRING_CLASS;
request2.addProperty(propInfo23, StEnvioTP.RADIO_OFF);

propInfo24.name =
MetodosWebService.ATUALIZAR_ST_ENVIO__NR_TENTATIVA;
propInfo24.type = PropertyInfo.STRING_CLASS;
request2.addProperty(propInfo24, cp.getNrTentativa()+1);


envelope2.setOutputSoapObject(request2);
try {
androidHttpTransport2.call(p.getNamespace()+p.getSoapAction()
+"/"+MetodosWebService.ATUALIZAR_ST_ENVIO
+MetodosWebService.REQUISICAO, envelope2);
envelope2.getResponse();
}catch(Exception e){
e.printStackTrace();
}
break;
default:
propInfo21.name = MetodosWebService.ATUALIZAR_ST_ENVIO__SENHA_WS;
propInfo21.type = PropertyInfo.STRING_CLASS;
request2.addProperty(propInfo21, p.getSenhaWebService());

propInfo22.name =
MetodosWebService.ATUALIZAR_ST_ENVIO__ID_CONFIGURACAO_RASTREADOR;
propInfo22.type = PropertyInfo.STRING_CLASS;
request2.addProperty(propInfo22,
cp.getIdConfiguracaoRastreador());

propInfo23.name = MetodosWebService.ATUALIZAR_ST_ENVIO__ST_ENVIO;
propInfo23.type = PropertyInfo.STRING_CLASS;
request2.addProperty(propInfo23, StEnvioTP.ENVIADO);

propInfo24.name =
MetodosWebService.ATUALIZAR_ST_ENVIO__NR_TENTATIVA;
propInfo24.type = PropertyInfo.STRING_CLASS;
request2.addProperty(propInfo24, cp.getNrTentativa()+1);

envelope2.setOutputSoapObject(request2);
try {
androidHttpTransport2.call(p.getNamespace()+p.getSoapAction()
+"/"+MetodosWebService.ATUALIZAR_ST_ENVIO
+MetodosWebService.REQUISICAO, envelope2);
envelope2.getResponse();
}catch(Exception e){
e.printStackTrace();
}
break;
}
}
}, new IntentFilter(""+cp.getIdConfiguracaoRastreador())); //Usa o
mesmo ID do PendingIntent e cria um escutador somente daquela intenção

//--quando o sms tiver sido entregue--
registerReceiver(new BroadcastReceiver() {
@SuppressWarnings("deprecation")
@Override
public void onReceive(Context context, Intent intent) {
final PreferenciasGateway p = new
PreferenciasGatewayDAO().getPreferenciasGateway(prefs);
SoapObject request2 = new SoapObject(p.getNamespace(),
MetodosWebService.ATUALIZAR_ST_ENTREGA);
PropertyInfo propInfo21 = new PropertyInfo();
PropertyInfo propInfo22 = new PropertyInfo();
PropertyInfo propInfo23 = new PropertyInfo();

SoapSerializationEnvelope envelope2 = new
SoapSerializationEnvelope(SoapEnvelope.VER11);
HttpTransportSE androidHttpTransport2 = new
HttpTransportSE(p.getUrl());

switch(getResultCode()){
case Activity.RESULT_OK:
propInfo21.name =
MetodosWebService.ATUALIZAR_ST_ENTREGA__SENHA_WS;
propInfo21.type = PropertyInfo.STRING_CLASS;
request2.addProperty(propInfo21, p.getSenhaWebService());

propInfo22.name =
MetodosWebService.ATUALIZAR_ST_ENTREGA__ID_CONFIGURACAO_RASTREADOR;
propInfo22.type = PropertyInfo.STRING_CLASS;
request2.addProperty(propInfo22,
cp.getIdConfiguracaoRastreador());

propInfo23.name =
MetodosWebService.ATUALIZAR_ST_ENTREGA__ST_ENTREGA;
propInfo23.type = PropertyInfo.STRING_CLASS;
request2.addProperty(propInfo23, StEntregaTP.ENTREGUE);


envelope2.setOutputSoapObject(request2);
try {
androidHttpTransport2.call(p.getNamespace()+p.getSoapAction()
+"/"+MetodosWebService.ATUALIZAR_ST_ENTREGA
+MetodosWebService.REQUISICAO, envelope2);
envelope2.getResponse();
}catch(Exception e){
e.printStackTrace();
}
break;
case Activity.RESULT_CANCELED:
propInfo21.name =
MetodosWebService.ATUALIZAR_ST_ENTREGA__SENHA_WS;
propInfo21.type = PropertyInfo.STRING_CLASS;
request2.addProperty(propInfo21, p.getSenhaWebService());

propInfo22.name =
MetodosWebService.ATUALIZAR_ST_ENTREGA__ID_CONFIGURACAO_RASTREADOR;
propInfo22.type = PropertyInfo.STRING_CLASS;
request2.addProperty(propInfo22,
cp.getIdConfiguracaoRastreador());

propInfo23.name =
MetodosWebService.ATUALIZAR_ST_ENTREGA__ST_ENTREGA;
propInfo23.type = PropertyInfo.STRING_CLASS;
request2.addProperty(propInfo23, StEntregaTP.NAO_ENTREGUE);


envelope2.setOutputSoapObject(request2);
try {
androidHttpTransport2.call(p.getNamespace()+p.getSoapAction()
+"/"+MetodosWebService.ATUALIZAR_ST_ENTREGA
+MetodosWebService.REQUISICAO, envelope2);
envelope2.getResponse();
}catch(Exception e){
e.printStackTrace();
}
break;
}
}
}, new IntentFilter(""+cp.getIdConfiguracaoRastreador())); //Usa o
mesmo ID do PendingIntent e cria um escutador somente daquela intenção

SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(cp.getNrRastreador(), null,
cp.getTextoSms(), enviadoPI, entreguePI);
}

Vinicius, tua dica me ajudou muito, valeu...

  • Digg
  • Del.icio.us
  • StumbleUpon
  • Reddit
  • RSS

0 comentários:

Postar um comentário