Screen brightness

60 views
Skip to first unread message

Peter Bell

unread,
Oct 26, 2019, 2:22:13 PM10/26/19
to CodenameOne Discussions
Hi

How can i change the brightness of the screen in codenameone?

Shai Almog

unread,
Oct 26, 2019, 11:04:10 PM10/26/19
to CodenameOne Discussions
Hi,
we don't have an API for that. You can probably use native interfaces to do that but it's not something apps normally do as it's normally controlled by device sensors.
Are you just trying to disable the screensaver?
Message has been deleted

Peter Bell

unread,
Oct 27, 2019, 7:33:56 AM10/27/19
to CodenameOne Discussions
Hi Shai


No I want save battery’. My gps app runs for hours. 
Do you know a native way, do you have some code?




Op zondag 27 oktober 2019 04:04:10 UTC+1 schreef Shai Almog:
Message has been deleted

Peter Bell

unread,
Oct 27, 2019, 1:14:42 PM10/27/19
to CodenameOne Discussions

i tried the native interface but no luck, i dont got any error messages. but the screen is not dimmed

i put the permission in build hints
<uses-permission android:name="android.permission.WRITE_SETTINGS" />


this is  my code
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.provider.Settings;

import android.view.View;
import android.view.WindowManager;
import android.app.Activity;
import android.view.Window;

public class BrightImpl extends Activity  {
    


    public void brightset(int brightness ) {
 
        
        try {
            Context context = getApplicationContext();
                  // Check whether has the write settings permission or not.
                boolean settingsCanWrite = hasWriteSettingsPermission(context);
 // If do not have then open the Can modify system settings panel.
                if(!settingsCanWrite) {
             changeScreenBrightness(context, brightness);
                }else {
                    changeScreenBrightness(context, brightness);
                }
 
 } catch (Exception e) {}
        
    }
// 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);
       */
        Window window = getWindow();
        WindowManager.LayoutParams layoutParams = window.getAttributes();
        layoutParams.screenBrightness = screenBrightnessValue / 255f;
        window.setAttributes(layoutParams);
       
    }
    
    

       // Check whether this app has android write settings permission.
    private boolean hasWriteSettingsPermission(Context context)
    {
        boolean ret = true;
        // Get the result from below code.
        ret = Settings.System.canWrite(context);
        return ret;
    }
    
    public boolean isSupported() {
        return false;
    }

}



Op zondag 27 oktober 2019 12:33:56 UTC+1 schreef Peter Bell:
Hi Shai


No I want to save battery’. My gps app runs for hours. 

Shai Almog

unread,
Oct 27, 2019, 11:04:02 PM10/27/19
to CodenameOne Discussions
It looks like you're working with really old Android samples. You need to make sure your native code works on a modern device, it's very possible this is no longer accessible to the app.
Message has been deleted

Peter Bell

unread,
Oct 30, 2019, 2:38:17 PM10/30/19
to CodenameOne Discussions
thanx i have changed the code  but it is still not working on my device
my device is LG4c with android 5.0 


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.app.AlertDialog;;
public class BrightImpl extends Activity  {
    


    public void brightset(int brightness ) {
 
        
        try {
            Context context = getApplicationContext();
                  // Check whether has the write settings permission or not.
                  
        if (!com.codename1.impl.android.AndroidNativeUtil.checkForPermission(Manifest.permission.WRITE_SETTINGS, "permission asked "))    {

            new AlertDialog.Builder(context)
    
    .setMessage("Geen permission")
    .setPositiveButton(android.R.string.ok, null)
    .show();
        
        }              else {
                
 
             changeScreenBrightness(context, brightness);}
 
 } catch (Exception e) {}
        
    }
// 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);
       */
        Window window = getWindow();
        WindowManager.LayoutParams layoutParams = window.getAttributes();
        layoutParams.screenBrightness = screenBrightnessValue / 255f;
        window.setAttributes(layoutParams);
         getWindow().addFlags(WindowManager.LayoutParams.FLAGS_CHANGED);
    }
    
    

  
    
    public boolean isSupported() {
        return false;
    }

}


Op maandag 28 oktober 2019 04:04:02 UTC+1 schreef Shai Almog:

Shai Almog

unread,
Oct 30, 2019, 10:55:13 PM10/30/19
to CodenameOne Discussions
If your device is an Android 5 device then the previous code might have worked as it predates the Android 6 update. Still I haven't tried this myself so I'm not sure what could happen there. I suggest connecting a cable and looking through logcat logs. You can also debug on Android Studio if that would help see: https://www.codenameone.com/how-do-i---use-the-include-sources-feature-to-debug-the-native-code-on-iosandroid-etc.html
Message has been deleted

Peter Bell

unread,
Nov 2, 2019, 12:47:25 PM11/2/19
to CodenameOne Discussions
Thanx i tried but logcat didnt give me no clues of the misstakes in the code
i cant die Android studio because i Don’t have abasic account my Paypal dodental work
Can you try the code ?  Maybe it is not externds activity

Shai Almog

