发送短信方法总是失败

时间:2018-08-13 15:00:01

标签: android android-sms

我创建了一种发送短信的方法。 因此,它总是失败。 我已经使用过使用权限SEND_SMS,但是它一直显示错误> 你有什么主意吗 ?

            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

我希望在用户对AlertDialog消息回答“是”之后发送该消息。 因此,我将代码修改如下:

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

到目前为止没有。它没有发送任何短信,也没有向我显示许可消息

1 个答案:

答案 0 :(得分:1)

  

非标准编程...是您的问题。

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

    }
}

提示:1

您应该养成使用log.d消息进行开发的习惯,这也有助于debuggingprogramming

提示:2

当用户单击“是”->调用用户定义的方法时,该方法将发送SMS ...,也不要overriddencomplex来创建burdenful方法... < / p>

提示:3

制作一个示例项目(a new project,该项目只会将SMS发送到hardcoded number。在此答案中使用提供的code。如果您阅读,学习和implement it neatly,它将100%起作用。如前所述...如果问题仍然存在,则将logcat转储到此处,以便我或社区可以帮助... On success在您喜欢的事件中在您的项目中实现它...