how to autostart an app at screenOn ?

mst4711

Member
Licensed User
Longtime User
Hi,
is there a way to start an app at the moment a user is unlocking the screen?
I saw something like "Intent.ACTION_SCREEN_ON", but have no glue, how to realize that in B4A.
:sign0085:
Thanks in advance!
 

mst4711

Member
Licensed User
Longtime User
You can also intercept it with the PhoneEvents (ScreenOn and ScreenOff events). Note that the service must be running in order to intercept these events. That means that you need to call Service.StartForeground.

Thank you Erel.
May be I understand (theoretical) what you try to tell me.
But will explain what I try to reach:
I want to have a reminder for some appointments via msgbox ( this is allready finished ). But this reminder should come up each time I unlock my tablet.

So, I have to create a service (start by reboot). In that service I have to create the broadcastreceiver. If the broadcastreceiver receive the ScreenOn broadcast, the service has to start the msgbox. After clicking OK the msgbox disappears and the ExitApplication close the app. Now the broadcastreceiver wait for the next ScreenOn broadcast.

Is this the correct way?
But do not know, how to implement such a service.
Do you have a codesnippet for me, to bring some light in my brain ?:D


Hi you need to set up a broadcastreceiver, you can either write your own library, probably use the reflection library or the easiest one is to use @XverhelstX BroadcastReceiver library

Thank you lagore. Is it easier with that library? But the same questions as above :eek:
 
Upvote 0

mst4711

Member
Licensed User
Longtime User
Hi, now I tried to create a small testproject. Starting a service at boot and installing the receiver. But there is no msgbox starting after unlock screen !!
In Log I can see the event.
Here the code:
'Activity module
Sub Process_Globals
End Sub

Sub Globals
End Sub

Sub Activity_Create(FirstTime As Boolean)
StartService(Starter)
End Sub

Sub Activity_Resume
End Sub

Sub Activity_Pause (UserClosed As Boolean)
End Sub

Service(starter)
'Service module
Sub Process_Globals
Dim PE As PhoneEvents
End Sub

Sub Service_Create
Dim I As Intent
I.Initialize("", "")
I.SetComponent("stm.starter/.main")
StartActivity(I)
PE.Initialize("PE")
' StopService("")
End Sub

Sub Service_Start (StartingIntent As Intent)
End Sub

Sub PE_UserPresent (Intent As Intent)
Log("User present: " & DateTime.Time(DateTime.Now))
Msgbox("Screen ON","")
ExitApplication
End Sub

Sub Service_Destroy
End Sub

Can someone check and find the failures, please?
 
Last edited:
Upvote 0

mc73

Well-Known Member
Licensed User
Longtime User
Msgboxes cannot be shown from inside a service. You could activate a sub in your main activity and fire the msgbox from there.
 
Upvote 0

mc73

Well-Known Member
Licensed User
Longtime User
I think you should spend a little time, reading about the concept of callSubDelayed, here. It is worth the time :)
 
Upvote 0

mst4711

Member
Licensed User
Longtime User
Hi mc73,
tried this:
CallSubDelayed(Main, "fire_msg") in the service
and
Sub fire_msg
Msgbox("Screen ON","")
End Sub
in the main

the msgbox come up if the activity is in foreground, but will not come if the activity is in background.
Any idea?
 
Upvote 0

mc73

Well-Known Member
Licensed User
Longtime User
Sure. If for example, the purpose of your app is to do some things upon turning screen on, you can simply startActivity your main activity instead of callingSubDelayed. Now, I think this is not usually recommended but I think you can give it try. Furthermore, you can have a globally declared variable in your service, set to true when pe_userPresent is triggered. Then, in activity_create of your main (which you started from pe_userPresent), you can check for this variable and show the msgBox :)
 
Upvote 0

mst4711

Member
Licensed User
Longtime User
Hi,
I did startActivity instead of callsubdelay and did the msgbox in Sub Activity_Resume. Then it works with activity in background.
But 1 add question: which command set the activity in background or pause after displaying the msgbox?
 
Upvote 0

mst4711

Member
Licensed User
Longtime User
Hi,
now I have a "running" code.
Only 1 problem is there. If it has nothing to display, it turns until it has to start the msgbox.
count should be raised each time I unlock the tablet. But it's not working.
can someone find my mistake, please?

B4X:
'Activity module (Main)
Sub Process_Globals
   Dim count As Int : count = 0
End Sub
Sub Globals
End Sub
Sub Activity_Create(FirstTime As Boolean)
   CallSubDelayed(Starter, "Service_Create")
End Sub
Sub Activity_Resume
    fire_msg
End Sub
Sub Activity_Pause (UserClosed As Boolean)
End Sub
Sub fire_msg
    Log("fire_msg: " & DateTime.Time(DateTime.Now) & "count=" & count)
    If count = 1 Then
       Msgbox("Screen ON","")
    End If   
    count = count + 1
    If count = 5 Then
      count = 0
    End If
    Activity.Finish
End Sub

'Service module (starter)
Sub Process_Globals
    Dim PE As PhoneEvents
End Sub
Sub Service_Create
    Dim I As Intent         
    I.Initialize("", "")
    I.SetComponent("stm.starter/.main") 
    StartActivity(I)
    PE.Initialize("PE")
End Sub
Sub Service_Start (StartingIntent As Intent)
End Sub
Sub PE_UserPresent (Intent As Intent)
    StartActivity(Main)
End Sub
Sub Service_Destroy
End Sub
 
Last edited:
Upvote 0
Top