Answer the question
In order to leave comments, you need to log in
How to make android app work when phone has screen off for a long time?
There is an application that works with WebSocket using this library: https://github.com/crossbario/autobahn-android
When the phone's screen goes blank, after a while (about 30 minutes of inactivity) the application disconnects from the websocket server.
How can I make the application continue to work even if the phone has been inactive for several hours?
Answer the question
In order to leave comments, you need to log in
Of course, you can programmatically set a blocker to turn off the processor, but your next question will be why the battery runs out so quickly.
The following code can start an "indestructible" background service (when the main service starts, the second one with the same id starts in foreground mode and is immediately killed):
public class Service_DontDie extends Service {
@Override
public void onCreate() {
Notification.Builder builder = new Notification.Builder(this)
.setSmallIcon(android.R.drawable.ic_secure);
Notification notification = builder.build();
startForeground(777, notification);
stopForeground(true);
}
}
public class Service_Main extends Service {
@Override
public void onCreate() {
Notification.Builder builder = new Notification.Builder(this)
.setSmallIcon(android.R.drawable.ic_secure);
Notification notification = builder.build();
startForeground(777, notification);
Intent hideIntent = new Intent(this, Service_DontDie.class);
startService(hideIntent);
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question