我创建了一种发送短信的方法。所以,总是失败。我使用了使用权限 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();
到目前为止什么都没有。它没有发送任何短信,也没有向我显示许可信息