2016-03-01 57 views
0

我的短信沒有得到發送,下面是我的代碼,我已經在2個地方使用了這個代碼,在一個地方它沒有跟隨「發送消息」吐司,在其他地方顯示通用故障。請幫助。提前感謝。短信沒有發送

String phoneNo = "9712930869"; 
String message = "Hi!"; 
sendSMS(phoneNo, message); 

//---sends a SMS message to another device--- 
private void sendSMS(String phoneNumber, String message) 
{ 
    /* 
    PendingIntent pi = PendingIntent.getActivity(this, 0,new Intent(this, test.class), 0); 
     SmsManager sms = SmsManager.getDefault(); 
     sms.sendTextMessage(phoneNumber, null, message, pi, null); 
    */ 

    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); 

    //---when the SMS has been sent--- 
    registerReceiver(new BroadcastReceiver(){ 
     @Override 
     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; 
      } 
     } 
    }, new IntentFilter(SENT)); 

    //---when the SMS has been delivered--- 
    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; 
      } 
     } 
    }, new IntentFilter(DELIVERED)); 

    SmsManager sms = SmsManager.getDefault(); 
    sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI); 
} 
+0

如果要發送大量短信在一起,它會淹沒自己的手機,最好是有一定的延遲。 接下來,如果你延遲,你必須看到它不是在UI線程中完成,否則你會得到ANR。 嘗試使用處理程序,我的朋友建議這一點,我試過了,它工作正常。 至於通用問題,我不確定..名稱通用使它聽起來像它可能是一個正常的網絡錯誤。 參考http://stackoverflow.com/questions/27471155/send-sms-leads-to-generic-failure – sasikumar

回答

0

使用SMS的許可清單中:

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