2013-05-12 4 views
0

앱에 내 앱 보내기 SMS는 전화 번호 목록을 반복하고 SMSManager.sendMultipartTextMessage()를 사용하는 꽤 표준적인 루프를 가지고 있습니다.안드로이드에서 한 번에 하나씩 SMS를 보내는 방법

BroadcastReceiver (고유 한 PendingIntent가있는 메시지 파트 당 하나, 수신자가 해당 인 텐트를 필터링 함)는 모든 실패를 기록하는 데 사용됩니다.

나를 위해 다시 만들 때 문제가 발생하지 않지만 내 사용자 중 일부가 여러 가지 일반적인 오류 (SmsManager.RESULT_ERROR_GENERIC_FAILURE)를 얻고 있음을 알고 있습니다.

저는 휴대 전화가 고속으로 여러 SMS를 처리 할 수 ​​없다고 생각합니다. 내 루프에 지연 (좋은 해결책이 아닙니다)을 넣더라도 SMS가 여전히 전화 상에 쌓여 갑자기 홍수로 빠져서 일반적인 오류가 발생한다고 생각합니다.

많은 사람들이이 문제를 겪고 있지만 매우 간단한 경우 이외의 다른 유용한 솔루션을 본적이 없습니다 (오류 흐름 없음 등).

나는 각 BroadcastReceiver.onReceive()가 (청취자를 효과적으로)보고 할 때까지 기다린 무한 루프를 생각했지만 무한 루프에 대한 아이디어는 싫어하고 모든 오류 흐름을 관리하려고 시도했다.

한 번에 하나씩 SMS 처리를 보장 할 수있는 방법에 대한 아이디어가 있습니까?

답변

1

메시지의 상태 (일반 오류/라디오 꺼짐/null PDU/SMS 전달됨/배달되지 않은 SMS)를 기다리려면 보류중인 인 텐트를 사용하십시오.

SmsManager smsManager=SmsManager.getDefault(); 
String SENT = "SMS_SENT"; 
String DELIVERED = "SMS_DELIVERED";  
public void sendSms(String phoneNumber,String sms){ 

PendingIntent sentPI = PendingIntent.getBroadcast(this, 0, 
      new Intent(SENT), 0); 

     PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0, 
      new Intent(DELIVERED), 0); 

//---when the SMS has been sent--- 
    registerReceiver(new BroadcastReceiver(){ 
     @Override 
     public void onReceive(Context arg0, Intent arg1) { 
      switch (getResultCode()) 
      { 
       case Activity.RESULT_OK: 
        // do what you need to do if OK 
        break; 
       case SmsManager.RESULT_ERROR_GENERIC_FAILURE: 
        // do what you need to do if GENERIC FAILURE 
        break; 
       case SmsManager.RESULT_ERROR_NO_SERVICE: 
        // do what you need to do if no Service 
        break; 
       case SmsManager.RESULT_ERROR_NULL_PDU: 

        break; 
       case SmsManager.RESULT_ERROR_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: 
        // Send next sms when previous one delivered your choice 

        break; 
       case Activity.RESULT_CANCELED: 
        //Do something if not delivered 
        break;       
      } 
     } 
    }, new IntentFilter(DELIVERED)); 


    smsManager.sendTextMessage(phoneNumber, null, sms, sentPI, deliveredPI); 

} 
+0

답을 주셔서 감사하지만 어떻게 그게 스로틀 만드는가? onReceive()에서 sendSMS()를 호출하고 싶지 않습니다. 그리고 만약 그렇다면 SMSManager.sendMultipartTextMessage()에 의해 예외가 던져지는 인스턴스를 다루지 않습니다. – LakesDeveloper

+0

sms가 successfuly로 보내 졌을 때 단순히 부울 값을 true로 설정하고 그 바운 튼에서 while 루프를 만듭니다. – Neo

+0

무한 루프를 피하려고했으나 시도해 보겠습니다. 확실히 솔루션이 작동해야하는 것처럼 들립니다. – LakesDeveloper

관련 문제