KeepAlive

peacemaker

Expert
Licensed User
Longtime User
How to keep HTTP background connection by a Service module, using KeepAlive ?
WiFi connection is closed, if the real device is switched off, so B4A-Bridge is off.
GPRS\EDGE connection is alive.
But the service that periodically tries to use HTTP connect seems, does not get response (no sound confirmation).

How KeepAlive can be correctly used that application with a service to be working like an background IM messanger with switched off screen ?
 

peacemaker

Expert
Licensed User
Longtime User
Yes, i use KeepAlive(False).
PDA is working, doesnt sleep, but if to switch the screen by a hardware button - Internet connection is stopped.
WiFi - for sure stopped, GPRS - seems not, but HTTP requests do not go, as i can understand by sound playing - no idea how to debug working after swithing off the PDA.

Emulator's connection is going to work if to click POWER virtual button.

The aim is to make application with a service that has to use HTTP always, like a IM messager.
 
Upvote 0

peacemaker

Expert
Licensed User
Longtime User
After reconnecting to the Bridge the LogCat just shows "connected", no log history from LG P500 device
 
Last edited:
Upvote 0

peacemaker

Expert
Licensed User
Longtime User
Maybe to add something to HTTP around
public abstract boolean keepAlive (HttpResponse response, HttpContext context)
?
 
Upvote 0

peacemaker

Expert
Licensed User
Longtime User
I saw for WIFI also there is a API to control, but GPRS for sure can be used like in IM, but how on b4A. That's the question.
Standard background online service, like IM, like Google's services...
 
Upvote 0

peacemaker

Expert
Licensed User
Longtime User
OK.
More about KeepAlive:
Is it can be called from a Service as well as from an activity ?
What is suspended in Android hardwarely without KeepAlive for sure (sound system, vibration...) ?
I can state that my previous application that uses GPS in a service and sound alerts by MediaPlayer - works good after switching the screen off without KeepAlive: GPS is working and sound is played OK any time i need.

The same service now uses HTTP and similar sound alerts by MediaPlayer by a timer (without GPS).
Service uses PhoneWakeState to KeepAlive, but latest test shows that the PDA sleeps after 5-8 seconds after switching the screen off: no sounds amd internal application's log shows that HTTP connection was lost during the sleep, both WiFi or GPRS.
But if connection lost - there should be a sould alert also, but practically this alert is started just after switching the screen on.

If PDA is powered on - all is OK with switched off screen, WiFi is always connected and application works OK, but it's clear.

Resume: KeepAlive does not work :-( On my PDA. Or i don't know how to use KeepAlive.

Hi, all: who tested KeepAlive on your devices ? Works ?
 
Last edited:
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
KeepAlive can be used from a service.
The purpose of KeepAlive is to prevent the device from dimming the screen and then going to sleep when the user hasn't touched anything. If the user presses on the sleep button KeepAlive will not help.

I'm not familiar with any documented list of components that are active when the device sleeps. I believe that it is not consistent between devices.
 
Upvote 0

peacemaker

Expert
Licensed User
Longtime User
If the user presses on the sleep button KeepAlive will not help.

OK. :-(
But how IM messengers work with HTTP in background if even the screen is off by the user ?



Types of Wake Locks:
FULL_WAKE_LOCK Wake lock that ensures that the screen and keyboard are on at full brightness.
ON_AFTER_RELEASE When this wake lock is released, poke the user activity timer so the screen stays on for a little longer.
PARTIAL_WAKE_LOCK Wake lock that ensures that the CPU is running. The screen might not be on.
SCREEN_BRIGHT_WAKE_LOCK Wake lock that ensures that the screen is on at full brightness; the keyboard backlight will be allowed to go off.
SCREEN_DIM_WAKE_LOCK Wake lock that ensures that the screen is on, but the keyboard backlight will be allowed to go off, and the screen backlight will be allowed to go dim.

public void acquire (long timeout)

How Timeout is set ?
 
Last edited:
Upvote 0

peacemaker

Expert
Licensed User
Longtime User
Yes, please, check, Erel. IMHO it's most needed variant of KeepAlive - to make sure that application works, but the screen (switched off) saves power in any case.
REFLECTION lib for trying this is too complicated to me :-(
 
Upvote 0

splatt

Active Member
Licensed User
Longtime User
I've been trying to use KeepAlive in an application but with no success.

I have a Process Global variable:

dim pws as PhoneWakeState

I have tried to activate it in Sub Create and Sub Resume, but neither seem to work.

Tried on both Galaxy Tab and Galaxy Note.

Can anyone suggest where and how this feature should work?
 
Upvote 0

margret

Well-Known Member
Licensed User
Longtime User
I have this in one of may apps for viewing videos. It works on anything I have used it on, from tablets to phones.

B4X:
'This in Globals
Dim awake As PhoneWakeState

Sub Activity_Resume
      awake.KeepAlive(True)
End Sub

Sub Activity_KeyPress (KeyCode As Int) As Boolean 
      If KeyCode = KeyCodes.KEYCODE_BACK Then
            vv.Stop
            vv.RemoveView
            awake.ReleaseKeepAlive
            Activity.Finish 
       End If
End Sub
 
Upvote 0
Top