I have an activity that extends AppCompatActivity
. For this activity to run I need to grant the app with ACCESS_COARSE_LOCATION
and WRITE_EXTERNAL_STORAGE
permissions. Everything was working well until I updated to Android Studio 3.0.
Now, the prompt for requesting WRITE_EXTERNAL_STORAGE
permissions is not showing, and the function
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults)
is returning -1 at grantResults without even asking.
This behavior is not happening if I request the permission ACCESS_COARSE_LOCATION
, which works as it should. Below, the code invoking both checks:
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();
}
}
And the other:
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();
}
}
The corresponding section in my manifest looks like this:
<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" />
I've read many things, but this is pretty strange, when some type of permissions are showing up the request dialog, while others aren't. By the way, there is no SD card in the device (always used internal storage), but that was never a problem.
Both my targetSdkVersion
and compileSdkVersion
are 26.
EDIT
Other relevant permissions, such as READ_EXTERNAL_STORAGE
, are also working correctly. It's just an issue with the write permission.
Tested on two android devices and emulator
Complete onCreate Method:
@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;
}
});
}
Thanks in advance.