Search code examples
androidandroid-sms

Send SMS method always fails


I have created a method to send an sms. So, it always fails. I have used uses-permission SEND_SMS but it keeps shows the error> DO you have any idea ?

            Log.i("Send SMS", "");
            String phoneNo = "XXXXXXXXXXXX";
            String sms = "HELLO" ;
            try {
                // Get the default instance of the SmsManager
                SmsManager smsManager = SmsManager.getDefault();
                smsManager.sendTextMessage(phoneNo,
                        null,
                        sms,
                        null,
                        null);
                Toast.makeText(ChooseDataActivity.this, "Your sms has successfully sent!",
                        Toast.LENGTH_LONG).show();
            } catch (Exception ex) {
                Toast.makeText(ChooseDataActivity.this,"Your sms has failed...",
                        Toast.LENGTH_LONG).show();
                ex.printStackTrace();
            }

@sandhya sasane

I want the message to be sent after the user answer "YES" to the AlertDialog message. So, I have modified the code to be like the followings :

   AlertDialog.Builder altdial = new AlertDialog.Builder(mContext);
    altdial.setMessage(msg).setCancelable(false)
            .setPositiveButton("نعم",
                    new DialogInterface.OnClickListener()
                    {
                        @Override
                        public void onClick(DialogInterface dialog, int which)
                        {
                                SAVE_TO_FILE(filenames,ret_val,mContext,mdata);

                            if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M)
                            {
                                int Permission  = ContextCompat.checkSelfPermission(mContext, Manifest.permission.SEND_SMS);

                                // 1 = PERMISSION DENIED
                                // 2 = PERMISSION DENIED BCZ OPERATION IS NOT ALLOWED
                                // 0 = PERMISSION GRANTED

                                if(Permission == 0)
                                {
                                    SmsManager MySmsManager = SmsManager.getDefault();
                                    ArrayList<String> msgArray = MySmsManager.divideMessage("dfdfdaf");
                                    MySmsManager.sendMultipartTextMessage("0558300694", null,msgArray, null, null);

                                }


                            }
                            else
                            {
                                if (ContextCompat.checkSelfPermission(mContext, Manifest.permission.SEND_SMS) != PackageManager.PERMISSION_GRANTED)
                                {
                                    // show error
                                }
                                else
                                {
                                    SmsManager MySmsManager = SmsManager.getDefault();
                                    ArrayList<String> msgArray = MySmsManager.divideMessage("dfdfdaf");
                                    MySmsManager.sendMultipartTextMessage("XXXXXXXXX", null,msgArray, null, null);

                                }
                            }

                        }
                    })
            .setNegativeButton("لا", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    setThe_answer(false);
                    dialog.cancel();
                    ConfirmTripActivity x = new ConfirmTripActivity() ;
                    x.myBoolean = false;
                }
            });

    AlertDialog alert = altdial.create();
    alert.show();

Nothing so far. It didn't send any sms nor showed me the permission message


Solution

  • Non standard programming... Is your problem..

    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M)
    {
        int Permission  = ContextCompat.checkSelfPermission(context, Manifest.permission.SEND_SMS);
    
        // 1 = PERMISSION DENIED
        // 2 = PERMISSION DENIED BCZ OPERATION IS NOT ALLOWED
        // 0 = PERMISSION GRANTED
    
        if(Permission == 0)
        {
            SmsManager MySmsManager = SmsManager.getDefault();
            ArrayList<String> msgArray = MySmsManager.divideMessage(DefaultMsgTemplate);
            MySmsManager.sendMultipartTextMessage(SendSMSTo, null,msgArray, null, null);
    
        }
    
    
    }
    else
    {
        if (ContextCompat.checkSelfPermission(context, Manifest.permission.SEND_SMS) != PackageManager.PERMISSION_GRANTED)
        {
            // show error
        }
        else
        {
            SmsManager MySmsManager = SmsManager.getDefault();
            ArrayList<String> msgArray = MySmsManager.divideMessage(DefaultMsgTemplate);
            MySmsManager.sendMultipartTextMessage(SendSMSTo, null,msgArray, null, null);
    
        }
    }
    

    Tip : 1

    You should make habit of developing using log.d messages... it helps in debugging and programming too

    Tip : 2

    When user clicks "YES" -> Call a user defined method, which will send SMS..., Do not make overridden methods too complex and burdenful...

    Tip : 3

    Make a sample project ( a new project ) which will just send a SMS to a hardcoded number. using the provided code in this answer. It will work 100%, if you read , study and implement it neatly. As stated... If problem persists then dump the logcat here so that i or community can help ... On success implement it in your project on whatever event you like...