4

我創建了一個應用程序,它具有發送短信的模塊。我使用2廣播接收器和待定的意圖,一個爲短信發送確認,另一個爲交付.. 短信發送廣播接收器工作正常,但交付不來。Android短信傳送廣播接收器不能在模擬器中工作

我在服務中使用以下代碼。

 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--- is working alright 
      registerReceiver(new BroadcastReceiver() 
      { 
       public void onReceive(Context arg0, Intent arg1) 
       { 
        switch (getResultCode()) 
        { 
         case Activity.RESULT_OK: 
          Toast.makeText(getBaseContext(), "SMS sent", 
            Toast.LENGTH_SHORT).show(); 

          break; 
         case SmsManager.RESULT_ERROR_GENERIC_FAILURE: 
          Toast.makeText(getBaseContext(), "Generic failure", 
            Toast.LENGTH_SHORT).show(); 
          break; 
         case SmsManager.RESULT_ERROR_NO_SERVICE: 
          Toast.makeText(getBaseContext(), "No service", 
            Toast.LENGTH_SHORT).show(); 
          break; 
         case SmsManager.RESULT_ERROR_NULL_PDU: 
          Toast.makeText(getBaseContext(), "Null PDU", 
            Toast.LENGTH_SHORT).show(); 
          break; 
         case SmsManager.RESULT_ERROR_RADIO_OFF: 
          Toast.makeText(getBaseContext(), "Radio off", 
            Toast.LENGTH_SHORT).show(); 
          break; 
        } 
        unregisterReceiver(this); 
       } 
      }, new IntentFilter(SENT)); 

      //---when the SMS has been delivered--- this part is not working 
      registerReceiver(new BroadcastReceiver() 
      { 


       @Override 
       public void onReceive(Context arg0, Intent arg1) 
       { 
        switch (getResultCode()) 
        { 
         case Activity.RESULT_OK: 
          Toast.makeText(getBaseContext(), "SMS delivered", 
            Toast.LENGTH_SHORT).show(); 
          break; 
         case Activity.RESULT_CANCELED: 
          Toast.makeText(getBaseContext(), "SMS not delivered", 
            Toast.LENGTH_SHORT).show(); 
          break;   

         default : 
          Toast.makeText(getBaseContext(), "Unable to generate delivery Report", 
            Toast.LENGTH_SHORT).show(); 
        } 
        unregisterReceiver(this); 
       } 
      }, new IntentFilter(DELIVERED));   

      SmsManager sms = SmsManager.getDefault(); 
      sms.sendTextMessage(phoneNumber, null, msg, sentPI, deliveredPI); 
+2

沒關係..它在單元格上工作...我實際上是在仿真器上測試它 – 2013-03-07 14:08:12

+0

可能的[Android:在模擬器中沒有收到SMS發送確認]的重複(http://stackoverflow.com/questions/9503585/ Android的不接收短信交付 - 認可 - 在仿真器) – Flow 2013-09-26 22:41:49

回答

5

網絡服務提供商來發送短信,就可以得到交付notification.The代碼在設備上工作,但在模擬器,所以它不包含任何服務provider.That的方式,它不會顯示在模擬器交付通知。