Native method

32 views
Skip to first unread message

Peter Bell

unread,
Nov 16, 2019, 1:51:22 PM11/16/19
to CodenameOne Discussions
Hi

I have a native code to set the Brightness of the screen. The code works good if the app is visible  
(see below)
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.

the code is

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

}

Shai Almog

unread,
Nov 16, 2019, 8:49:32 PM11/16/19
to CodenameOne Discussions
Hi,
onDestroy()/onResume() will never be invoked in your code. They are methods of activity which you don't derive (and shouldn't derive).

You can detect suspend/resume via stop()/start() methods of the main class and re-activate the brightness code in start().

Peter Bell

unread,
Nov 17, 2019, 9:04:53 AM11/17/19
to CodenameOne Discussions
thanx   i dont have an idea to put in the stop and start method. Can you give me a clue.
the problem is not that the brightset is not fired but only takes places when my main acitivity is close by hittiing the home button



Op zondag 17 november 2019 02:49:32 UTC+1 schreef Shai Almog:

Shai Almog

unread,
Nov 17, 2019, 9:27:52 PM11/17/19
to CodenameOne Discussions
If you just invoke the brightness native interface in the beginning of the start() method in your main class doesn't that work correctly?
If not I'm not really sure, it's low level native Android behavior I haven't personally touched.

Peter Bell

unread,
Nov 18, 2019, 11:08:57 AM11/18/19
to CodenameOne Discussions
Hi Shai
i tried your advice  and invoked in start()
1 bright.brightset(255);  my app gives black screen
2 also when i  create the class new  my app gives a black screen
    bright = NativeLookup.create(Bright.class);
    bright.brightset(255);
3 when i add this one whwre as bright is first  time -1 and then  when i call brightset brght is 20 or 255
    if (brght > 0 ) {
                  
    bright = NativeLookup.create(Bright.class);
    bright.brightset(brght);
    }

the app works fine and brightset in the app also
but the behavior is the same when i leave the app and come back to it  brightness is only changed when i leave the app again
it looks like the brighset method runs on a different thread and runs only when the app is not active
i checked  with dialog if the code is fired and it does the Bright class is made again


Op maandag 18 november 2019 03:27:52 UTC+1 schreef Shai Almog:

Shai Almog

unread,
Nov 18, 2019, 9:34:04 PM11/18/19
to CodenameOne Discussions
Hi,
a black screen is usually an indication of an exception. The native thread makes sense, try to wrap your native code in the android native event dispatch thread as explained here:

Peter Bell

unread,
Nov 19, 2019, 6:42:54 AM11/19/19
to CodenameOne Discussions
thanx that was the solution

Op dinsdag 19 november 2019 03:34:04 UTC+1 schreef Shai Almog:
Reply all
Reply to author
Forward
0 new messages