我的应用程序不断崩溃,并显示以下错误代码:
java.lang.SecurityException: Sending SMS message: uid 10282 does not have android.permission.SEND_SMS.
这是我的代码
Button button2 = (Button) findViewById(R.id.text);
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String messageToSend = "this is a text";
String number = "XXXXXX";
SmsManager.getDefault().sendTextMessage(number, null, messageToSend, null,null);
}
});
}
答案 0 :(得分:3)
请尝试在您的第一个活动的onCreate方法中添加此代码,如果仍然存在任何错误,请随时与我联系。 这将检查SMS权限,并在未授予SMS权限的情况下进行请求。
Here, 'this' is the current activity
if ((ContextCompat.checkSelfPermission(this, Manifest.permission.READ_SMS) +
ContextCompat.checkSelfPermission(this, Manifest.permission.SEND_SMS))
!= PackageManager.PERMISSION_GRANTED) {
// Permission is not granted
// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(this,"Manifest.permission.READ_SMS") ||
ActivityCompat.shouldShowRequestPermissionRationale(this,"Manifest.permission.READ_SMS")) {
// Show an explanation to the user *asynchronously* -- don't block
// this thread waiting for the user's response! After the user
// sees the explanation, try again to request the permission.
} else {
// No explanation needed; request the permission
ActivityCompat.requestPermissions(this,
new String[]{"Manifest.permission.READ_SMS, Manifest.permission.SEND_SMS"},
REQUEST_CODE);
// REQUEST_CODE is an
// app-defined int constant. The callback method gets the
// result of the request.
}
}
else {
// Permission has already been granted
}
答案 1 :(得分:1)
将<uses-permission android:name="android.permission.SEND_SMS"/>
添加到清单xml文件中。
由于该应用没有发送短信的适当权限而生成错误。
答案 2 :(得分:1)
您必须使用SMS意图,或者您需要发送声明表格... 从2019年1月9日开始不允许发送短信,通话记录。.Plesse检查此链接android SMS权限
答案 3 :(得分:0)
在第一个或主要活动中添加以下代码,如果在片段中添加,则可能无效。
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
if (checkSelfPermission(Manifest.permission.SEND_SMS) == PackageManager.PERMISSION_DENIED) {
Log.d("permission", "permission denied to SEND_SMS - requesting it");
String[] permissions = {Manifest.permission.SEND_SMS};
requestPermissions(permissions, PERMISSION_REQUEST_CODE);
}
}
如果您尚未在清单文件中添加以下权限,请添加:
<uses-permission android:name="android.permission.SEND_SMS"/>