【发布时间】:2018-12-17 04:30:28
【问题描述】:
我创建了一个 android 服务来保持 WebSocket 连接。我正在从 MainActivity 调用服务。因此,当我打开应用程序并从 WebSocket 服务器获取消息时建立了连接。如果我最小化应用程序,那个时候也有连接。这很好。
如果我从最近使用的应用程序中清除该应用程序,则与服务器的连接会断开。但我的服务一直在我的设备上运行。
Screenshot - Clearing recently used apps
服务类
package com.myapp;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.util.Log;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.WebSocket;
public class WebSocketService extends Service {
public static final String TAG = WebSocketService.class.getSimpleName();
private static String SOCKET_ADDR = "ws://pc-websocket:8080/WebSocket/Endpoint";
WebSocket socket;
private OkHttpClient client;
public WebSocketService() {
super();
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if(socket==null) {
new Thread(new Runnable() {
public void run() {
connectWSocket(SOCKET_ADDR);
}
}).start();
}
return START_STICKY;
}
private void connectWSocket(String SOCKET_ADDR) {
Log.i(TAG, "connectToPASocket()");
client = new OkHttpClient();
Request request = new Request.Builder().url(SOCKET_ADDR).build();
EchoWebSocketListener listener = new EchoWebSocketListener(this);
socket = client.newWebSocket(request, listener);
}
}
WebSocketListener
package com.myapp;
import android.content.Context;
import android.support.v4.app.NotificationCompat;
import android.app.NotificationManager;
import okhttp3.Response;
import okhttp3.WebSocket;
import okhttp3.WebSocketListener;
import okio.ByteString;
public final class EchoWebSocketListener extends WebSocketListener {
private static final int NORMAL_CLOSURE_STATUS = 1000;
private Context context;
public EchoWebSocketListener(){
}
public EchoWebSocketListener(Context context){
this.context=context;
}
@Override
public void onOpen(WebSocket webSocket, Response response) {
}
@Override
public void onMessage(WebSocket webSocket, String message) {
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this.context, null)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(message)
.setContentText(message)
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(message))
.setPriority(NotificationCompat.PRIORITY_DEFAULT);
NotificationManager notificationManager = (NotificationManager) this.context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(123, mBuilder.build());
}
@Override
public void onMessage(WebSocket webSocket, ByteString bytes) {
}
@Override
public void onClosing(WebSocket webSocket, int code, String reason) {
webSocket.close(NORMAL_CLOSURE_STATUS, null);
}
@Override
public void onFailure(WebSocket webSocket, Throwable t, Response response) {
webSocket.close(NORMAL_CLOSURE_STATUS, null);
}
}
MainActivity
package com.myapp;
import android.app.ActivityManager;
import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent mServiceIntent = new Intent(getApplicationContext(), WebSocketService.class);
startService(mServiceIntent);
}
}
我需要这方面的帮助。有没有其他方法可以保持 WebSocket 连接?
我尝试连接 AsyncHttpClient。但同样的问题。
AsyncHttpClient.getDefaultInstance().websocket("ws://pc-websocket:8080/WebSocket/Endpoint",
null, new AsyncHttpClient.WebSocketConnectCallback() {
.....
.....
【问题讨论】:
-
根据您使用的服务,这实际上是操作系统的正确行为,请在此处查看“意图服务”:developer.android.com/training/run-background-service/…
-
感谢您的回复@Mohammed。我在“意向服务”中也遇到了同样的问题。
-
你检查过你是否在ping服务器吗?我以前做过,但我自己实现了 websocket,所以 ping/pong 功能是可调试的。
-
@Mohammed 好难受。我可以连接服务器。如果我从最近使用的应用程序中关闭该应用程序,那么它只会关闭。
标签: java android websocket android-service