1

I am trying to send an SMS using the following import [import android.telephony.SmsManager;] .... The first time I run the code, it worked just fine using my --Samsung Galaxy Note 3--. Then I tried to send another message, but it didn't work and it keeps showing a {Generic failure} message. I then tried to send a message normally from my Android Messaging app, but it shows a message that [sending sms failed]

I also used the permission in the Manifest

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

This is the Activity that send the SMS

import java.util.ArrayList;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Build;
import android.os.Bundle;
import android.telephony.SmsManager;

import android.widget.TextView;
import android.widget.Toast;
public class SMS_contacts_Activity extends Activity {
public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);


       String Message="Hi there this is a message from my app";
       String Number ="55568103";

       sendSMS(Number, Message);
}

 //---sends an SMS message to another device---
public 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);
    //---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_LONG).show();
                    break;
                case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                    Toast.makeText(getBaseContext(), "Generic failure ", 
                            Toast.LENGTH_LONG).show();
                    break;
                case SmsManager.RESULT_ERROR_NO_SERVICE:
                    Toast.makeText(getBaseContext(), "No service ", 
                            Toast.LENGTH_LONG).show();
                    break;
                case SmsManager.RESULT_ERROR_NULL_PDU:
                    Toast.makeText(getBaseContext(), "Null PDU ", 
                            Toast.LENGTH_LONG).show();
                    break;
                case SmsManager.RESULT_ERROR_RADIO_OFF:
                    Toast.makeText(getBaseContext(), "Radio off ", 
                            Toast.LENGTH_LONG).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_LONG).show();
                    break;
                case Activity.RESULT_CANCELED:
                    Toast.makeText(getBaseContext(), "SMS not delivered", 
                            Toast.LENGTH_LONG).show();
                    break;                        
            }
        }
    }, new IntentFilter(DELIVERED));        

    SmsManager sms = SmsManager.getDefault();
    sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);        
  }
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
  • This is due to RESULT_ERROR_GENERIC_FAILURE, the sentPI may include the extra "errorCode" containing a radio technology specific value, generally only useful for troubleshooting. Check the specific error code for more debugging. – AnkitSomani Aug 06 '14 at 16:24
  • its not showing anything in the LogCat!!! i cant see any indication of errors – Adam Badawi Aug 06 '14 at 16:27
  • What I meant was a extra error code, that you have to add in your code. if ( i.hasExtra("errorCode") ) { return i.getStringExtra("errorCode"); } else { return "Unknown error. No 'errorCode' field."; } – AnkitSomani Aug 06 '14 at 16:38
  • I am not sure about that is there any additional code that i should be adding?!?!?! because as far as i am concerned this has worked perfectly at the bingeing then it stopped working?!? – Adam Badawi Aug 06 '14 at 17:26
  • Ankit gave you the code. Beyond that, if the stock messaging app is also failing, then it sounds like a device and/or account problem. Try rebooting the device. Also, if you've sent quite a few messages while testing, you may have hit the per-hour limit. You might just wait a while and try again. – Mike M. Aug 06 '14 at 17:40
  • I tried it again today the issue continued, i read about android Kitkat... having some issues with sending SMS messages and people complaining about the Messaging app always failing, could that be a related issue?? The thing is, in android Kitkat, i must set my app to be the default messaging app, but it's not related to messaging, i just want it to send auto SMS.. to operate some of its functions. Is there anyway around that issue?!?!?! – Adam Badawi Aug 07 '14 at 19:03

0 Answers0