我的活动延伸AppCompatActivity
。要运行此活动,我需要授予应用ACCESS_COARSE_LOCATION
和WRITE_EXTERNAL_STORAGE
权限。在我更新到Android Studio 3.0之前,一切运行良好。
现在,没有显示请求WRITE_EXTERNAL_STORAGE
权限的提示,以及功能
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults)
在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卡(总是使用内部存储器),但这绝不是问题。
targetSdkVersion
和compileSdkVersion
都是26。
修改
其他相关权限(例如READ_EXTERNAL_STORAGE
)也正常运行。这只是写权限的问题。
测试了两个Android设备和模拟器
完成onCreate方法:
@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;
}
});
}
提前致谢。
答案 0 :(得分:2)
原来这个问题是由于新版HockeyApp造成的。 HockeySDK集成在我的应用程序中。现在,从版本5.0.0开始,文档说:
为了准备Android O,HockeySDK-Android现在限制了 使用maxSdkVersion过滤器的WRITE_EXTERNAL_STORAGE权限。在 一些用例,例如应用程序包含需要的依赖项 这个权限,maxSdkVersion让那些人无法做到 依赖项以授予或请求权限。解决方案 那些情况是声明工具:node =“replace”清单合并 稍后在依赖树中的策略
因此,在清单中添加工具:node =“replace”到WRITE权限后,问题就解决了。
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" tools:node="replace"/>
我希望这对其他人有用
答案 1 :(得分:0)
试试这个:
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);
}
我建议您查看文档:{{3}}
<强>更新强>
为什么您对这两种权限使用相同的请求代码?
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
更改每个请求代码值。