unread,
Nov 2, 2019, 10:47:56 PM11/2/19
to CodenameOne Discussions
I'm afraid we can't debug your code for you. We rarely even do that for enterprise accounts.
I suggest adding logging and trying to pinpoint the issue.

Peter Bell

unread,
Nov 3, 2019, 4:32:41 AM11/3/19
to CodenameOne Discussions
Ii didnt ment that you have to debug the code, but do you hve any clue what can be wrong with the code

Op donderdag 31 oktober 2019 03:55:13 UTC+1 schreef Shai Almog:

Peter Bell

unread,
Nov 3, 2019, 12:48:18 PM11/3/19
to CodenameOne Discussions
i still get problems 

1 i cant import com.codename1.impl.android.AndroidNativeUtil

when building on the server i get the problem
Compiling with JDK Java compiler API.
/tmp/build1637479177142233945xxx/TalkingGps/src/main/java/com/Bellproductions/TalkingGps/BrightImpl.java:12: error: cannot find symbol
import com.codename1.impl.android.AndroidNatativeUti;
                                 ^
  symbol:   class AndroidNatativeUti
  location: package com.codename1.impl.android

2 wheb i dont include the import
i cant reach the class BrightImpl
Bright.Issurported doesnt return true


i trigger native interface
     private Bright bright;
    bright = (Bright)NativeLookup.create(Bright.class);

   private void dimlistner() {
      if (dim.isSelected()) {
     Boolean B = true;
     int brght = 55;
     if (bright.isSupported()) {
             
             B = bright.brightset(brght);
     }
             Dialog.show("Check", B.toString() , "OK", null);
         
      } else {
     int brght = 255;
         Boolean B = true;
        if (bright.isSupported()) {
      
         B = bright.brightset(brght);  }
           Dialog.show("Check", B.toString() , "OK", null);
      }
      }      


Bright,java

import com.codename1.system.NativeInterface;



    public interface Bright extends NativeInterface  {
      public boolean  brightset(int brightness) ;
    
}


import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.provider.Settings;

import android.view.View;
import android.view.WindowManager;
import android.app.Activity;
import android.view.Window;
import com.codename1.impl.android.AndroidNativeUtil;


public class BrightImpl extends Activity  {
    


    public boolean brightset(int brightness ) {
 
        
        try {
            Context context = getApplicationContext();
                  // Check whether has the write settings permission or not.
                  
    if (!com.codename1.impl.android.AndroidNatativeUtil.checkForPermissions(Manifest.permission.WRITE_SETTINGS, "permission asked "))    {}              
                
 
             changeScreenBrightness(context, brightness);
 return false;
 
 } catch (Exception e) {}
        return false;
    }
// 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);
       */
        Window window = getWindow();
        WindowManager.LayoutParams layoutParams = window.getAttributes();
        layoutParams.screenBrightness = screenBrightnessValue / 255f;
        window.setAttributes(layoutParams);
         getWindow().addFlags(WindowManager.LayoutParams.FLAGS_CHANGED);
    }
    
    

  
    
    public boolean isSupported() {
        return true;
    }

}




  

    
     
    
    
 


Op zondag 3 november 2019 10:32:41 UTC+1 schreef Peter Bell:

Shai Almog

unread,
Nov 3, 2019, 9:02:52 PM11/3/19
to CodenameOne Discussions
I'm guessing this has something to do with code like this:
(Exception e) {}

If you catch and swallow exceptions things will fail.
You can import the native utils but only in the native code. It doesn't exist in the Codename One space classes only for native compilation.

Peter Bell

unread,
Nov 6, 2019, 2:22:54 PM11/6/19
to CodenameOne Discussions
Stil having problems

1 it was not the exception in the try catch i removed the catch
still no change in brightness

2 i checked if the brightimpl file works with a simple class without extends activity with a dialog return the code works

Bright.java
Public float brightset(int brightness)

BrightImpl,Java

Public float brightset(int brightness) { return 0}

The return i display in a dialog

3 the simple code With extends activity doesnt work

The question is should it work

4 the code With the brightness change (See last post) doesnt work

Any clues where the problems lies?

Shai Almog

unread,
Nov 6, 2019, 9:55:55 PM11/6/19
to CodenameOne Discussions
No. Native interfaces don't extend activity and it shouldn't work. It's a different lifecycle.
Native interfaces are objects we create. An activity is something the OS needs to create. These are very different things. We create the native CodenameOneActivity and provide you various tools to work with it e.g. AndroidNativeUtils etc. see https://www.codenameone.com/manual/advanced-topics.html

Peter Bell

unread,
Nov 8, 2019, 5:35:29 AM11/8/19
to CodenameOne Discussions
thanx Shai for the link

i solved the puzzle

here is the code which works
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   { 

    
   
    public float brightset(int brightness ) {
 
        

      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;
     }
            
            
}}
   
    
   // 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;
    }

}



Op donderdag 7 november 2019 03:55:55 UTC+1 schreef Shai Almog:
Reply all
Reply to author
Forward
0 new messages