Websocket paused when android app goes to background
My android app starts a service that opens a websocket to communicate to a remote server. The service spawns a thread whose run method looks like this.
public void run() {
try {
super.run();
for(int i = 1; i < 1000; i++) {
Log.d(TAG, String.format(" *** Iteration #%d.", i));
Thread.sleep(3000); // Dummy load.
mWebsocket.sendTextMessage("Test");
}
}
catch (Exception exc) {
Log.d(MY_TAG, "MyThread.run - Exception: " + exc.getMessage());
}
}
When I turn off the screen or send the app to the background, logcat shows that the loop is running, but the remote server stops receiving the test messages. Apparently, the messages are pooling somewhere because once the app is back to the foreground, the server will received a bunch of test messages. Is this the expected behavior on Android? I've tried different Websocket packages (Autobahn, okhttp3, ...) and the result is the same.
android websocket okhttp3 autobahnws
add a comment |
My android app starts a service that opens a websocket to communicate to a remote server. The service spawns a thread whose run method looks like this.
public void run() {
try {
super.run();
for(int i = 1; i < 1000; i++) {
Log.d(TAG, String.format(" *** Iteration #%d.", i));
Thread.sleep(3000); // Dummy load.
mWebsocket.sendTextMessage("Test");
}
}
catch (Exception exc) {
Log.d(MY_TAG, "MyThread.run - Exception: " + exc.getMessage());
}
}
When I turn off the screen or send the app to the background, logcat shows that the loop is running, but the remote server stops receiving the test messages. Apparently, the messages are pooling somewhere because once the app is back to the foreground, the server will received a bunch of test messages. Is this the expected behavior on Android? I've tried different Websocket packages (Autobahn, okhttp3, ...) and the result is the same.
android websocket okhttp3 autobahnws
add a comment |
My android app starts a service that opens a websocket to communicate to a remote server. The service spawns a thread whose run method looks like this.
public void run() {
try {
super.run();
for(int i = 1; i < 1000; i++) {
Log.d(TAG, String.format(" *** Iteration #%d.", i));
Thread.sleep(3000); // Dummy load.
mWebsocket.sendTextMessage("Test");
}
}
catch (Exception exc) {
Log.d(MY_TAG, "MyThread.run - Exception: " + exc.getMessage());
}
}
When I turn off the screen or send the app to the background, logcat shows that the loop is running, but the remote server stops receiving the test messages. Apparently, the messages are pooling somewhere because once the app is back to the foreground, the server will received a bunch of test messages. Is this the expected behavior on Android? I've tried different Websocket packages (Autobahn, okhttp3, ...) and the result is the same.
android websocket okhttp3 autobahnws
My android app starts a service that opens a websocket to communicate to a remote server. The service spawns a thread whose run method looks like this.
public void run() {
try {
super.run();
for(int i = 1; i < 1000; i++) {
Log.d(TAG, String.format(" *** Iteration #%d.", i));
Thread.sleep(3000); // Dummy load.
mWebsocket.sendTextMessage("Test");
}
}
catch (Exception exc) {
Log.d(MY_TAG, "MyThread.run - Exception: " + exc.getMessage());
}
}
When I turn off the screen or send the app to the background, logcat shows that the loop is running, but the remote server stops receiving the test messages. Apparently, the messages are pooling somewhere because once the app is back to the foreground, the server will received a bunch of test messages. Is this the expected behavior on Android? I've tried different Websocket packages (Autobahn, okhttp3, ...) and the result is the same.
android websocket okhttp3 autobahnws
android websocket okhttp3 autobahnws
edited Oct 17 '17 at 19:34
Chu Bun
asked Oct 16 '17 at 23:50
Chu BunChu Bun
5610
5610
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
If you want this function to be guaranteed to continue to run while your app's UI is in the background, you will need to make that service run as a foreground service. There are some restrictions/guidelines on the use of foreground services, see the documentation at https://developer.android.com/guide/components/services.html.
Alternatively, if this is work that needs to occur on a periodic recurring basis and does not need to run continuously, you may be able to utilize JobScheduler; see https://developer.android.com/topic/performance/scheduling.html.
I just wonder why the loop is still running and output to the logcat, but the messages won't go anywhere. Is this behavior specific to websocket in genernal? to these two websocket packages?
– Chu Bun
Oct 17 '17 at 15:02
Even when the service runs in its own thread, it looks like all network activities not just websocket are stopped when main app goes to the background. I guess to use the network in this case, foreground service must be use.
– Chu Bun
Oct 17 '17 at 19:48
Yes, I really think that you will be better off with a foreground service. That will also avoid times when the Android OS will just kill your background service to reclaim resources for other apps, or to save battery.
– Scott Kronheim
Oct 17 '17 at 23:23
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f46780547%2fwebsocket-paused-when-android-app-goes-to-background%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
If you want this function to be guaranteed to continue to run while your app's UI is in the background, you will need to make that service run as a foreground service. There are some restrictions/guidelines on the use of foreground services, see the documentation at https://developer.android.com/guide/components/services.html.
Alternatively, if this is work that needs to occur on a periodic recurring basis and does not need to run continuously, you may be able to utilize JobScheduler; see https://developer.android.com/topic/performance/scheduling.html.
I just wonder why the loop is still running and output to the logcat, but the messages won't go anywhere. Is this behavior specific to websocket in genernal? to these two websocket packages?
– Chu Bun
Oct 17 '17 at 15:02
Even when the service runs in its own thread, it looks like all network activities not just websocket are stopped when main app goes to the background. I guess to use the network in this case, foreground service must be use.
– Chu Bun
Oct 17 '17 at 19:48
Yes, I really think that you will be better off with a foreground service. That will also avoid times when the Android OS will just kill your background service to reclaim resources for other apps, or to save battery.
– Scott Kronheim
Oct 17 '17 at 23:23
add a comment |
If you want this function to be guaranteed to continue to run while your app's UI is in the background, you will need to make that service run as a foreground service. There are some restrictions/guidelines on the use of foreground services, see the documentation at https://developer.android.com/guide/components/services.html.
Alternatively, if this is work that needs to occur on a periodic recurring basis and does not need to run continuously, you may be able to utilize JobScheduler; see https://developer.android.com/topic/performance/scheduling.html.
I just wonder why the loop is still running and output to the logcat, but the messages won't go anywhere. Is this behavior specific to websocket in genernal? to these two websocket packages?
– Chu Bun
Oct 17 '17 at 15:02
Even when the service runs in its own thread, it looks like all network activities not just websocket are stopped when main app goes to the background. I guess to use the network in this case, foreground service must be use.
– Chu Bun
Oct 17 '17 at 19:48
Yes, I really think that you will be better off with a foreground service. That will also avoid times when the Android OS will just kill your background service to reclaim resources for other apps, or to save battery.
– Scott Kronheim
Oct 17 '17 at 23:23
add a comment |
If you want this function to be guaranteed to continue to run while your app's UI is in the background, you will need to make that service run as a foreground service. There are some restrictions/guidelines on the use of foreground services, see the documentation at https://developer.android.com/guide/components/services.html.
Alternatively, if this is work that needs to occur on a periodic recurring basis and does not need to run continuously, you may be able to utilize JobScheduler; see https://developer.android.com/topic/performance/scheduling.html.
If you want this function to be guaranteed to continue to run while your app's UI is in the background, you will need to make that service run as a foreground service. There are some restrictions/guidelines on the use of foreground services, see the documentation at https://developer.android.com/guide/components/services.html.
Alternatively, if this is work that needs to occur on a periodic recurring basis and does not need to run continuously, you may be able to utilize JobScheduler; see https://developer.android.com/topic/performance/scheduling.html.
answered Oct 17 '17 at 0:11
Scott KronheimScott Kronheim
58746
58746
I just wonder why the loop is still running and output to the logcat, but the messages won't go anywhere. Is this behavior specific to websocket in genernal? to these two websocket packages?
– Chu Bun
Oct 17 '17 at 15:02
Even when the service runs in its own thread, it looks like all network activities not just websocket are stopped when main app goes to the background. I guess to use the network in this case, foreground service must be use.
– Chu Bun
Oct 17 '17 at 19:48
Yes, I really think that you will be better off with a foreground service. That will also avoid times when the Android OS will just kill your background service to reclaim resources for other apps, or to save battery.
– Scott Kronheim
Oct 17 '17 at 23:23
add a comment |
I just wonder why the loop is still running and output to the logcat, but the messages won't go anywhere. Is this behavior specific to websocket in genernal? to these two websocket packages?
– Chu Bun
Oct 17 '17 at 15:02
Even when the service runs in its own thread, it looks like all network activities not just websocket are stopped when main app goes to the background. I guess to use the network in this case, foreground service must be use.
– Chu Bun
Oct 17 '17 at 19:48
Yes, I really think that you will be better off with a foreground service. That will also avoid times when the Android OS will just kill your background service to reclaim resources for other apps, or to save battery.
– Scott Kronheim
Oct 17 '17 at 23:23
I just wonder why the loop is still running and output to the logcat, but the messages won't go anywhere. Is this behavior specific to websocket in genernal? to these two websocket packages?
– Chu Bun
Oct 17 '17 at 15:02
I just wonder why the loop is still running and output to the logcat, but the messages won't go anywhere. Is this behavior specific to websocket in genernal? to these two websocket packages?
– Chu Bun
Oct 17 '17 at 15:02
Even when the service runs in its own thread, it looks like all network activities not just websocket are stopped when main app goes to the background. I guess to use the network in this case, foreground service must be use.
– Chu Bun
Oct 17 '17 at 19:48
Even when the service runs in its own thread, it looks like all network activities not just websocket are stopped when main app goes to the background. I guess to use the network in this case, foreground service must be use.
– Chu Bun
Oct 17 '17 at 19:48
Yes, I really think that you will be better off with a foreground service. That will also avoid times when the Android OS will just kill your background service to reclaim resources for other apps, or to save battery.
– Scott Kronheim
Oct 17 '17 at 23:23
Yes, I really think that you will be better off with a foreground service. That will also avoid times when the Android OS will just kill your background service to reclaim resources for other apps, or to save battery.
– Scott Kronheim
Oct 17 '17 at 23:23
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f46780547%2fwebsocket-paused-when-android-app-goes-to-background%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown