2017-01-26 132 views
1

我正在嘗試爲Android創建一個簡單的SMS庫,併爲其創建了一些自定義監聽器。我想在事件發生時觸發該監聽器,例如。短信發送或失敗。我能夠發送和接收短信,但監聽器事件沒有被調用,因爲我的監聽器總是返回空值。有人可以告訴我我在哪裏做錯了嗎?回調監聽器總是返回null

public class SendSMS { 

    private OnSMSListener listener; 

    public void OnSMSListener(OnSMSListener listener) { 
     this.listener = listener; 
    } 

    public SendSMS(Context context) { 
     Holder.setContext(context); 
     this.listener = null; 

    } 


    public void setNumber(String number) { 
     Holder.setMobNumber(number); 

    } 

    public void setMessage(String message) { 
     Holder.setMessage(message); 
    } 

    public void sendMessage() { 
     ImplSendSms.sendTextMessage(listener); 
    } 

    /** 
    * Interface definition for a callback to be invoked 
    */ 
    public interface OnSMSListener { 

     void onSmsSent(); 

     void onGenericFailure(); 

     void onNoService(); 

     void onNullPdu(); 

     void onRadioOff(); 
    } 

MyImplSendSms.class文件:

public class ImplSendSms { 

    public static void sendTextMessage(final SendSMS.OnSMSListener listener) { 

     Toast.makeText(Holder.getContext(), "sendTextMessage", Toast.LENGTH_SHORT).show(); 

     String SENT = "SMS_SENT"; 
     PendingIntent sentPI = PendingIntent.getBroadcast(Holder.getContext(), 0, 
       new Intent(SENT), 0); 

//---when the SMS has been sent--- 
     Holder.getContext().registerReceiver(new BroadcastReceiver() { 
      @Override 
      public void onReceive(Context arg0, Intent arg1) { 
       switch (getResultCode()) { 
        case Activity.RESULT_OK: 
         Toast.makeText(Holder.getContext(), "Inside Sent", Toast.LENGTH_SHORT).show(); 
         if (listener != null) { 
          Toast.makeText(Holder.getContext(), "Inside Sent Listener", Toast.LENGTH_SHORT).show(); 
          listener.onSmsSent(); 
         } 

         break; 
        case SmsManager.RESULT_ERROR_GENERIC_FAILURE: 
         if (listener != null) { 
          listener.onGenericFailure(); 
         } 

         break; 
        case SmsManager.RESULT_ERROR_NO_SERVICE: 
         if (listener != null) { 
          listener.onNoService(); 
         } 
         break; 
        case SmsManager.RESULT_ERROR_NULL_PDU: 
         if (listener != null) { 
          listener.onNullPdu(); 
         } 
         break; 
        case SmsManager.RESULT_ERROR_RADIO_OFF: 
         if (listener != null) { 
          listener.onRadioOff(); 
         } 

         break; 
       } 
      } 
     }, new IntentFilter(SENT)); 


     SmsManager sms = SmsManager.getDefault(); 


     sms.sendTextMessage(Holder.getMobNumber(), null, Holder.getMessage(), sentPI, null); 
    } 

我的MainActivity

SendSMS sms = new SendSMS(MainActivity.this); 
     sms.setMessage("Hello"); 
     sms.setNumber("123456789"); 
     sms.sendMessage(); 
     sms.OnSMSListener(new SendSMS.OnSMSListener() { 
      @Override 
      public void onSmsSent() { 
       Toast.makeText(MainActivity.this, "SMS Sent", Toast.LENGTH_SHORT).show(); 
      } 

      @Override 
      public void onGenericFailure() { 
       Toast.makeText(MainActivity.this, "Generic Failure", Toast.LENGTH_SHORT).show(); 
      } 

      @Override 
      public void onNoService() { 
       Toast.makeText(MainActivity.this, "No Service", Toast.LENGTH_SHORT).show(); 
      } 

      @Override 
      public void onNullPdu() { 
       Toast.makeText(MainActivity.this, "Null PDU", Toast.LENGTH_SHORT).show(); 
      } 

      @Override 
      public void onRadioOff() { 
       Toast.makeText(MainActivity.this, "Radio Off", Toast.LENGTH_SHORT).show(); 
      } 
     }); 
+0

你在哪裏註冊你的聽衆? –

+1

當你調用'sms.sendMessage()'時,'SendSMS'中的listener'爲空,因爲你還沒有調用'OnSMSListener()',所以null在'ImplSendSms.sendTextMessage(listener)'中傳遞。 –

+1

請將您的'OnSMSListener' setter重命名爲'setOnSMSListener'類似的東西 – StuStirling

回答

0

要設置監聽你叫sendMessage()後。更改爲以下內容:

sms.OnSMSListener(new SendSMS.OnSMSListener() { 
     @Override 
     public void onSmsSent() { 
      Toast.makeText(MainActivity.this, "SMS Sent", Toast.LENGTH_SHORT).show(); 
     } 

     @Override 
     public void onGenericFailure() { 
      Toast.makeText(MainActivity.this, "Generic Failure", Toast.LENGTH_SHORT).show(); 
     } 

     @Override 
     public void onNoService() { 
      Toast.makeText(MainActivity.this, "No Service", Toast.LENGTH_SHORT).show(); 
     } 

     @Override 
     public void onNullPdu() { 
      Toast.makeText(MainActivity.this, "Null PDU", Toast.LENGTH_SHORT).show(); 
     } 

     @Override 
     public void onRadioOff() { 
      Toast.makeText(MainActivity.this, "Radio Off", Toast.LENGTH_SHORT).show(); 
     } 
    }); 
    sms.sendMessage(); 

PS。您的OnSMSListener應該是setOnSMSListener()

+0

是的感謝這個catch現在可以正常工作,但是我怎麼能限制用戶先使用sendMessage()然後實現監聽器 – Aamir

+0

在'setMessage(OnSMSListener smsListener)'或'SendSMS'類的構造函數中。 – StuStirling