Android Question Turn On Screen Programmatically

Marcos Alves

Well-Known Member
Licensed User
Longtime User
Hello all,

besides there are some answers to threads in this forum those say that it's not possible to turn on the screen programmaticaly, I found this:

https://stackoverflow.com/questions/30090589/turn-on-screen-programmatically-on-android/30090732

This says that there are two ways to do that:

1. Before sdk level 27, using window.addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
2. In sdk level 27 and upper, using this.setTurnScreenOn(true);

There are some additional considerations also about this here: https://developer.android.com/reference/android/R.attr.html#turnScreenOn

Then, I think that creating a java function could be possible, but I still didn't realize how to put everything above together.
Could anybody help? I think that this resource is a "wish" in this forum and there are many questions about this... I'll keep trying but if anybody more experienced with java could help me will be great!

Thanks!
 

Marcos Alves

Well-Known Member
Licensed User
Longtime User
You can use PhoneWakeState to turn the screen on.
Hello @Erel ,

that's exactly what I trie but didn't worked. I used this:

B4X:
    Dim Pws As PhoneWakeState
    Pws.KeepAlive(True)

I noticed also that it's not a specific device limitation because other apps that I have installed are able to turn on the screen when some events are arriving (example: in WhatsApp when I receive a call the screen is turned on... telegram... same... )

Then, I'm trying to implement the function using the java code indicated by google... if you could give me a path to do this, I'll be thankful :)
 
Upvote 0

Marcos Alves

Well-Known Member
Licensed User
Longtime User
How are you calling this code? Are you sure that the code is actually executed?
Yes,

it's really being executed... I put this just before the code that generates a sound and opens an Activity - what is happening... see the complete sub code:

B4X:
Sub Voip_OnIncomingCall(uri As String, callId As Long)
  
    'Log("incoming call -> " & uri)

    Dim Pws As PhoneWakeState
    Pws.KeepAlive(True)
  
    CallSubDelayed(DownQueue,"GetQueue")
    If uri.Contains("52.67.239.109") Then
        'ToastMessageShow ("svc() evt Voip OnIncomingCall", False)
        Dim RadioActive As Boolean = False
        Dim Login As String = ""
        If Kvs.ContainsKey("radioactive") Then
            RadioActive = Kvs.Get("radioactive")
            Login = Kvs.Get("remotelogin")
        End If

        Dim sf As StringFunctions
        Dim ContatoReal As String
        sf.Initialize
        'ContatoReal = sf.Mid(uri,2,sf.Len(uri)-1)
        'ContatoReal = sf.Left(ContatoReal,sf.At(ContatoReal,"<") - 2)
      
        If sf.Left(uri,1) <> Chr(34) Then 'direct call
            Dim PosicaoDt As Int = sf.At(uri,":")
            Dim PosicaoArr As Int = sf.At(uri,"@")     
            ContatoReal = sf.Mid(uri,PosicaoDt + 2,PosicaoArr - PosicaoDt - 1)
        Else
            ContatoReal = sf.Mid(uri,2,sf.Len(uri)-1)
            ContatoReal = sf.Left(ContatoReal,sf.At(ContatoReal,"<") - 2)
        End If
      
        If Not(RadioActive) Or RadioActive And ContatoReal <> Login Then
            'clear radio event if true (needed???) - TODO test!

            If sf.InString(ContatoReal,".") < 0 And sf.Len(ContatoReal) < 5  Then
                Phone.rejectCall
            End If
          
            ToastMessageShow ("Recebendo Chamada!", False)

            If SdkVersion < 23 Then
                Nb.Initialize
              
                Nb.SmallIcon = "oncall"

                Nb.LargeIcon = LoadBitmap(File.DirAssets,"largecall.png")
          
                Nb.ContentTitle = "Chamada Telefônica"
          
                Nb.ContentText =  " Telefone tocando..."
                Nb.SubText = "Clique aqui para atender ou recusar"
          
                Nb.DefaultSound = False
                Nb.setActivity(emchamada)
                Nb.DefaultVibrate = False
                Nb.OnGoingEvent = True
          
                Nb.Notify(1)
            Else
                NotifyV6("largecall.png","Chamada Telefônica","Clique aqui para atender ou recusar","Chamada Telefonica","Clique para atender ou recusar", 1 ,emchamada)
            End If
          
            OnCall=True
            emchamada.Contact = ContatoReal
            emchamada.Incomming = True
            emchamada.VideoFlag = Phone.isVideoCall
            emchamada.ShouldClose = False
            emchamada.Refresh = IsPaused(emchamada)
            StartActivity(emchamada)
            Phone.SpeakerphoneOn = False
        Else 'Radio Call
            If sf.InString(ContatoReal,".") < 0 And sf.Len(ContatoReal) < 5  Then
                Phone.rejectCall
            Else
                Phone.answerCall(200,False)
                Phone.SpeakerphoneOn = True
            End If
            'Will Generate OnCallConnected
          
        End If 

        '    Call.Contact = uri
        '    Call.Incomming = True
        '    Call.VideoFlag = Phone.isVideoCall
        '    Call.ShouldClose = False
        '    Call.Refresh = IsPaused(Call)
        '    StartActivity(Call)
    Else
        Phone.rejectCall
    End If

End Sub

- After Pws we have:

- Notifications
- StartActivity
- StartService
...and so on...

All the code is running as I can see the effects on the device screen. I can see the log also if I connect the device to a computer using USB or wifi. Even when trying using debug, if I put a breakpoint in pws I can see the code stopping there (besides this last is the worst test because the screen can be turned on due many other reasons in debug mode).
Then: the screen isn't being turned on after Pws.KeepAlive(True)
The ONLY consideration that I think that MUST to be done is: the sub is inside the starter module (what I think that shouldn't be a problem, right?)

That's it... let's go to implement the Google Developers code in java or is there a workaround?
 
Upvote 0

sz4t4n

Member
Licensed User
Longtime User
i have tested keepalive and it works ok on my phone (android 9). See attached example project (after you click wake button just turn off screen by the phisick power off button - after 5 sec screen will wake)
 

Attachments

  • wake.zip
    9 KB · Views: 475
Upvote 0

Marcos Alves

Well-Known Member
Licensed User
Longtime User
i have tested keepalive and it works ok on my phone (android 9). See attached example project (after you click wake button just turn off screen by the phisick power off button - after 5 sec screen will wake)
Interesting... there is a peculiarity in your test... I read a requisite in google developers which tells that the Activity that is calling this.setTurnScreenOn(true) MUST to be visible after the screen turn on... in my situation, the keepalive is called in a service and startactivity is called AFTER this.
Maybe my solution could be to change the keepalive to the sub Activity_Resume... I'll test.
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
i have tested keepalive and it works ok on my phone (android 9). See attached example project (after you click wake button just turn off screen by the phisick power off button - after 5 sec screen will wake)
Does not work on my device (Huawei - Android 8)

P.S. It works only if I declare and use PhoneWakeState in an Activity (actually in the B4XMainPage).
 
Last edited:
Upvote 0
Top