2017-12-13 3 views
1

내 앱이 지오 펜싱에 관한 것입니다. 지오 펜스가 발생하면 SMS를 보내야합니다.인 텐트 서비스에서 SMS 보내기 - SMS 수신 여부를 확인하기 위해 브로드 캐스트를 수신 할 수 없습니다.

거의 완벽하게 작동하지만 SMS를 전송했는지 확인하기 위해 브로드 캐스트를 수신 할 수 없습니다. 나는 자신을 설명 할 것입니다 :

사용자가 지오 펜스를 입력하면 지오 펜스 인 텐트 서비스가 호출되고 SMS를 보냅니다. 그것은 매력처럼 작동하지만 SMS가 전송되었거나 문제가 발생했는지 (즉, 신호가 없음) 다시 보내려고하는지 확인해야합니다.

그래서 먼저 내가 SMS를 이런 식으로 보내려고했다 :

private void sendsms() { 

     String SMS_SENT = "SMS_SENT"; 
     final PendingIntent sentPendingIntent = PendingIntent.getBroadcast(this, 0, new Intent(), PendingIntent.FLAG_UPDATE_CURRENT); 

// check if SMS has been sent 
     smsSentReceiver =new BroadcastReceiver() { 
      @Override 
      public void onReceive(Context context, Intent intent) { 
       switch (getResultCode()) { 
        case Activity.RESULT_OK: 
         Log.e("SMS", "SMS sent successfully"); 
         break; 
        case SmsManager.RESULT_ERROR_GENERIC_FAILURE: 
         Log.e("SMS","Generic failure cause"); 
         break; 
        case SmsManager.RESULT_ERROR_NO_SERVICE: 
         Log.e("SMS", "Service is currently unavailable"); 
         break; 
        case SmsManager.RESULT_ERROR_NULL_PDU: 
         Log.e("SMS", "No pdu provided"); 
         break; 
        case SmsManager.RESULT_ERROR_RADIO_OFF: 
         Log.e("SMS", "Radio was explicitly turned off"); 
         break; 
       } 
      } 
     }; 


     registerReceiver(smsSentReceiver, new Intent()); 

     // Get the default instance of SmsManager 
     // Send a text based SMS 
     SmsManager smsMgr = SmsManager.getDefault(); 
     smsMgr.sendTextMessage(towhom, null, customtxt, sentPendingIntent, null); 

이 작동하지만, 아무것도 SMS를 전송 한 후 발생도없고, 로그 전혀. 그런 다음

나는 수신기를 만들고 매니페스트에 퍼팅이 다른 방법을 시도 :

private smsSentReceiver smsSentReceiver; 

private void sendsms() { 

     final PendingIntent sentPendingIntent = PendingIntent.getBroadcast(this, 0, new Intent(), PendingIntent.FLAG_UPDATE_CURRENT); 

     smsSentReceiver = new smsSentReceiver(); 
     registerReceiver(smsSentReceiver, new IntentFilter()); 

     // Get the default instance of SmsManager 
     // Send a text based SMS 
     SmsManager smsMgr = SmsManager.getDefault(); 
     smsMgr.sendTextMessage(towhom, null, customtxt, sentPendingIntent, null); 

그리고 이것은 smsSentReceiver.java

public class smsSentReceiver extends BroadcastReceiver { 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     switch (getResultCode()) { 
      case Activity.RESULT_OK: 
       Log.e("SMS", "SMS sent successfully"); 
       break; 
      case SmsManager.RESULT_ERROR_GENERIC_FAILURE: 
       Log.e("SMS","Generic failure cause"); 
       break; 
      case SmsManager.RESULT_ERROR_NO_SERVICE: 
       Log.e("SMS", "Service is currently unavailable"); 
       break; 
      case SmsManager.RESULT_ERROR_NULL_PDU: 
       Log.e("SMS", "No pdu provided"); 
       break; 
      case SmsManager.RESULT_ERROR_RADIO_OFF: 
       Log.e("SMS", "Radio was explicitly turned off"); 
       break; 
     } 
    } 
} 

그리고 매니페스트입니다 :

<receiver 
      android:name=".smsSentReceiver" 
      android:enabled="true" 
      > 

     </receiver> 

SMS 상태에 대한 로그가 전혀 표시되지 않습니다 ...

SMS 상태가 표시되지 않는 이유에 대한 아이디어가 있으십니까?

PS : 전용 에뮬레이터에서 테스트 한

편집 : 나는 시도했다

마지막 것은 @ 마이크 M.의 의견에 따라, 이것이다 :

private void sendsms() { 

     final PendingIntent sentPendingIntent = PendingIntent.getBroadcast(this, 0, new Intent(this, smsSentReceiver.class), PendingIntent.FLAG_CANCEL_CURRENT); 

SmsManager smsMgr = SmsManager.getDefault(); 
     smsMgr.sendTextMessage(towhom, null, customtxt, sentPendingIntent, null); 

그리고 아직 아무것도 ..

+1

'onHandleIntent()'가 끝나자 마자'IntentService'가 멈추기 때문에 당신은 동적으로 등록 된 수신기에서 브로드 캐스트를 얻지 못할 것입니다. 그러나, 당신은 현재'new Intent()'와 같은 두 가지 시도 모두에서 동일한 문제를 가지고 있습니다. 그건 아무데도 안가.매니페스트에 ''요소가 올바르게 지정 되었다면 정적으로 등록 된 요소는 'new Intent (this, smsSentReceiver.class)'로 변경하면 작동합니다. 스 니펫에있는'smsSentReceiver' 인스턴스와'registerReceiver()'호출은 정적으로 등록 된 클래스에는 불필요하기 때문에 제거 할 수 있습니다. –

+0

* sms를 보낸 후에 아무 것도 일어나지 않으며 로그가 전혀 없습니다. * 코드를 공유하지 않았으므로 확신 할 수 없습니다. 그러나 이벤트가 전송 될 때까지 귀하의 의도가 이미 완료되어 중단되었다고 생각합니다. –

+0

@MikeM. 나는 네가 말한 모자를 시험해 보았다. 나는 이것을 바꿨다 : final PendingIntent sentPendingIntent = PendingIntent.getBroadcast (0, 새로운 인 텐트 (this, smsSentReceiver.class), PendingIntent.FLAG_CANCEL_CURRENT); 여전히 작동하지 않습니다 ... – algarrobo

답변

1

IntentService은 작업이 끝나면 자동으로 중지되며 동적으로 등록 된 수신기도 사라집니다. 귀하의 첫 번째 스 니펫에서, 수신자는 sendTextMessage() 전화가 즉시 반환되고 PendingIntent이 비동기 적으로 실행되므로 최종적으로 방송을 수신 할 가능성이 없습니다.

두 번째 스 니펫 에서처럼 Receiver 클래스를 정적으로 등록하면 IntentService이 사망 한 후에도 Receiver가 브로드 캐스트를 수신 할 수 있습니다. 결과 Receiver 인스턴스는 그에 연결되지 않습니다. 그러나 PendingIntent에 사용한 Intent은 수신기 클래스를 올바르게 타겟팅하지 않습니다. 이것을 new Intent(this, smsSentReceiver.class)으로 변경하십시오.

마지막으로 logs with certain tags are suppressed, 어떤 이유로 든 "SMS" 태그 (정확하게) 중 하나입니다. 해당 태그를 여기 나열되지 않은 것으로 변경하십시오. 예 : "smsSentReceiver".