M
M
Madion2016-06-19 20:27:09
Android
Madion, 2016-06-19 20:27:09

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

3 answer(s)
A
Artem Gapchenko, 2016-06-19
@artemgapchenko

Foreground Service .

P
Peter, 2016-06-19
@petermzg

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.

C
coden55, 2016-06-21
@coden55

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 question

Ask a Question

731 491 924 answers to any question