本文介绍了java.lang.SecurityException:正在发送短信:uid 10282没有android.permission.SEND_SMS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的应用程序不断崩溃,并显示以下错误代码:
My app keeps crashing with this error code:
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方法中添加此代码,如果仍然存在任何错误,请随时与我联系.这将检查SMS权限,并在未授予SMS权限的情况下进行请求.
Try adding this code in onCreate method of your 1st activity and feel free to contact me again if any error persists.This will check for SMS permission and will ask for it in case it hasn't been granted.
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
}
这篇关于java.lang.SecurityException:正在发送短信:uid 10282没有android.permission.SEND_SMS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!