package com.xscm.moduleutil.utils; import android.content.Context; import android.content.Intent; import android.os.Build; import com.xscm.moduleutil.service.IMConnectionService; public class IMServiceManager { private static IMServiceManager instance; private boolean isServiceStarted = false; private IMServiceManager() { } public static synchronized IMServiceManager getInstance() { if (instance == null) { instance = new IMServiceManager(); } return instance; } public void startIMService(Context context) { if (isServiceStarted) { return; } Intent serviceIntent = new Intent(context, IMConnectionService.class); try { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { context.startForegroundService(serviceIntent); } else { context.startService(serviceIntent); } isServiceStarted = true; } catch (Exception e) { e.printStackTrace(); } } public void stopIMService(Context context) { if (!isServiceStarted) { return; } Intent serviceIntent = new Intent(context, IMConnectionService.class); try { context.stopService(serviceIntent); isServiceStarted = false; } catch (Exception e) { e.printStackTrace(); } } public boolean isServiceStarted() { return isServiceStarted; } }