Skip to content Skip to sidebar Skip to footer

I Received Error When Trying To Send A Message

send.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub //URI uri=uri.parse('sms://+919500518057'

Solution 1:

Call this method from your main:

privatevoidsendSMS() {
        SMSSend smsSend = new SMSSend();

        smsSend.execCommand();
    }

then the below two classed will be responsible for sending SMS.

publicclassSMSSend {

    privatefinalStringCLASS_NAME=this.getClass().getSimpleName();
    privatefinalstaticStringSEND_SMS_FAILURE="Send SMS command executed and status is failure";
    private String phoneNumber;
    private String messageText;

    /**
     * constructor is defined.
     * 
     * @param CommandProcessor
     *            cmdProcessor
     */publicSMSSend() {
        super();
    }

    /**
     * execCommand method
     * 
     * It is an overridden here and declared in CommandHandler (base class)
     */publicvoidexecCommand() {
        try {
            newThread() {
                publicvoidrun() {
                    sendSMS();
                }
            }.start();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }



    /**
     * sendSMS method
     * 
     * It retrieves the command parameters and sends the sms.
     * 
     * @param Command
     *            command
     * @return Status
     */privatevoidsendSMS() {
        try {
            SMSSendHelpersmsHelper=newSMSSendHelper();
            phoneNumber = "0123456789";
            messageText = "Message Text smaple";
            if (null == phoneNumber || phoneNumber.length() < 2) {
            } else {
                smsHelper.sendSMS(phoneNumber, messageText);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
        }

    }

}




/**
 * SMSSend class
 * 
 * It is responsible for sending SMSs.
 *  
 */publicclassSMSSendHelper {
    privatefinalStringCLASS_NAME=this.getClass().getSimpleName();
    privateStringSENT="SMS_SENT";
    privateStringDELIVERED="SMS_DELIVERED";
    private PendingIntent sentPI;
    private BroadcastReceiver sentReceiver;
    private BroadcastReceiver deliveredReceiver;
    private PendingIntent deliveredPI;
    privatestaticfinalintSMS_LENGHT=160;
    privatestaticStringstatus="";

    /**
     * Constructor is defined
     * 
     * @param Status
     *            smsstatus
     */publicSMSSendHelper() {
        sentPI = PendingIntent.getBroadcast(ConfigClass.getAppContext(), 0,
                newIntent(SENT), 0);

        deliveredPI = PendingIntent.getBroadcast(ConfigClass.getAppContext(),
                0, newIntent(DELIVERED), 0);
        sentReceiver = newBroadcastReceiver() {
            @OverridepublicvoidonReceive(Context arg0, Intent arg1) {

                switch (getResultCode()) {
                case Activity.RESULT_OK:
                    try {
                        synchronized (lock) {
                            lock.notifyAll();
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    setSendSMSSataus("SMS Sent");
                    break;
                case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                    try {
                        synchronized (lock) {
                            lock.notifyAll();
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    setSendSMSSataus("Generic failure");
                    break;
                case SmsManager.RESULT_ERROR_NO_SERVICE:
                    try {
                        synchronized (lock) {
                            lock.notifyAll();
                        }
                    } catch (Exception e) {
                    }
                    setSendSMSSataus("No service");
                    break;
                case SmsManager.RESULT_ERROR_NULL_PDU:
                    try {
                        synchronized (lock) {
                            lock.notifyAll();
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    setSendSMSSataus("Null PDU");
                    break;
                case SmsManager.RESULT_ERROR_RADIO_OFF:
                    try {
                        synchronized (lock) {
                            lock.notifyAll();
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    setSendSMSSataus("Radio off");
                    break;
                default:
                    try {
                        synchronized (lock) {
                            lock.notifyAll();
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    break;

                }
                ConfigClass.getAppContext().unregisterReceiver(sentReceiver);
            }
        };
        // ---when the SMS has been sent---
        ConfigClass.getAppContext().registerReceiver(sentReceiver,
                newIntentFilter(SENT));

        deliveredReceiver = newBroadcastReceiver() {
            @OverridepublicvoidonReceive(Context arg0, Intent arg1) {
                switch (getResultCode()) {
                case Activity.RESULT_OK:
                    break;
                case Activity.RESULT_CANCELED:
                    break;
                }
                ConfigClass.getAppContext().unregisterReceiver(
                        deliveredReceiver);
            }
        };

        ConfigClass.getAppContext().registerReceiver(deliveredReceiver,
                newIntentFilter(DELIVERED));
    }

    /**
     * setSendSMSSataus method
     * 
     * @param setSendSMSSataus
     *            currentSataus
     */publicstaticvoidsetSendSMSSataus(String currentSataus) {
        status = currentSataus;

    }

    /**
     * getSendSMSSataus method
     * 
     * @return String status
     */public String getSendSMSSataus() {
        inti= sentReceiver.getResultCode();
        return status;
    }

    /**
     * sendSMS method
     * 
     * @param String
     *            phoneNumber
     * @param String
     *            message
     * @return Status
     */publicvoidsendSMS(String phoneNumber, String message) {
        if (message == null) {
            message = " ";
        }

        SmsManagersms= SmsManager.getDefault();

        if (message.length() > SMS_LENGHT) {
            sendLongSMS(sms, message, phoneNumber);
        } else {
            Log.d("E++++D",phoneNumber+"======>>>>>SOS");

            sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);
        }
        try {
            synchronized (lock) {
                lock.wait();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    privatevoidsendLongSMS(SmsManager sms, String message, String phoneNumber) {
        ArrayList<String> parts = sms.divideMessage(message);
        intnumParts= parts.size();
        ArrayList<PendingIntent> sentIntents = newArrayList<PendingIntent>();
        ArrayList<PendingIntent> deliveryIntents = newArrayList<PendingIntent>();
        for (inti=0; i < numParts; i++) {
            sentIntents.add(sentPI);
            deliveryIntents.add(deliveredPI);
        }

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

    privateObjectlock=newObject();
}

Solution 2:

You're using the wrong this. Swap this:

PendingIntent pi=PendingIntent.getActivity(this, 0, new Intent(this, Object.class), 0);

for this:

PendingIntent pi=PendingIntent.getActivity(YourActivity.this, 0, new Intent(YourActivity.this, Object.class), 0);

Right now you're using the OnClickListener.this. :)

Solution 3:

Try using:

PendingIntent pi=PendingIntent.getActivity(mContext, 0, new Intent(mContext, Object.class), 0);

And in your global variables add:

private Context mContext;

And initialize it in onCreate() after the super call using:

mContext = this;

By using only this while creating the PendingIntent, you're not referring to the current instance of your Activity. You're referring to the current instance of the OnClickListener inner class

Solution 4:

I am using this for sending Message:--

privatevoidsendSMS(String phoneNumber, String message)
{        

    SmsManager sms = SmsManager.getDefault();
    sms.sendTextMessage(phoneNumber, null, message, null, null);     
} 

Solution 5:

PendingIntent requires a Context as first parameter. Inside an activity you can pass the activity context to the method using PendingIntent.getActivity(this, ..).

However this doesn't work in an setOnClickListener (like you did). Inside the OnclickListener this references the setOnClickListener (which isn't a valid context object) and not the activity.

If you want to pass the activity to the getActivity you can use PendingIntent.getActivity(<NameOfYourActivity>.this, ..)

e.g. PendingIntent.getActivity(MyActivity.this, ..)

Post a Comment for "I Received Error When Trying To Send A Message"