请求权限提示不显示



我有一个扩展AppCompatActivity的活动。要运行此活动,我需要向应用授予ACCESS_COARSE_LOCATIONWRITE_EXTERNAL_STORAGE权限。在我更新到Android Studio 3.0之前,一切都运行良好。

现在,请求WRITE_EXTERNAL_STORAGE权限的提示没有显示,并且函数

public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults)

在授予结果时返回 -1 甚至没有询问。

如果我请求权限ACCESS_COARSE_LOCATION,则不会发生此行为,该权限正常工作。下面是调用这两个检查的代码:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (this.checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("This app needs location access");
builder.setMessage("Please grant location access so this app can detect beacons.");
builder.setPositiveButton(android.R.string.ok, null);
builder.setOnDismissListener(new DialogInterface.OnDismissListener() {
public void onDismiss(DialogInterface dialog) {
requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, PERMISSION_REQUEST_COARSE_LOCATION);
}
});
builder.show();
}
}

另一个:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
final int PERMISSION_REQUEST_COARSE_LOCATION = 1;
if (this.checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("This app needs sd access");
builder.setMessage("Please grant sd access so this app can save files.");
builder.setPositiveButton(android.R.string.ok, null);
builder.setOnDismissListener(new DialogInterface.OnDismissListener() {
public void onDismiss(DialogInterface dialog) {
requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, PERMISSION_REQUEST_WRITE_EXTERNAL_STORAGE);
}
});
builder.show();
}
}

我的清单中的相应部分如下所示:

<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

我已经读过很多东西,但这很奇怪,当某种类型的权限显示在请求对话框中,而其他权限则没有。 顺便说一下,设备中没有SD卡(始终使用内部存储),但这从来都不是问题。

我的targetSdkVersioncompileSdkVersion都是26岁。

编辑

其他相关权限(如READ_EXTERNAL_STORAGE)也正常工作。这只是写入权限的问题。 在两个安卓设备和模拟器上测试

完成创建方法:

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_device_discovery);
context = this;
wm = (WifiManager) context.getApplicationContext().getSystemService(WIFI_SERVICE);
intent = getIntent();
if(intent.getBooleanExtra("LOCAL_NETWORK", true) == true){
ipAddress = Formatter.formatIpAddress(wm.getConnectionInfo().getIpAddress());
port = DEFAULT_LIGHT_PORT;
}
else{
ipAddress = intent.getStringExtra("TRACK_IP");
port = intent.getIntExtra("TRACK_PORT", DEFAULT_GATEWAY_PORT);
}
font = Typeface.createFromAsset(MainActivity.mainActivity.getAssets(), "RepRg.ttf");
mHandler = new Handler();
deviceArrayList = new ArrayList<>();
devicesBufferList = new ArrayList<>();
lightListAdapter = new LightListAdapter(this, deviceArrayList, intent);
discoveredListView = (ListView) findViewById(R.id.paired_devices);
discoveredListView.setAdapter(lightListAdapter);
listAllCheckBox = (CheckBox) findViewById(R.id.listAllCheckBox);
refreshButton = (ImageButton) findViewById(R.id.refreshButton);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (this.checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("This app needs sd access");
builder.setMessage("Please grant sd access so this app can save files.");
builder.setPositiveButton(android.R.string.ok, null);
builder.setOnDismissListener(new DialogInterface.OnDismissListener() {
public void onDismiss(DialogInterface dialog) {
requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, PERMISSION_REQUEST_WRITE_EXTERNAL_STORAGE);
}
});
builder.show();
}
}

listAllCheckBox.setTypeface(font);
listAllCheckBox.setTextSize(18);
listAllCheckBox.setTextColor(Color.parseColor("#202020"));
listAllCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
lightListAdapter.clear();
processDeviceListing();
}
});
refreshButton.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
refreshButton.setImageResource(R.drawable.refresh_down);
break;
case MotionEvent.ACTION_UP:
refreshButton.setImageResource(R.drawable.refresh_up);
refreshDevicesList();
break;
case MotionEvent.ACTION_CANCEL:
refreshButton.setImageResource(R.drawable.refresh_up);
break;
}
return false;
}
});
}

提前谢谢。

事实证明,问题是由于新版本的HockeyApp引起的。HockeySDK集成在我的应用程序中。现在,从版本 5.0.0 开始,文档说:

为了为Android O做好准备,HockeySDK-Android现在限制了 WRITE_EXTERNAL_STORAGE maxSdkVersion 筛选器的权限。在 一些用例,例如,应用程序包含需要 这个权限,maxSdk版本使那些不可能 用于授予或请求权限的依赖项。解决方案 这些情况是声明工具:node="replace"清单合并 依赖关系树中稍后的策略

因此,在清单中将 tools:node="replace" 添加到 WRITE 权限后,问题就解决了。

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" tools:node="replace"/>

来源在这里

我希望这对其他人有用

试试这个:

if (ContextCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_COARSE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.ACCESS_COARSE_LOCATION},
PERMISSION_REQUEST_COARSE_LOCATION);
}

我建议您检查文档:https://developer.android.com/training/permissions/requesting.html

更新

为什么对两个权限使用相同的请求代码?

requestPermissions(new String[]{Manifest.permission. ACCESS_COARSE_LOCATION}, PERMISSION_REQUEST_COARSE_LOCATION); <-- PERMISSION_REQUEST_COARSE_LOCATION

requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, PERMISSION_REQUEST_COARSE_LOCATION);  <-- PERMISSION_REQUEST_COARSE_LOCATION

更改每个请求代码值。

最新更新