-1

我创建了一种发送短信的方法。所以,总是失败。我使用了使用权限 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();

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

4

1 回答 1

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

当用户单击“是”时 -> 调用用户定义的方法,它将发送短信......,不要也制作overridden方法complexburdenful......

提示:3

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

于 2018-08-13T16:33:18.153 回答