2014-01-28 200 views
0

我有以下代碼發送長的短信。爲什麼我沒有收到廣播?

正如你可以在代碼中看到我截斷了長時間的測試零​​件和 我與SmsManager

提供(發送短信集和SMS)PendingIntents的ArrayList發送的部分。

我爲PendingIntents註冊了兩個BroadcastReceivers。

的問題是,BroadcastReceivers似乎什麼也得不到,因此他們離開了那裏掛着,等着那顯然從未到達廣播...

public void sendViaSms(String nexmoNumber, String message) { 
    initSmsReceivers(); 

    SmsManager sms = SmsManager.getDefault(); 
      //CREATE THE PARTS OF THE SMS 
    ArrayList<String> parts =sms.divideMessage(message); 
    int numParts = parts.size(); 
    ArrayList<PendingIntent> sentIntents = new ArrayList<PendingIntent>(); 
    ArrayList<PendingIntent> deliveryIntents = new ArrayList<PendingIntent>(); 
    //POPULATE THE ARRAY OF PENDINGINTENTS 
      for (int i = 0; i < numParts; i++) { 
    sentIntents.add(PendingIntent.getBroadcast(MessageCompose.this, 0, new Intent(SENT), 0)); 
    deliveryIntents.add(PendingIntent.getBroadcast(MessageCompose.this, 0, new Intent(DELIVERED), 0)); 
    } 
      //SEND THE SMS IN PARTS WITH THE ARRAY OF PENDINGINTENTS 
    sms.sendMultipartTextMessage(nexmoNumber,null, parts, sentIntents, deliveryIntents); 
} 

    //CREATE AND REGISTER THE BROADCASTRECEIVERS 
private void initSmsReceivers() { 
    if (receiversInitialized) { 
     return; 
    } 

    receiversInitialized = true; 

    sendBroadcastReceiver = new BroadcastReceiver() { 

     public void onReceive(Context contex, Intent intent) { 
      switch (getResultCode()) { 
      case SmsManager.RESULT_ERROR_GENERIC_FAILURE: 
      case SmsManager.RESULT_ERROR_NO_SERVICE: 
      case SmsManager.RESULT_ERROR_NULL_PDU: 
      case SmsManager.RESULT_ERROR_RADIO_OFF: 
       progressDialog.cancel(); 
       showMessage(R.string.error, R.string.email_send_failed); 
       break; 
      } 
     } 
    }; 

    deliveryBroadcastReceiver = new BroadcastReceiver() { 
     @Override 
     public void onReceive(Context contex, Intent intent) { 
      if (!progressDialog.isShowing()) { 
       return; 
      } 

      switch (getResultCode()) { 
      case Activity.RESULT_OK: 
       showSuccessDialog(); 
       break; 
      default: 
       showMessage(R.string.error, R.string.email_send_failed); 
       break; 
      } 

      progressDialog.cancel(); 


     } 
    }; 

    MessageCompose.this.registerReceiver(deliveryBroadcastReceiver, 
      new IntentFilter(DELIVERED)); 
    MessageCompose.this.registerReceiver(sendBroadcastReceiver, 
      new IntentFilter(SENT)); 

} 
+0

您是否已經在收音機中定義了您的收音機? –

+0

確保您有權限 – betorcs

回答

1

你有權限在你的清單中聲明? (使用撥出的)

 <uses-permission android:name="android.permission.RECEIVE_SMS" /> 
     <uses-permission android:name="android.permission.READ_SMS" /> 
     <uses-permission android:name="android.permission.SEND_SMS" />