I want to get android.permission.WRITE_SETTINGS
permissions to toggle a value there. After take a look at the documentation I got this code, however it's not showing the dialog at all.
On permission granted I call an AlarmManager to start a BroadcastReceiver, I do it on the startAlarm method. However I think it doesn't have nothing to do with my problem.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SeePermissions();
}
public void SeePermissions(){
// Here, thisActivity is the current activity
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.WRITE_SETTINGS)
!= PackageManager.PERMISSION_GRANTED) {
// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(this,
Manifest.permission.WRITE_SETTINGS)) {
} else {
// No explanation needed, we can request the permission.
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.WRITE_SETTINGS},
MY_PERMISSIONS_REQUEST_WRITE_SETTINGS);
}
}
}
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case MY_PERMISSIONS_REQUEST_WRITE_SETTINGS: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Intent alarmIntent=new Intent(this, AlarmReceiver.class);
enableNightMode = PendingIntent.getBroadcast(this, 123, alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT);
startAlarm();
} else {
Toast.makeText(getApplicationContext(),"NO",Toast.LENGTH_SHORT).show();
}
return;
}
}
}