我正在onCreate
方法中运行此代码以尝试获得许可,但是由于某种原因,它给了我错误:
需要不兼容的类型int发现此部分为布尔值:if (ContextCompat.checkSelfPermission(MainActivity.this, android.Manifest.permission.READ_SMS)+ ContextCompat.checkSelfPermission(MainActivity.this, android.Manifest.permission.SEND_SMS))
@Override
protected void onCreate(Bundle savedInstanceState) {
if (ContextCompat.checkSelfPermission(MainActivity.this, android.Manifest.permission.READ_SMS) +
ContextCompat.checkSelfPermission(MainActivity.this, android.Manifest.permission.SEND_SMS))
!= PackageManager.PERMISSION_GRANTED) {
if (AppCompatActivity.shouldShowRequestPermissionRationale(this,"Manifest.permission.READ_SMS") ||
AppCompatActivity.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
}
答案 0 :(得分:1)
在“如果”条件开始时,一个开放的圆括号丢失了:
if (ContextCompat.checkSelfPermission(MainActivity.this, android.Manifest.permission.READ_SMS) + ContextCompat.checkSelfPermission(MainActivity.this, android.Manifest.permission.SEND_SMS)) != PackageManager.PERMISSION_GRANTED){......}
您有3个开放式括号和4个封闭的括号。
这是您应该拥有的:
if ((ContextCompat.checkSelfPermission(MainActivity.this, android.Manifest.permission.READ_SMS) + ContextCompat.checkSelfPermission(MainActivity.this, android.Manifest.permission.SEND_SMS)) != PackageManager.PERMISSION_GRANTED){.....}
答案 1 :(得分:0)
ContextCompat.checkSelfPermission(MainActivity.this,
android.Manifest.permission.READ_SMS) +
ContextCompat.checkSelfPermission(MainActivity.this, android.Manifest.permission.SEND_SMS))
!= PackageManager.PERMISSION_GRANTED
这部分不是有效的Java语句。
您可能想要这样的东西:
if ((ContextCompat.checkSelfPermission(MainActivity.this, android.Manifest.permission.READ_SMS) != PackageManager.PERMISSION_GRANTED)
|| (ContextCompat.checkSelfPermission(MainActivity.this, android.Manifest.permission.SEND_SMS)
!= PackageManager.PERMISSION_GRANTED))