我試圖通過我的應用程序更改我的系統/手機亮度。爲此,我需要徵求許可(「危險許可」)。我不確定我是否已經採用了正確的方式,但這看起來並不奏效,因爲它總是返回拒絕權限。Android工作室危險權限
public void switches() {
if (systemBrightness) {
ActivityCompat.requestPermissions(MainActivity.this,
new String[]{Manifest.permission.WRITE_SETTINGS},
1);
Toast.makeText(this, "Changed to system brightness: changing the 「system brightness」 is permanent", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "Changed to screen brightness: ", Toast.LENGTH_SHORT).show();
}
}
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case 1: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// permission was granted, yay! Do the
// contacts-related task you need to do.
} else {
// permission denied, boo! Disable the
// functionality that depends on this permission.
Toast.makeText(MainActivity.this, "Permission denied to read your External storage", Toast.LENGTH_SHORT).show();
}
return;
}
// other 'case' lines to check for other
// permissions this app might request
}
}
//瑪尼巨星
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.FLASHLIGHT" />
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
public void changeScreenBrightness(float brightness) {
mBrightness = brightness;
if (systemBrightness) {
if (!Settings.System.canWrite(this)) {
Intent i = new Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS);
startActivity(i);
} else {
Settings.System.putInt(mContentResolver, Settings.System.SCREEN_BRIGHTNESS, (int) (mBrightness * 255)* preset);
}
} else {
WindowManager.LayoutParams mLayoutParams = mWindow.getAttributes();
mLayoutParams.screenBrightness = mBrightness * preset;
mWindow.setAttributes(mLayoutParams);
}
}
無錯誤可見,以便far..did你把'WRITE_SETTINGS'許可清單中? – Pzy64
我已將我的清單權限添加到問題 – Amar
真的,沒有人能指出我正確的方向? – Amar