Android Question Android 14 not sending network message when App closes.

davemorris

Active Member
Licensed User
Longtime User
I have an APP which communicates with a computer using network sockets (TCP protocol). The computer is only allowed to communicate with one device at a time, therefore when the App is closed (by the user), it sends a special sign-off message to the computer.

Generally, the App works Ok on Android-10 and Android-14 devices. Except when the App is closed then only the Android-10 appears to send the sign-off message.

The software to send the sign-off message is initiated within the Starter service sub Service_TaskRemoved()

Disconnect software in Starter service:
' This event will be raised when the user removes the app from the recent apps list.

Sub Service_TaskRemoved

B4XPages.MainPage.helper.DisconnectFromServer 'TODO This could be throwing and error in Android 14 (at shutdown).

End Sub

The App uses B4X pages, and the software which sends the sign-off message to the computer sits in a class which is declared in the Main Page.

I assume the sign-off software is OK as it works on Android-10, when the App is closed, and will also work if activated manually within the App (on both Android-10 and Android-14 devices).

Perhaps Android-14 has a problem with services - but I am not sure, any input would be appreciated.



Kind regards

Dave
 
Solution
You can't rely on TaskRemoved to be called before the process is killed. You should instead let the client send a ping message every X seconds and remove clients after X * 3 seconds, if they haven't sent anything.

davemorris

Active Member
Licensed User
Longtime User
Hi

Yes, I agree with Erel's response, a keep alive message would solve the situation but TaskRemoved does work on Android-10.

It would appear that the TaskRemoved event cannot be used with certain versions of Android rendering the event handler useless for any important tasks.

Can I ask is there any way that Android could raise and event like "App is about to close" ?

I have also considered using the background event but the disadvantage is that it will close the communications link when the App is still running (but in background).

Currently the ping method appears to be the only real solution which is not Android version dependent.

Kind regards
Dave
 
Last edited:
Upvote 0
Top