I have a Service of My App,and I use it for Socket.IO. I don't want to allow binding,so:
/**
 * You must always implement this method, but if you don't want to allow binding
 * then you should return null.
 */
@Override
public IBinder onBind(@NonNull Intent intent) {
    return null;
}
@Override
public int onStartCommand(@NonNull Intent intent, int flags, int startId) {
    return START_NOT_STICKY;
}
And I init the Socket.IO in the onCreate():
@Override
public void onCreate() {
    super.onCreate();
    mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    Log.d(TAG, "Service Create");
    //...More
    setForeground();
    initSocketIO();
}
private void setForeground() {
    Intent notificationIntent = new Intent(this, LiveTabActivity.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
    //getNotification() is deprecated in API16
    if (Build.VERSION.SDK_INT >= 16) {
        Notification notification = new Notification.Builder(context)
                .setContentTitle(getString(R.string.app_name))
                .setContentText(getString(R.string.service_running))
                .setSmallIcon(R.drawable.defimgs)
                .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.md1))
                .setContentIntent(pendingIntent).build();
        startForeground(100, notification);
        return;
    }
    Notification notification = new Notification.Builder(context)
            .setContentTitle(getString(R.string.app_name))
            .setContentText(getString(R.string.service_running))
            .setSmallIcon(R.drawable.defimgs)
            .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.md1))
            .setContentIntent(pendingIntent).getNotification();
    startForeground(100, notification);
}
@Override
public void onDestroy() {
    super.onDestroy();
    Log.d(TAG, "Service onDestroy");
    //...more
}
In initSocketIO() I init the data of Socket.IO so that I can receive the Message of the server.And when the Service receive the message ,It will have a Log and push a Notification.
After that the app work well.But in this App I want to provide the function for user to logout their account.So when user select to logout this App should stop the Service.
But when I use stopService() in the Activity to stop the Service, I can see the "Service onDestroy" in the LogCat but the Service is still alive! Because I can see the Log in LogCat when the Service receive message from Server!
Even if I use EventBus to send a Event to this Service to make this Service call stopSelf by itself , this problem remains.
So How can I stop the Service which start with Foreground ? Thanks for your help.