2013-04-04 62 views
2

發送一個很長的消息basiclly我有我與SmsManager發送客戶端之間的短信聊天應用程序,但我能夠比它可以發送較長的消息。 我讀了它只能送東西約40-60信,但我想sendm像200-400筆記 消息,因此,有沒有辦法用它做呢?如何使用SmsManager

+0

理想情況下,你不應該通過短信發送郵件,使用GCM來代替。 – Shrikant 2013-04-04 10:30:08

+2

你檢查過'divideMessage()'和'sendMultipartTextMessage()「方法,如http://developer.android.com/reference/android/telephony/SmsManager.html描述。 – guanabara 2013-04-04 10:33:13

回答

5

我已創建了一個演示給你..我認爲它可以幫助你..

將在部分devide您的郵件併發送一個接一個......

public class Home extends Activity 
{ 
    Context ctx; 

    private static final String SMS_SEND_ACTION = "CTS_SMS_SEND_ACTION"; 
    private static final String SMS_DELIVERY_ACTION = "CTS_SMS_DELIVERY_ACTION"; 

    String SENT = "SMS_SENT"; 
    String DELIVERED = "SMS_DELIVERED"; 

    String ph_no; 
    String str; 

    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.home); 

     ctx = this; 

     ph_no = "set destination mob. no here.."; 
     str = "Your message here...."; 

     SmsManager sm = SmsManager.getDefault(); 

     IntentFilter sendIntentFilter = new IntentFilter(SMS_SEND_ACTION); 
     IntentFilter receiveIntentFilter = new IntentFilter(SMS_DELIVERY_ACTION); 

     PendingIntent sentPI = PendingIntent.getBroadcast(ctx, 0,new Intent(SMS_SEND_ACTION), 0); 
     PendingIntent deliveredPI = PendingIntent.getBroadcast(ctx, 0,new Intent(SMS_DELIVERY_ACTION), 0); 

     BroadcastReceiver messageSentReceiver = new BroadcastReceiver() 
     {  
      @Override 
      public void onReceive(Context context, Intent intent) 
      { 
       switch (getResultCode()) 
       { 
        case Activity.RESULT_OK: 
         Toast.makeText(context, "SMS sent", Toast.LENGTH_SHORT).show(); 
         break; 
        case SmsManager.RESULT_ERROR_GENERIC_FAILURE: 
         Toast.makeText(context, "Generic failure", Toast.LENGTH_SHORT).show(); 
         break; 
        case SmsManager.RESULT_ERROR_NO_SERVICE: 
         Toast.makeText(context, "No service", Toast.LENGTH_SHORT).show(); 
         break; 
        case SmsManager.RESULT_ERROR_NULL_PDU: 
         Toast.makeText(context, "Null PDU", Toast.LENGTH_SHORT).show(); 
         break; 
        case SmsManager.RESULT_ERROR_RADIO_OFF: 
         Toast.makeText(context, "Radio off", Toast.LENGTH_SHORT).show(); 
         break; 
       } 
      } 
     }; 

     registerReceiver(messageSentReceiver, sendIntentFilter); 

     BroadcastReceiver messageReceiveReceiver = 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;       
       } 
      } 
     }; 

     registerReceiver(messageReceiveReceiver, receiveIntentFilter); 

     ArrayList<String> parts =sm.divideMessage(str); 

     ArrayList<PendingIntent> sentIntents = new ArrayList<PendingIntent>(); 
     ArrayList<PendingIntent> deliveryIntents = new ArrayList<PendingIntent>(); 

     for (int i = 0; i < parts.size(); i++) 
     { 
      sentIntents.add(PendingIntent.getBroadcast(ctx, 0, new Intent(SMS_SEND_ACTION), 0)); 
      deliveryIntents.add(PendingIntent.getBroadcast(ctx, 0, new Intent(SMS_DELIVERY_ACTION), 0)); 
     } 

     sm.sendMultipartTextMessage(ph_no,null, parts, sentIntents, deliveryIntents); 
    } 
} 

這代碼也會給你發送和發送通知。 (此發送和傳遞通知是額外的代碼,但它的地方是使用全爲您)

相關問題