-1

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

ama989
  • 463
  • 1
  • 5
  • 23
  • Again which error, you have not mentioned... – sandhya sasane Aug 13 '18 at 16:34
  • I always get Toast.makeText(ChooseDataActivity.this,"Your sms has failed...", – ama989 Aug 13 '18 at 16:42
  • Check the modified code please. @sandhyasasane – ama989 Aug 13 '18 at 16:47
  • Check my edited answer to the your edited question – sandhya sasane Aug 13 '18 at 17:57
  • You even not taken efforts to show errors when permissions are 1 and 2... You even not taken efforts to show error where it is commented... You even not taking efforts to dump the logcat to show what error is there to the stackoverflow community... Sorry ... but down voting for the same... so that you will take some efforts while coding and while posting the questions here onwards... Apologises ... – sandhya sasane Aug 13 '18 at 18:01
  • Thanks for the commit. Anyways. Your Answer works fine. But, There is an issue. When it asks for sending sms permission and I allow it. It sends fine. But, when I try to send again. The app doesn't send anything nor show the permission message. – ama989 Aug 13 '18 at 19:12

1 Answers1

1

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...

sandhya sasane
  • 1,334
  • 12
  • 19