Android Question [SOLVED] [Things] How to use the new 3 APIs to control settings in Android Things

Carlengue

Member
Licensed User
Longtime User
Google added new APIs in Android Things DP6 to control device settings, including display orientation, uses three different APIs to control settings for the screen (display), system time, and available locales.

How can use it in B4A? I use the orignal raspberry PI touch screen Display, and work perfectly using B4A.

Classes:

-ScreenManager, to control different screen-related settings
-TimeManager, to control different time-related settings
-DeviceManager, to control the list of locales available to the device.

https://developer.android.com/things/sdk/apis/settings.html

Thanks.
 
Last edited:

Erel

B4X founder
Staff member
Licensed User
Longtime User
B4X:
Dim ScreenManager, ScreenManagerStatic As JavaObject
ScreenManager.InitializeNewInstance("com.google.android.things.device.ScreenManager", Array(0))
ScreenManagerStatic.InitializeStatic("com.google.android.things.device.ScreenManager")

ScreenManager.RunMethod("setBrightnessMode", Array(ScreenManagerStatic.GetField("BRIGHTNESS_MODE_MANUAL"))
ScreenManager.RunMethod("setBrightness", Array(255))
ScreenManager.RunMethod("lockRotation", Array(ScreenManagerStatic.GetField("ROTATION_90"))

https://developer.android.com/things/reference/com/google/android/things/device/ScreenManager.html
 
Upvote 0

Carlengue

Member
Licensed User
Longtime User
I have tried the code that you have given me and I receive the following error, which the class does not find. I have the latest version of Android Things installed

Error occurred on line: 185 (Main)
java.lang.ClassNotFoundException: com.google.android$things$device$ScreenManager
at anywheresoftware.b4j.object.JavaObject.getCorrectClassName(JavaObject.java:288)
at anywheresoftware.b4j.object.JavaObject.InitializeNewInstance(JavaObject.java:83)
at b4a.Carlos.main._boton_brillo_click(main.java:559)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:710)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:339)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:249)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:139)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:170)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:166)
at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:80)
at android.view.View.performClick(View.java:6294)
at android.view.View$PerformClick.run(View.java:24770)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
** Activity (main) Pause, UserClosed = true **
 
Upvote 0

Carlengue

Member
Licensed User
Longtime User
I ve change the androidthings,jar and when compiling gives this error:

PARSE ERROR:
unsupported class file version 52.0
...while parsing com/google/android/things/AndroidThings.class
1 error; aborting

In the manifest i have this line:

B4X:
AddApplicationText(<uses-library android:name="com.google.android.things"/>)
 
Upvote 0

Carlengue

Member
Licensed User
Longtime User
I've put back the old androidthings.jar and made some modifications
Now the error seems to be of permissions.

B4X:
Caused by: java.lang.SecurityException: Calling process requires permission com.google.android.things.permission.MODIFY_SCREEN_SETTINGS

B4X:
Error occurred on line: 225 (Main)
java.lang.reflect.InvocationTargetException
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4j.object.JavaObject.RunMethod(JavaObject.java:131)
    at b4a.Carlos.main._boton_brillo_click(main.java:600)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:710)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:339)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:249)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:139)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:170)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:166)
    at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:80)
    at android.view.View.performClick(View.java:6294)
    at android.view.View$PerformClick.run(View.java:24770)
    at android.os.Handler.handleCallback(Handler.java:790)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:6494)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Caused by: java.lang.SecurityException: Calling process requires permission com.google.android.things.permission.MODIFY_SCREEN_SETTINGS
    at android.os.Parcel.readException(Parcel.java:2004)
    at android.os.Parcel.readException(Parcel.java:1950)
    at com.google.android.things.internal.device.IDeviceManagementService$Stub$Proxy.setScreenBrightnessMode(IDeviceManagementService.java:385)
    at com.google.android.things.device.ScreenManager.setBrightnessMode(ScreenManager.java:155)
    ... 21 more
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Try to add
B4X:
AddPermission(com.google.android.things.permission.MODIFY_SCREEN_SETTINGS)
to your manifesteditor
 
Upvote 0

Carlengue

Member
Licensed User
Longtime User
Yes i ve test it and the first time it dont work

I ve reboot the RPI3 and now works

Thanks
 
Last edited:
Upvote 0
Top