简体   繁体   中英

Why the Service still ( Seem to Be )alive after I call stopService

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.

You do not show it in your code, but it sounds like you still have an active listener for socketIO. In onDestroy , you need to call off and disconnect on the Socket you previously created in initSocketIO . Also, if you started any background threads yourself in the service, you need to make sure they are shutdown in onDestroy .

Service is destroyed , but the listener of Socket.IO is still alive . Remember to close Socket.IO and shutdown the background thread.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM