弱龄寄事外,委怀在琴书。这篇文章主要讲述Android - WebSocket - 从最近使用的应用程序清除应用程序时,连接从服务中删除相关的知识,希望能为你提供帮助。
我已经创建了一个android服务来保持WebSocket连接。我正在从MainActivity调用该服务。因此,当我打开应用程序并从WebSocket服务器获取消息时建立连接。如果我最小化应用程序,那个时候也有连接。这可以。
如果我从最近使用的应用程序清除应用程序,则连接将与服务器断开连接。但我的服务一直在我的设备上运行。
【Android - 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 staticString 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);
}
}
主要信息
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() {
.....
.....
答案使用服务,但在前台,例如:https://www.101apps.co.za/articles/services-tutorials-part-2-a-foreground-service.html
推荐阅读
- Android(滚动并不是一直到底)
- 在WebsocketAdapter中获取ServletMapping [Jetty]
- 无法从android okhttp3连接到节点websocket
- Android View和ViewGroup
- 如何在Android屏幕上识别视图移动()
- 无法在Asynctask Android,java中调用View
- 调用ondraw后重置画布,然后再在android中调用它
- 在Android应用中正确加载资源(使用openGL)
- 是否有适用于Android的图像编辑SDK [关闭]