Android Question setBrightness on Android 9

ilan

Expert
Licensed User
Longtime User
hi this code is crashing on android 9

B4X:
Sub screenBrightness(brightnessInt As Int)     '0..255
   Dim J As JavaObject
   J.InitializeContext
   J.RunMethod("setBrightness",Array(J,brightnessInt))
End Sub

i think i need to grant write settings permission but how?

https://www.dev2qa.com/how-to-grant-write-settings-permission-in-android/

runtime permission does not have such a permission.

thanx, ilan
 

DonManfred

Expert
Licensed User
Longtime User
runtime permission does not have such a permission.
Just just the string when using runtimepermission.

B4X:
rp.CheckAndRequest("android.permission.WRITE_SETTINGS")
 
Upvote 0

ilan

Expert
Licensed User
Longtime User
Just just the string when using runtimepermission.

B4X:
rp.CheckAndRequest("android.permission.WRITE_SETTINGS")

no this wont work. in Overlay Window from @Informatix he is asking on start for permission that will open the app settings and allow you to enable the overlay setting of that app. so we need something that will open the write settings window of the app like in the link:

https://www.dev2qa.com/how-to-grant-write-settings-permission-in-android/

the runtimepermission is not doing it.

i am sure we can do it with JavaObject but how? :)

EDIT: maybe this will help: https://stackoverflow.com/questions/35053932/write-settings-permission-ambiguity/35220968
 
Last edited:
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Upvote 0

ilan

Expert
Licensed User
Longtime User
No javaobject needed. B4A should work

Try

B4X:
    Dim in As Intent
    in.Initialize("android.settings.action.MANAGE_WRITE_SETTINGS","")
    StartActivity(in)

thanx donmanfred, this will open all apps. i would like to open only the relevant app. i tried like this but i get a crash:

B4X:
            Dim i As Intent
            i.Initialize("android.settings.action.MANAGE_WRITE_SETTINGS","")
            i.SetPackage("www.sagital.scrshield")
            StartActivity(i)

*** Service (starter) Create ***
6
jd: 2458565.1024959492
Freitag:
17:53 | 16:27:35
zmanit:0
shabbat = false
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = true **
*** Service (screenshieldsrv) Create ***
** Service (screenshieldsrv) Start **
** Activity (settings) Create, isFirst = true **
Panel size is unknown. Layout may not be loaded correctly.
ApproximateScreenSize: 5.143260152860246
** Activity (settings) Resume **
settings$ResumableSub_chk_Clickresume (java line: 681)
android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.settings.action.MANAGE_WRITE_SETTINGS flg=0x20000 pkg=www.sagital.scrshield }
at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:2018)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1673)
at android.app.Activity.startActivityForResult(Activity.java:4689)
at android.app.Activity.startActivityForResult(Activity.java:4647)
at android.app.Activity.startActivity(Activity.java:5008)
at android.app.Activity.startActivity(Activity.java:4976)
at anywheresoftware.b4a.keywords.Common.StartActivity(Common.java:859)
at www.sagital.scrshield.settings$ResumableSub_chk_Click.resume(settings.java:681)
at www.sagital.scrshield.settings._chk_click(settings.java:622)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:196)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:180)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:176)
at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:80)
at android.view.View.performClick(View.java:7352)
at android.widget.TextView.performClick(TextView.java:14177)
at android.widget.CompoundButton.performClick(CompoundButton.java:143)
at android.view.View.performClickInternal(View.java:7318)
at android.view.View.access$3200(View.java:846)
at android.view.View$PerformClick.run(View.java:27801)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7045)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:964)
 
Upvote 0

ilan

Expert
Licensed User
Longtime User
the second problem is that with RuntimePermission i am not getting the right value of the Permission status. i am still getting FALSE even if i have enabled it manually.

i tried:

B4X:
CheckAndRequest("android.settings.action.MANAGE_WRITE_SETTINGS")

and:

B4X:
CheckAndRequest("android.permission.WRITE_SETTINGS")

but none of them worked.

so we need to find a way how to get the right value if the permission was enabled and if not open the settings window of that app to enable it manually.

thanx.
 
Upvote 0

ilan

Expert
Licensed User
Longtime User
this is the OverlayPermission java code from @Informatix overlaywindow lib:

B4X:
package b4a.flm.overlaywdw;

import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.provider.Settings;
import anywheresoftware.b4a.BA;
import anywheresoftware.b4a.IOnActivityResult;

@BA.ShortName("OverlayPermission")
@BA.Events(values = {
        "DrawPermission(Allowed As Boolean)"
})
public class OverlayPermission
{
    IOnActivityResult iOnActivityResult;

    /** Requests the permission to draw overlays over applications for Android versions >= 23. Returns whether a permission is required or not. */
    public boolean RequestPermission(final BA ba, final String EventPrefix)
    {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && !Settings.canDrawOverlays(ba.context)) {
            Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, Uri.parse("package:" + BA.applicationContext.getPackageName()));
            iOnActivityResult = new IOnActivityResult() {
                @Override
                public void ResultArrived(int resultCode, Intent intent) {
                    ba.raiseEvent(this, EventPrefix.toLowerCase(BA.cul) + "_drawpermission", new Object[] { Settings.canDrawOverlays(ba.context) });
                }
            };
            ba.startActivityForResult(iOnActivityResult, intent);
            return true;
        }
        return false;
    }

    /** Returns whether this application is allowed to draw overlays over applications. */
    public boolean IsAllowed(BA ba)
    {
        return (Build.VERSION.SDK_INT < Build.VERSION_CODES.M || Settings.canDrawOverlays(ba.context));
    }
}

this may help.
 
Upvote 0

ilan

Expert
Licensed User
Longtime User
ok now this will open the setting ONLY for the sending App:

B4X:
            Dim i As Intent
            i.Initialize("android.settings.action.MANAGE_WRITE_SETTINGS","package:" & Application.PackageName)
            StartActivity(i)

but how do we check now if the permission is enabled and if not open the settings window??
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
i tried like this but i get a crash:
Manifesteditor
B4X:
AddManifestText(<uses-permission android:name="android.permission.WRITE_SETTINGS" />)
AddActivityText(Main,
<activity android:name=".permission.WriteSettingsPermissionActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
)

B4X:
    Dim in As Intent
    in.Initialize("android.settings.action.MANAGE_WRITE_SETTINGS","")
    in.SetComponent(Application.PackageName)
    StartActivity(in)
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
but how do we check now if the permission is enabled and if not open the settings window??

you probably can do it with javaobject.

i tried it with inline java

B4X:
    Dim in As Intent
    in.Initialize("android.settings.action.MANAGE_WRITE_SETTINGS","package:" & Application.PackageName)
    StartActivity(in)

B4X:
#If JAVA
import android.provider.Settings;

public boolean canWrite() {
   boolean canwrite = android.provider.Settings.System.canWrite(this);
     return canwrite;
}
#End If
B4X:
    Dim canWrite As Boolean = NativeMe.RunMethod("canWrite", Null)
    Log(canWrite)
 
Upvote 0
Top