Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Connection Drops after 15 mins #1

Open
doomed2020 opened this issue Feb 18, 2018 · 4 comments
Open

Connection Drops after 15 mins #1

doomed2020 opened this issue Feb 18, 2018 · 4 comments

Comments

@doomed2020
Copy link

doomed2020 commented Feb 18, 2018

Hi,

While I was testing on Android 5.1.1 (Lollipop), if the mobile remains Idle for around 15 minutes, the Connection of the Client with the Server Drops and never Re-Connects. Even if the Server is Restarted, it does not connect. The Client is still running in the background. Even if I re-open the client, there is still no connection. The Only way to re-establish the connection is to End Task the client from background and then re-open the app, then the connection is established but again for no more than 15 mins of Idle time.

I have checked the code, it should be running as a "service" and "start_sticky" should make the 'NetworkHandler.class' to restart once it is destroyed or loses connection. May be it needs a "Keep-Alive" kind of a function to keep the connection active with the server for as long as possible and once the connection is dropped, start the process again.

@AdvancedHacker101
Copy link
Owner

This is the hardest problem with all of my android apps.
Basically android is improving battery life, optimizing the system etc.
Behind the scenes background services get stopped and not restarted. Trust me I've done a lot of research including testing the connection while in school with a GPS tracker project. Even if you manage to restart the background process, the code will pause on the connect, because android stops some of the hardware components to save battery life.
Another option I tired was FCM (Firebase Cloud Messaging) where the server downstreams data to the device, and the device doesn't need constant connection to the server.
The problem here is, that FCM offers 2 types of message delivery:

  • Background
  • Notification

Obvliously I went for background, because bacgkround messages are sent to the application, and the application then handles the data. The problem here is the same as before: Not every background message reaches the device. A little browsing on the internet shows that many people have this problem, but no solution exists. FCM works stable with Notification mode, which is OK for Chat, Calendar applications and Games. Your application only knows of the notification when it's pressed by the user and it will open your application.

Conslusion

We can try but I think nothing will work, I even saw more strict rules on background services with android 8.1 Oreo, where not even holding a wakelock can keep the device awake, because it is automatically released. Basically android's idea is we only need to handle data when the user is using the phone, otherwise save battery and only FCM notifications can work, which again, needs user action for the application to handler the data. And some help forums now ask what is the purpose of constant connection, instead of trying to fix the problem. If you try to wake the device say every 5 seconds that's inconsistent too. After a while the device just stops some hardware components and only turns them on when the users wake their phones.

@doomed2020
Copy link
Author

Some Apps on startup ask for Permission to be placed in Battery Optimization, which allows them to run in the background.

private void openPowerSettings(Context context) {
    Intent intent = new Intent();
    intent.setAction(Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS);
    context.startActivity(intent);
}

The app has to be added manually by the user. Also try to make the app "Device Admin" as well, there is a chance these apps will not stop working in the background if the device is locked or idle.

https://developer.android.com/guide/topics/admin/device-admin.html
https://stackoverflow.com/questions/41596509/android-m-startactivity-battery-optimization

@cexdelta
Copy link

some progress with this?

@vqcate
Copy link

vqcate commented Apr 29, 2023

yk how i can use it and download it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants