How to keep the android client connected to the server even on activity changes and send data to server?

荒凉一梦 提交于 2019-11-27 11:42:26

The code is right. The only mistake was that I was trying to start the service twice once in onServiceConnected() method and the other in onCreate() method.

I changed my code to start the service only in onCreate() method in the launcher activity. The rest of the activities directly bind to the service by using doBindService() method

When a service is bound to an Activity , it gets killed when the Activity is stopped, its like a background service only for the Activity. Hence, to keep it running, implement the onStartCommand() method in your Service class and return this.START_STICKY. and manage you're service in this method.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!