

質問です。

Androidのネイティブ機能について、マニフェストの  
`uses-permission
android:name="android.permission.WRITE_SETTINGS"`関連がうまく動作しません。（画面輝度の変更・取得、フォントサイズの変更ができないなどなど。）  
特にエラーは出ておらず、AndroidJavaClassから単に文字列などを取得する他のメソッドはできてます。

[code]

    //sample.java
    public void changeAppBrightness(Context context, int brightness) {
      if (context != null) {
        Window window = ((Activity) context).getWindow();
        WindowManager.LayoutParams lp = window.getAttributes();
        if (brightness == -1) {
          lp.screenBrightness = WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_NONE;
        } else {
          lp.screenBrightness = (brightness <= 0 ? 1 : brightness) / 255f;
        }
        window.setAttributes(lp);
      }
    }
    
[/code]

[code]

    //sample.cs
    private void hoge(){
      AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer"); 
      AndroidJavaObject context = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
      AndroidJavaClass screenMgr = new AndroidJavaClass ("hoge");
      screenMgr.Call("changeAppBrightness",context,100);
    }
    
[/code]


CC BY-SA 3.0
2018-03-06T09:36:22.670
42164
2022-07-30T08:00:10.040
2018-03-06T09:49:43.570
19110
27625
question
android unity3d
Androidネイティブ機能で画面輝度の変更


ここ（<https://qiita.com/GeneralD/items/5e77121cf0b26a103d38>）を参考に  
Unity側とマニフエストだけで完結できました。


CC BY-SA 3.0
2018-03-06T14:33:56.497
42174
2018-03-06T14:33:56.497
27625
42164
answer


Unityで完結 ＋ マニフェストに

uses-permission android:name="android.permission.WRITE_SETTINGS"  
を追加

//sample.cs

[code]

    private void SetBrightness(float brightness){
    #if UNITY_ANDROID　&& !UNITY_EDITOR
            var unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
            var activity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
            activity.Call("runOnUiThread", new AndroidJavaRunnable(() => {
                var window = activity.Call<AndroidJavaObject>("getWindow");
                var lp = window.Call<AndroidJavaObject>("getAttributes");
                lp.Set("screenBrightness", brightness.Value);
                window.Call("setAttributes", lp);
    
                int b = 1 + (int)(Math.Min(1, Math.Max(0, brightness.Value)) * 254f); // 1 <= b <= 255
                var system = new AndroidJavaClass("android.provider.Settings.System");
                var contentResolver = activity.Call<AndroidJavaObject>("getContentResolver");
                system.CallStatic("putInt", contentResolver, system.CallStatic<string>("SCREEN_BRIGHTNESS_MODE"), 0);
                system.CallStatic("putInt", contentResolver, system.CallStatic<string>("SCREEN_BRIGHTNESS"), b);
            }));
    #endif
    }
    
[/code]


CC BY-SA 4.0
2018-03-08T06:46:41.370
42215
2018-11-24T15:19:42.003
2018-11-24T15:19:42.003
728
27625
42164
answer