0

我試圖通過我的應用程序更改我的系統/手機亮度。爲此,我需要徵求許可(「危險許可」)。我不確定我是否已經採用了正確的方式,但這看起來並不奏效,因爲它總是返回拒絕權限。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); 
     } 
    } 
+0

無錯誤可見,以便far..did你把'WRITE_SETTINGS'許可清單中? – Pzy64

+0

我已將我的清單權限添加到問題 – Amar

+0

真的,沒有人能指出我正確的方向? – Amar

回答

1

問計風險的權限比脆弱的權限處理方式不同。您需要使用Settings.System.canWrite(Context)方法來檢查您是否可以這樣做。如果不通過調用startActivityForResult發送合適的Intent到系統:

public boolean checkWriteSettingsPermission() { 
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) { 
     return true; 
    } 
    if (!Settings.System.canWrite(getContext())) { 
     Intent intent = new Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS, 
      Uri.parse("package:" + getPackageName())); 
     startActivityForResult(intent, REQUEST_CODE); 
     return false; 
    } else { 
     return true; 
    } 
} 

一切都寫在documentation

+0

我已經編輯了我的問題的意圖權限。我現在正在起牀一個新的意圖/窗口,標題爲「Aviability」。但在這裏任何地方都不會讓我接受允許應用程序更改系統屏幕亮度 – Amar

+0

自動亮度設置?檢查此答案:https://stackoverflow.com/a/36001592/6507689 –

+0

好的,但這是一個過時的答案。因爲自從我的API級別應用程序是23在清單中的權限聲明不會工作? – Amar