Android 如何启动类似socket.io的前台服务以保持连接并侦听消息
我正在使用此源应用程序与其他设备聊天。但是如何让它像一个服务一样启动,这样我就可以启动前台服务了。 我是否需要在服务中重写MainFragment和LoginActivity socket.io应用程序 我曾在SocketService类中尝试过类似的方法,我还需要在应用程序的服务中包含其他内容,以便在应用程序关闭时获取通知消息Android 如何启动类似socket.io的前台服务以保持连接并侦听消息,android,socket.io,android-service,foreground-service,Android,Socket.io,Android Service,Foreground Service,我正在使用此源应用程序与其他设备聊天。但是如何让它像一个服务一样启动,这样我就可以启动前台服务了。 我是否需要在服务中重写MainFragment和LoginActivity socket.io应用程序 我曾在SocketService类中尝试过类似的方法,我还需要在应用程序的服务中包含其他内容,以便在应用程序关闭时获取通知消息 public class SocketService extends Service { private Socket mSocket; public
public class SocketService extends Service {
private Socket mSocket;
public static final String TAG = SocketService.class.getSimpleName();
private static final String NOTIFICATION_CHANNEL_ID_DEFAULT = "App running in background";
String GROUP_KEY_WORK_EMAIL = "com.android.example.WORK_EMAIL";
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
throw null;
}
@Override
public void onCreate() {
super.onCreate();
Toast.makeText(this, "on created", Toast.LENGTH_SHORT).show();
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setGroup(GROUP_KEY_WORK_EMAIL);
Notification notification = builder.build();
NotificationCompat.BigTextStyle bigTextStyle = new NotificationCompat.BigTextStyle();
// Set big text style.
builder.setStyle(bigTextStyle);
startForeground(3, notification);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Toast.makeText(this, "start command", Toast.LENGTH_SHORT).show();
try {
mSocket = IO.socket(Constants.CHAT_SERVER_URL);
} catch (URISyntaxException e) {
throw new RuntimeException(e);
}
mSocket.on("newMessageReceived", onNewMessage);
mSocket.connect();
return START_STICKY;
}
private Emitter.Listener onNewMessage = new Emitter.Listener() {
@Override
public void call(Object... args) {
JSONObject data = (JSONObject) args[0];
String username;
String message;
try {
username = data.getString("username");
message = data.getString("message");
} catch (JSONException e) {
Log.e(TAG, e.getMessage());
return;
}
Log.d(TAG, "call: new message ");
setNotificationMessage(message, username);
}
};
public void setNotificationMessage(CharSequence message, CharSequence title) {
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setSmallIcon(R.drawable.ic_launcher);
builder.setContentTitle(title);
builder.setContentText(message);
NotificationManagerCompat nm = NotificationManagerCompat.from(this);
nm.notify(3, builder.build());
}
}
当你的应用程序在后台时,你不应该使用前台服务来获取通知消息 相反,你应该使用 但是,如果您仍然需要在前台服务中使用套接字连接
SocketManager socketManger = SocketManger.getSocketManger();
@Override
public void onCreate() {
socketManger.init(this::onSocketEvent);
}
public void onSocketEvent(boolean connect){
//your code when the socket connection connect or disconnect
}
只需创建一个singleton类来处理所有套接字连接,并将其用于前台服务,如下所示
public class SocketManger {
private static SocketManger socketManger;
Socket socket;
Callback<Boolean> onConnect;
public void init(Callback<Boolean> onConnect){
this.onConnect = onConnect;
connectToSocket();
listenToPublicEvents();
}
private void connectToSocket(){
try{
IO.Options opts = new IO.Options();
//optional parameter for authentication
opts.query = "token=" + YOUR_TOKEN;
opts.forceNew = true;
opts.reconnection = true;
opts.reconnectionDelay = 1000;
socket = IO.socket(YOUR_URL, opts);
socket.connect();
}
catch(URISyntaxException e){
throw new RuntimeException(e);
}
}
private void listenToPublicEvents(){
socket.on(Socket.EVENT_CONNECT, args -> {
if(onConnect!=null)
onConnect.onResult(true);
} );
socket.on(Socket.EVENT_DISCONNECT, args ->{
if(onConnect!=null)
onConnect.onResult(false);
});
}
public void emit(String event, JSONObject data, Ack ack){
socket.emit(event, new JSONObject[]{data}, ack);
}
public void on(String event, Emitter.Listener em){
socket.on(event, em);
}
public static SocketManger getSocketManger() {
if(socketManger == null){
socketManger = new SocketManger();
}
return socketManger;
}
public boolean isConnected(){
return socket!=null && socket.connected();
}
public void onDestroy() {
onConnect = null;
socket.disconnect();
}
public interface Callback<T> {
void onResult(T t);
}
}
并确保在服务被破坏时断开插座
@Override
public void onDestroy() {
socketManger.onDestroy()
super.onDestroy();
}
如果希望在应用程序启动后立即启动服务,可以从onCreate方法中的自定义应用程序类启动它 或者您可以从任何活动(例如,从onCreate方法)启动它,以防您希望从特定活动启动服务 或者,当设备启动时,您可以从BroadcastReceiver启动。在这种情况下,请使用BOOT_COMPLETED操作: 要启动您的服务,只需在您想启动服务的任何地方使用以下代码:
Intent intent = new Intent(context, SocketService.class);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
context.startForegroundService(intent);
} else {
context.startService(intent);
}