Android - WebSocket - 从最近使用的应用程序清除应用程序时,连接从服务中删除
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android - WebSocket - 从最近使用的应用程序清除应用程序时,连接从服务中删除相关的知识,希望对你有一定的参考价值。
我已经创建了一个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);
}
}
主要信息
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 - WebSocket - 从最近使用的应用程序清除应用程序时,连接从服务中删除的主要内容,如果未能解决你的问题,请参考以下文章
如何将音频数据从 Android 流式传输到 WebSocket 服务器?