我的应用程序不断崩溃并出现以下错误代码:
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);
}
});
}
尝试在第一个活动的 onCreate 方法中添加此代码,如果仍然存在任何错误,请随时再次与我联系。
这将检查短信权限,并在未授予权限的情况下请求该权限。
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
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)