我想获得android.permission.WRITE_SETTINGS
权限以在那里切换值。看一下我得到这段代码的文档后,它根本没有显示对话框。
在授予权限时,我调用AlarmManager来启动BroadcastReceiver,我在startAlarm方法上执行。但是我觉得它与我的问题无关。
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;
}
}
}