Android Question UDP while Sleep [solved]

FlyingEagle

Member
Licensed User
Longtime User
Hi there,

I have a service knitted in my app that controls the UDP communication.
so far so good, BUT, when the phone display is off, nothing more happens.
switched to on it will be processed all pending request.

I previously thought, the services actually work permanently in the background.

I want the app continuously listening on udp and responds accordingly, whether display on or off.

regards
fly
 
Last edited:

FlyingEagle

Member
Licensed User
Longtime User
I have a service knitted in my app that controls the UDP communication.
so far so good, BUT, when the phone display is off, nothing more happens.
switched to on it will be processed all pending request.
 
Upvote 0

FlyingEagle

Member
Licensed User
Longtime User
Yes.
Phone is a Samsung S4
B4X:
Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("Layout01")
    If FirstTime Then
        StartService(myUDPService)
    End If
    Dim p As PhoneWakeState
    p.KeepAlive(True)
    p.PartialLock
End Sub
Service
B4X:
#Region  Service Attributes
    #StartAtBoot: False
    #StartCommandReturnValue: android.app.Service.START_STICKY
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Dim UDPSocket1 As UDPSocket
End Sub

Sub Service_Create
    ToastMessageShow("Create myUDPService", False)
    Log ("Create myUDPService")
End Sub

Sub Service_Start (StartingIntent As Intent)
    If UDPSocket1.IsInitialized = False Then
        UDPSocket1.Initialize("UDP", 8888, 8000)
    End If
End Sub

Sub Service_Destroy
    ToastMessageShow("Going Down", False)
    Log ("WE ARE GOING DOWN!!!!!!!!!!!!!!")
End Sub

Sub UDP_PacketArrived (Packet As UDPPacket)
    Dim msg As String
   
    msg = BytesToString(Packet.Data, Packet.Offset, Packet.Length, "UTF8")
    msg = msg.Trim

    If msg = "code1" Then
        CallSubDelayed2 (Main, "SetButton", True)
        StartActivity(Main)
    Else
        CallSubDelayed2 (Main, "SetButton", False)
    End If
   
    ToastMessageShow(msg, True)
End Sub
Activity
B4X:
#Region  Project Attributes
    #ApplicationLabel: B4A Example
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.

End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.

    Private Button1 As Button
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("Layout01")
    If FirstTime Then
        StartService(myUDPService)
    End If
    Dim p As PhoneWakeState
    p.KeepAlive(True)
    p.PartialLock
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub SetButton(state As Boolean )
    Button1.Enabled = state
End Sub

Sub Button1_Click
   
End Sub
 
Upvote 0

FlyingEagle

Member
Licensed User
Longtime User
hey eril,

is it possible, that the UDP did not receive broadcast messages wenn the phones display is switched off?

but that's no discription that the screen never goes on "code1" is received directly or by broadcast ...

regards
 
Upvote 0

FlyingEagle

Member
Licensed User
Longtime User
The B4A bridge is connecting while screen is off.
i can also ping the device.
i do not change anything before i write this lines.
 
Upvote 0

FlyingEagle

Member
Licensed User
Longtime User
for everybody who may have the same problems

Activity-View
B4X:
Sub Process_Globals
   Public sNotif As Notification
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("...")
    If FirstTime Then
        StartService(...)
    End If
    Dim r As Reflector
    r.Target = r.GetActivity
    r.Target = r.RunMethod("getWindow")
    r.RunMethod2("addFlags", 6815872, "java.lang.int")
End Sub

Sub Activity_Pause (UserClosed As Boolean)
   If UserClosed = True Then
     Dim p As PhoneWakeState
     p.ReleaseKeepAlive
     p.ReleasePartialLock
   End If
End Sub
Service-View
B4X:
Sub Process_Globals
   Dim UDPSocket1 As UDPSocket
   Dim FirstTime As Boolean = True
End Sub

Sub Service_Start (StartingIntent As Intent)
    If FirstTime = True Then
        Main.sNotif.Initialize
        Main.sNotif.Icon = "icon"
        Main.sNotif.SetInfo("...", "..." ,Main)
        Main.sNotif.Sound = False
        Main.sNotif.Vibrate = False
        Main.sNotif.Notify(1)
      
        UDPSocket1.Initialize("UDP", 8888, 8000)
    End If

    Service.StartForeground(1, Main.sNotif)
    FirstTime = False
  
    Dim p As PhoneWakeState
    p.ReleaseKeepAlive
    p.KeepAlive(True)
    p.ReleasePartialLock
    p.PartialLock
    StartActivity(Main)
End Sub

Sub UDP_PacketArrived (Packet As UDPPacket)
  Dim msg As String

   msg = BytesToString(Packet.Data, Packet.Offset, Packet.Length, "UTF8")
   Main.sNotif.SetInfo ("...", "..." ,Main) ' if you want to ...
   
   ' Maybe you have to uncomment this lines ...
   'Dim p As PhoneWakeState
   'p.ReleasePartialLock
   'p.ReleaseKeepAlive
   'p.KeepAlive(True)
   'p.PartialLock
End Sub
Now the phone switch on the screen if a UDP-Paket was received.
But, keep in mind, by now it seemed not possible to use broadcast-udp-messages while phone's display (sleepmode?) is off ...
 
Last edited:
Upvote 0

ivan.tellez

Active Member
Licensed User
Longtime User
I previously thought, the services actually work permanently in the background.

I want the app continuously listening on udp and responds accordingly, whether display on or off.


Well, I think the problem its here...
B4X:
If msg = "code1" Then
     CallSubDelayed2 (Main, "SetButton", True)
     StartActivity(Main)
Else
     CallSubDelayed2 (Main, "SetButton", False)
End If

This is really simple, The service maybe is "working permanently in the background" BUT, if the screen is off, the object "Main" (Activity) is PAUSED, so, all the CallSubDelayed2 are not processed.


I want the app continuously listening on udp and responds accordingly, whether display on or off.

Part of the app is on pause, and part is working. Use just the part that is working; make the response on the same service, you can proccess the data there even if the screen is off. Use a Notification to prevent android from killing the service and for intereacting with the user.
 
Last edited:
Upvote 0

FlyingEagle

Member
Licensed User
Longtime User
thx for your reply, but there's is one more problem i figured out.
as i said in my last post, i sended broadcast messages, and this seemed to be not working during screen off, now i'm using direct connection and it works.
i also did a log(msg); instead of CallSubDelayed2 (Main, "SetButton", True) - but this were also not fired.
With the code above it works like a charme.
 
Upvote 0
Top