I have a native code to set the Brightness of the screen. The code works good if the app is visible
if i call brightset (20) then the screen is dimmed and when i hit it a can with brightset (255) than the screen is bright
But when i hit the home button and go back to the app and call brightset again the brightness isnt change.
the brightness is changed when i hit the home button. What do i have to add to the code that this is not happening and i can
change the brightness in the app and the brightness is direct visible.
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.provider.Settings;
import android.Manifest;
import android.view.View;
import android.view.WindowManager;
import android.app.Activity;
import android.view.Window;
import com.codename1.impl.android.AndroidNativeUtil.*;
import android.os.Build;
import java.io.IOException;
public class BrightImpl {
private int brightness = 255;
public float brightset(int brightnesss ) {
brightness = brightnesss ;
Context context = com.codename1.impl.android.AndroidNativeUtil.getContext();
// Check whether has the write settings permission or not.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if(!com.codename1.impl.android.AndroidNativeUtil.checkForPermission(Manifest.permission.WRITE_SETTINGS, "Write settings denied")){
return -100;
} else {
changeScreenBrightness(context, brightness );
return 20;
}
} else {
if(!com.codename1.impl.android.AndroidNativeUtil.checkForPermission(Manifest.permission.WRITE_SETTINGS, "Write settings denied")){
return -100;
} else {
changeScreenBrightness(context, brightness );
return 20;
}
}}
public void onDestroy(){
}
public void onResume(){
}
// This function only take effect in real physical android device,
// it can not take effect in android emulator.
private void changeScreenBrightness(Context context, int screenBrightnessValue)
{
// Change the screen brightness change mode to manual.
Settings.System.putInt(context.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE, Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
// Apply the screen brightness value to the system, this will change the value in Settings ---> Display ---> Brightness level.
// It will also change the screen brightness for the device.
Settings.System.putInt(context.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS, screenBrightnessValue);
//refreshes the screen
/* int br = Settings.System.getInt(context.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS);
WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.screenBrightness = (float) screenBrightnessValue / 255;
getWindow().setAttributes(lp);
*/
Activity myactivity = com.codename1.impl.android.AndroidNativeUtil.getActivity();
Window window = myactivity.getWindow();
WindowManager.LayoutParams layoutParams = window.getAttributes();
layoutParams.screenBrightness = screenBrightnessValue / 255f;
window.setAttributes(layoutParams);
myactivity.getWindow().addFlags(WindowManager.LayoutParams.FLAGS_CHANGED);
}
public boolean isSupported() {
return true;
}
}