发送短信方法总是失败
Send SMS method always fails
我创建了一个发送短信的方法。
所以,总是失败。
我使用了 uses-permission 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
我希望在用户回答 "YES" 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();
目前还没有。它没有发送任何短信,也没有向我显示许可消息
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);
}
}
小费:1
您应该养成使用 log.d
消息进行开发的习惯...它对 debugging
和 programming
也有帮助
提示:2
当用户单击 "YES" -> 调用用户定义的方法,该方法将发送短信...,不要制作 overridden
方法 complex
和 burdenful
...
小费:3
创建一个示例项目 (a new project
),它只会将 SMS
发送到 hardcoded number
。使用此答案中提供的 code
。如果您阅读、学习和 implement it neatly
,它将 100% 有效。如前所述...如果问题仍然存在,请将 logcat
转储到此处,以便我或社区可以提供帮助...On success
在您喜欢的任何事件中在您的项目中实施它...
我创建了一个发送短信的方法。 所以,总是失败。 我使用了 uses-permission 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
我希望在用户回答 "YES" 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();
目前还没有。它没有发送任何短信,也没有向我显示许可消息
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);
}
}
小费:1
您应该养成使用 log.d
消息进行开发的习惯...它对 debugging
和 programming
也有帮助
提示:2
当用户单击 "YES" -> 调用用户定义的方法,该方法将发送短信...,不要制作 overridden
方法 complex
和 burdenful
...
小费:3
创建一个示例项目 (a new project
),它只会将 SMS
发送到 hardcoded number
。使用此答案中提供的 code
。如果您阅读、学习和 implement it neatly
,它将 100% 有效。如前所述...如果问题仍然存在,请将 logcat
转储到此处,以便我或社区可以提供帮助...On success
在您喜欢的任何事件中在您的项目中实施它...