2012-07-21 63 views
8

我想獲取短信發送報告我正在嘗試很多示例,但是,一個流程不顯示發送報告,就好像當我的代碼中發送短信時不顯示短信發送報告失敗,否則顯示報告喜歡沒有服務等。當我的餘額爲0時,我想查找短信發送失敗的送達報告。Android中的短信發送報告

幫我用任何代碼!

在此先感謝!

+0

如果您想要人幫助您,您需要接受答案,FYI – 2012-09-24 19:25:23

回答

36
BroadcastReceiver sendBroadcastReceiver = new SentReceiver(); 
    BroadcastReceiver deliveryBroadcastReciever = new DeliverReceiver();; 

    @Override 
    protected void onPause() { 
     // TODO Auto-generated method stub 
     super.onPause(); 

     try { 
      unregisterReceiver(sendBroadcastReceiver); 
      unregisterReceiver(deliveryBroadcastReciever); 
     } catch (Exception e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    } 
@Override 
protected void onDestroy() { 
    // TODO Auto-generated method stub 
    super.onDestroy(); 
    try { 
     unregisterReceiver(sendBroadcastReceiver); 
     unregisterReceiver(deliveryBroadcastReciever); 
    } catch (Exception e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
} 

    send_sms.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       if (textView.getText().toString().equals("") 
         | textView.getText().toString().equals(null)) { 
        Toast.makeText(SendSMS.this, enter_ph_no, Toast.LENGTH_LONG) 
          .show(); 
       } else { 


         sendSMS(textView.getText().toString(), "sms_content"); 
         finish(); 


       } 

      } 
     }); 
private void sendSMS(String phoneNumber, String message) { 
     String SENT = "SMS_SENT"; 
     String DELIVERED = "SMS_DELIVERED"; 

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

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

     registerReceiver(sendBroadcastReceiver, new IntentFilter(SENT)); 

     registerReceiver(deliveryBroadcastReciever, new IntentFilter(DELIVERED)); 
     SmsManager sms = SmsManager.getDefault(); 
     sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI); 


    } 

    class DeliverReceiver extends BroadcastReceiver { 
     @Override 
     public void onReceive(Context context, 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; 
      } 

     } 
    } 

    class SentReceiver extends BroadcastReceiver { 
     @Override 
     public void onReceive(Context context, Intent arg1) { 
      switch (getResultCode()) { 
      case Activity.RESULT_OK: 
       Toast.makeText(getBaseContext(), sms_sent, Toast.LENGTH_SHORT) 
         .show(); 
       startActivity(new Intent(SendSMS.this, ChooseOption.class)); 
       overridePendingTransition(R.anim.animation, R.anim.animation2); 
       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; 
      } 

     } 
    } 
+0

酷!這個對我有用。 謝謝 – 2013-07-21 08:56:36

+0

如何unregisterReceiver()?我成爲這樣一個exachtion?有任何想法嗎? – 2013-08-28 11:46:10

+0

使用更新的答案 – Manikandan 2013-08-28 14:40:51