Android Question Turn On Screen Programmatically

Discussion in 'Android Questions' started by Marcos Alves, Aug 8, 2019.

  1. Marcos Alves

    Marcos Alves Active Member Licensed 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:

    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:

    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!

    noeleon likes this.
  2. Erel

    Erel Administrator Staff Member Licensed User

    You can use PhoneWakeState to turn the screen on.
  3. Marcos Alves

    Marcos Alves Active Member Licensed User

    Hello @Erel ,

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

    Dim Pws As PhoneWakeState
    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 :)
  4. Erel

    Erel Administrator Staff Member Licensed User

    How are you calling this code? Are you sure that the code is actually executed?
  5. Marcos Alves

    Marcos Alves Active Member Licensed User


    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:

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

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

    Dim sf As StringFunctions
    Dim ContatoReal As String
    'ContatoReal = sf.Mid(uri,2,sf.Len(uri)-1)
            'ContatoReal = sf.Left(ContatoReal,sf.At(ContatoReal,"<") - 2)
    If sf.Left(uri,1) <> Chr(34Then '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)
                ContatoReal = sf.Mid(
                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
    End If
    ToastMessageShow ("Recebendo Chamada!"False)

    If SdkVersion < 23 Then
                    Nb.SmallIcon = 

                    Nb.LargeIcon = 
                    Nb.ContentTitle = 
    "Chamada Telefônica"
                    Nb.ContentText =  
    " Telefone tocando..."
                    Nb.SubText = 
    "Clique aqui para atender ou recusar"
                    Nb.DefaultSound = 
                    Nb.DefaultVibrate = 
                    Nb.OnGoingEvent = 
    "largecall.png","Chamada Telefônica","Clique aqui para atender ou recusar","Chamada Telefonica","Clique para atender ou recusar"1 ,emchamada)
    End If
                emchamada.Contact = ContatoReal
                emchamada.Incomming = 
                emchamada.VideoFlag = 
                emchamada.ShouldClose = 
                emchamada.Refresh = 
    Phone.SpeakerphoneOn = False
    Else 'Radio Call
                If sf.InString(ContatoReal,".") < 0 And sf.Len(ContatoReal) < 5  Then
    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)
    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?
  6. sz4t4n

    sz4t4n Member Licensed 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)

    Attached Files:

  7. Marcos Alves

    Marcos Alves Active Member Licensed User

    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.
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice