2012-12-28 17 views
6

पर कई लोगों को प्रोग्रामेटिक रूप से एसएमएस भेजना मैं वर्तमान में एक एसएमएस एप्लिकेशन विकसित करने की कोशिश कर रहा हूं और उपयोगकर्ता को कई लोगों को एसएमएस भेजने की अनुमति देता हूं।जेनेरिक त्रुटि

एसएमएस जब तक मैं sendMultipartTextMessage जो नीचे

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

    SmsManager sms = SmsManager.getDefault(); 
    ArrayList<String> parts = sms.divideMessage(message); 
    int messageCount = parts.size(); 

    Log.i("Message Count", "Message Count: " + messageCount); 

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

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

    for (int j = 0; j < messageCount; j++) { 
     sentIntents.add(sentPI); 
     deliveryIntents.add(deliveredPI); 
    } 

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

    sms.sendMultipartTextMessage(phoneNumber, null, parts, sentIntents, deliveryIntents); 
} 

है जब मैं इस तरह एक पाश में एक से अधिक लोगों को भेजने के लिए प्रयास मैं एक सामान्य त्रुटि का सामना करना होगा उपयोग करने के लिए है और संदेश जीता ' टी भेजा जाएगा। मुझे संदेह है कि यह संदेश इसलिए है क्योंकि संदेश शायद 3-4 भागों है और एंड्रॉइड सिस्टम 3000 मिलीसेकंड देरी के साथ भी समय में संदेश नहीं भेज सकता है।

for (int t = 0; t < array.length; t++) { 
     System.out.println("temp: " + array[t].toString()); 
     try { 
      sendSMS(array[t].toString(), message); 
      Thread.sleep(3000); 
     } catch (InterruptedException e) { 
      e.printStackTrace(); 
     } 
    } 

संपादित करें: उपर्युक्त कोड एक AsyncTask में है जो किसी सेवा के अंदर निष्पादित किया जाता है।

+0

अपना लॉककैट दिखाएं –

+0

कोई लॉगकट त्रुटि नहीं है, यह केवल 'SmsManager.RESULT_ERROR_GENERIC_FAILURE' के लिए टोस्ट दिखाएगी, जेनेरिक त्रुटि दो बार जब मैं इसे 2 संपर्कों में भेजने की कोशिश करता हूं। – dythe

+0

आपको यह त्रुटि एमुलेटर या डिवाइस में मिली है? –

उत्तर

19

आईएचएमओ आप सही दृष्टिकोण का उपयोग नहीं कर रहे हैं। मैं एक अलग उपयोग कर रहा हूँ और यह काम कर रहा है। मैं समझाने की कोशिश करता हूं:

आपको सभी लंबित इरादों के लिए काउंटर रखना चाहिए क्योंकि अगर आपके द्वारा भेजे जा रहे एसएमएस में दो भाग हैं तो आपको दो RESULT_OK (या RESULT_ERROR_ *) प्राप्त होंगे और केवल तभी जब आप सभी भागों के परिणाम प्राप्त करेंगे अगले एसएमएस भेजना जारी रखें। मुझे थ्रेड को अवरुद्ध करने का विचार पसंद नहीं है ... शायद यही कारण है कि आपको अजीब त्रुटियां मिल रही हैं।

private int mMessageSentParts; 
private int mMessageSentTotalParts; 
private int mMessageSentCount; 

private void startSendMessages(){ 

    registerBroadCastReceivers(); 

    mMessageSentCount = 0; 
    sendSMS(array[mMessageSentCount].toString(), message); 
} 

private void sendNextMessage(){ 
    if(thereAreSmsToSend()){ 
     sendSMS(array[mMessageSentCount].toString(), message); 
    }else{ 
     Toast.makeText(getBaseContext(), "All SMS have been sent", 
         Toast.LENGTH_SHORT).show(); 
    } 
} 

private boolean thereAreSmsToSend(){ 
    return mMessageSentCount < array.length; 
} 

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

    SmsManager sms = SmsManager.getDefault(); 
    ArrayList<String> parts = sms.divideMessage(message); 
    mMessageSentTotalParts = parts.size(); 

    Log.i("Message Count", "Message Count: " + mMessageSentTotalParts); 

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

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

    for (int j = 0; j < mMessageSentTotalParts; j++) { 
     sentIntents.add(sentPI); 
     deliveryIntents.add(deliveredPI); 
    } 

    mMessageSentParts = 0; 
    sms.sendMultipartTextMessage(phoneNumber, null, parts, sentIntents, deliveryIntents); 
} 

private void registerBroadCastReceivers(){ 

    registerReceiver(new BroadcastReceiver() { 
     @Override 
     public void onReceive(Context arg0, Intent arg1) { 
      switch (getResultCode()) { 
      case Activity.RESULT_OK: 

       mMessageSentParts++; 
       if (mMessageSentParts == mMessageSentTotalParts) { 
        mMessageSentCount++; 
        sendNextMessage(); 
       } 

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

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

} 
+0

क्या आपका दृष्टिकोण सिर्फ एक उपयोगकर्ता को संदेश भेज रहा है? क्योंकि मेरे मामले में 'सरणी [टी] .toString() 'केवल 0 से 3 लोगों की संख्या की स्ट्रिंग सरणी है। – dythe

+1

यह समय पर एक प्राप्तकर्ता को संदेश भेजता है। मैंने माना कि आपकी सरणी की प्रत्येक स्थिति एक एकल फोन नंबर है। – rciovati

+0

इसे हल करने के लिए प्रबंधित, सही उत्तर के रूप में चिह्नित। – dythe

2

मैं एक ही त्रुटि "सामान्य विफलता" इस कोड का उपयोग हुआ आ गए हैं:

मैं अपने दृष्टिकोण के साथ अपने कोड पुनर्संशोधित। लेकिन मैंने सरणी [mMessageSentCount] .toString()। Trim() का उपयोग किया। फिर मेरी समस्या हल हो गई & इसकी कामकाजी जुर्माना। धन्यवाद

संबंधित मुद्दे