Re: [android-developers] Re: Socket sharing between activities
Can't say what's happening, but: if you took inspiration from there, you are creating the service as auto_create, which means that it stays around unless there are no bound activities anymore. Assuming that you are unbinding it on the onDestroy, the thread contained in the service may be still running (an thus, receiving data) during the transition from A to B. Remember also that if you don't unregister the activity A callback, it will kept being called even if activity A is not visible anymore. Sorry I couldn't help anymore, Federico On Thu, Apr 19, 2012 at 7:47 PM, Tonez apires...@gmail.com wrote: Hi guys, Thanks to everyone for the prompt responses. I've studied up on Services / IntentServices as well as how I would go about using a Singleton to satisfy my goal and have decided to give both a try to take the experiential learning route. I've started with Services and have managed to get my app working like it should with binding an activity in view with the Service running my network thread. I have a bug though which I'm having trouble resolving in activity 'A' I'm using the bindService method to start my service so as to communicate back and forth between the Service and the activity. I've created my service connection like so: private CallBackActivityAInterface _service = null; private ServiceConnection serviceConnection = new ServiceConnection() { public void onServiceConnected(ComponentName className, IBinder binder) { _service = (CallBackActivityAInterface)binder; _service.registerActivityACallBack(activityACallBack); } public void onServiceDisconnected(ComponentName className) { _service = null; } } In my onServiceConnected method above I pass in a call back instance to the service so it can communicate with activity A from the network worker thread it is running which is waiting for incoming tcp data. This is what the CallBackActivityAInterface implementation looks like in activity A: private CallBackActivityAInterface activityACallBack = new CallBackActivityAInterface() { @Override public void receivedData() { runOnUiThread(new Runnable() { @Override public void run() { Log.d(DEBUG, hoorah we have a response); } }); } }; This works fine, when I invoke _activityACallBack.receivedData() in my service from within the worker thread that's waiting for tcp data I get my debug output above. It's worth noting for clarification sake that the callback instance I pass in to my _service.registerActivityACallBack(..) method in my service is assigned to a global variable of type CallBackActivityAInterface within my service class. In activity B I have the same sort of implementation as you see above where I create it's own service connection instance and pass in a callback to the service so it can communicate with activity B. When I navigate to activity B from activity A, I first unbind my service connection from the running service in activity A and then use the bindService method in activity B with it's own service connection. The issue I'm having is the callback instance I'm sending through to the service in activity B's onServiceConnected method is null when I try use it from within the worker thread that is currently running in the service. This is the callback implementation I have in activity B: private CallBackActivityBInterface _service = null; private ServiceConnection serviceConnection = new ServiceConnection() { public void onServiceConnected(ComponentName className, IBinder binder) { _service = (CallBackActivityBInterface)binder; _service.registerActivityBCallBack(activityBCallBack); } public void onServiceDisconnected(ComponentName className) { _service = null; } } private CallBackActivityBInterface activityBCallBack = new CallBackActivityBInterface() { @Override public void receivedDataInActivityB() { runOnUiThread(new Runnable() { @Override public void run() { Log.d(DEBUG, hoorah we have a response in activity B); } }); } }; Stepping through the code reveals that activityBCallBack is not null when I inspect it within _service.registerActivityBCallBack(..), however the global variable I assign activityBCallBack to in my _service.registerActivityBCallBack(..) method is null when I try use it from within my worker thread that is already running. Any idea why this would be null? I suspect it may be null because the worker thread is started before activity B has a
Re: [android-developers] Re: Socket sharing between activities
Thanks again for the clarifications. Federico On Wed, Apr 18, 2012 at 1:26 AM, Dianne Hackborn hack...@android.com wrote: On Tue, Apr 17, 2012 at 12:03 AM, Federico Paolinelli fedep...@gmail.com wrote: Not using a service will make the connection available to be killed if the application goes background. Is that correct? Yes. And, if we want to close the connection when the application is no longer visible, where should the connection be closed? The onStop() gets called when the app goes background but also when the activity gets destroyed because the user pressed the back button to get to a previous activity (and in this case we don't want to close the connection). You just want to keep track of the number of active clients to decide when you should be running. Have two methods: class MyConnection { private mStartCount; void start() { mStartCount++; if (mStartCount == 1) { // Bring up connection } } void stop() { mStartCount--; if (mStartCount == 0) { // Bring down connection } } } And call these in each onStart() and onStop() of the activities that are using the connection. -- Dianne Hackborn Android framework engineer hack...@android.com Note: please don't send private questions to me, as I don't have time to provide private support, and so won't reply to such e-mails. All such questions should be posted on public forums, where I and others can see and answer them. -- You received this message because you are subscribed to the Google Groups Android Developers group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en -- Federico -- You received this message because you are subscribed to the Google Groups Android Developers group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en
Re: [android-developers] Re: Socket sharing between activities
Thanks Dianne for the clarification. I always thought that every task should have been performed in a native android component, was it an activity or a service. I am quite curious about this topic. Now, let's assume that his application does not need to fetch the data while in background, and that in his singleton class runs a thread that reads from the stream. What is going to happen when the application goes in background because, for example, the user receives a phone call? From my understanding (which may be very poor), the onStop() method of the visible activity gets called BUT the process will still be active, and with that the thread that reads from the stream. The singleton will be destroyed only when the system needs resources to be allocated for another application and it chooses our app as the one to be killed. On the other hand, in case no resources are needed, the singleton will still be around fetching data from the stream. Not using a service will make the connection available to be killed if the application goes background. Is that correct? And, if we want to close the connection when the application is no longer visible, where should the connection be closed? The onStop() gets called when the app goes background but also when the activity gets destroyed because the user pressed the back button to get to a previous activity (and in this case we don't want to close the connection). Sorry to bother, but I am working on similar stuff and I need to clear my head a bit :-) Thanks again for the support, Federico On Tue, Apr 17, 2012 at 4:10 AM, Dianne Hackborn hack...@android.com wrote: You don't need a service. And please please don't use startService() unless you really want to use up the user's resources with your app trying to run when they are not in it. Way too many apps I see abuse resources and keep themselves running in the background and thus just sucks. Just make a singleton that manages the stream, which any part of your code that wants it can access. On Mon, Apr 16, 2012 at 12:10 PM, Federico Paolinelli fedep...@gmail.com wrote: Another approach that you could follow is to host the tcp connection inside a service (http://developer.android.com/reference/android/app/Service.html) You can then access the service from any activity you want using the binding pattern or through intents. Just remember that your app may continue to live in background, and your service will still be running unless the os kills it. It's up to you to disconnect if the activities go in background or if you want to notify the user that the connection is still up and running. Remember also that service runs in the ui thread, so you will likely need to run a different thread inside the service. Hope this helps, Federico Il giorno lunedì 16 aprile 2012 18:31:09 UTC+2, Tonez ha scritto: Hi Everyone, I'm building an Android app which uses TCP sockets to communicate with a .net server application. The android app as a whole relies quite heavily on TCP and as such nearly all the features in the app require writing to and listening from a socket stream. I'm trying to determine what the best design approach is for having more than one activity utilize a live active socket. I've recently just finished building an iPhone version of this app, the way in which I got each feature (different view controllers) to use one live active socket connection was by passing the live socket instance to each view controller, each view controller would then retain ownership of that socket and as such the delegate methods which fire when a transmission is received work as expected. Trying to simulate this design in Android is proving to be a pain because I can't pass a live socket instance to another activity as part of an intent parameter. If I wanted to have activity A listen for incoming TCP data, and then navigate to Activity B but then have activity B send TCP data to the .net server and of-course spawn a new thread to listen for incoming TCP data - what would be the best approach to achieve this? At the moment what I have is as follows: activity A spawns a new thread listening for incoming TCP data, activity A can communicate with the .net server perfectly fine. When I navigate to activity B and then want to communicate with the .net server - creating a new socket instance and then listening for incoming data results in activity A's readLine() method receiving the data. Which makes sense, it's still running - but obviously the goal is to have activity B receive this data. An alternative approach I tried was to close down the TCP socket I have in activity A when opening up another TCP socket connection when I need to use TCP in activity B - although this somewhat works it really feels like the wrong way to go about it. And lastly, one other approach I've thought of is to have one activity handling all TCP comms with the .net
Re: [android-developers] Re: Socket sharing between activities
On Tue, Apr 17, 2012 at 12:03 AM, Federico Paolinelli fedep...@gmail.comwrote: Not using a service will make the connection available to be killed if the application goes background. Is that correct? Yes. And, if we want to close the connection when the application is no longer visible, where should the connection be closed? The onStop() gets called when the app goes background but also when the activity gets destroyed because the user pressed the back button to get to a previous activity (and in this case we don't want to close the connection). You just want to keep track of the number of active clients to decide when you should be running. Have two methods: class MyConnection { private mStartCount; void start() { mStartCount++; if (mStartCount == 1) { // Bring up connection } } void stop() { mStartCount--; if (mStartCount == 0) { // Bring down connection } } } And call these in each onStart() and onStop() of the activities that are using the connection. -- Dianne Hackborn Android framework engineer hack...@android.com Note: please don't send private questions to me, as I don't have time to provide private support, and so won't reply to such e-mails. All such questions should be posted on public forums, where I and others can see and answer them. -- You received this message because you are subscribed to the Google Groups Android Developers group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en
Re: [android-developers] Re: Socket sharing between activities
You don't need a service. And please please don't use startService() unless you really want to use up the user's resources with your app trying to run when they are not in it. Way too many apps I see abuse resources and keep themselves running in the background and thus just sucks. Just make a singleton that manages the stream, which any part of your code that wants it can access. On Mon, Apr 16, 2012 at 12:10 PM, Federico Paolinelli fedep...@gmail.comwrote: Another approach that you could follow is to host the tcp connection inside a service ( http://developer.android.com/reference/android/app/Service.html) You can then access the service from any activity you want using the binding pattern or through intents. Just remember that your app may continue to live in background, and your service will still be running unless the os kills it. It's up to you to disconnect if the activities go in background or if you want to notify the user that the connection is still up and running. Remember also that service runs in the ui thread, so you will likely need to run a different thread inside the service. Hope this helps, Federico Il giorno lunedì 16 aprile 2012 18:31:09 UTC+2, Tonez ha scritto: Hi Everyone, I'm building an Android app which uses TCP sockets to communicate with a .net server application. The android app as a whole relies quite heavily on TCP and as such nearly all the features in the app require writing to and listening from a socket stream. I'm trying to determine what the best design approach is for having more than one activity utilize a live active socket. I've recently just finished building an iPhone version of this app, the way in which I got each feature (different view controllers) to use one live active socket connection was by passing the live socket instance to each view controller, each view controller would then retain ownership of that socket and as such the delegate methods which fire when a transmission is received work as expected. Trying to simulate this design in Android is proving to be a pain because I can't pass a live socket instance to another activity as part of an intent parameter. If I wanted to have activity A listen for incoming TCP data, and then navigate to Activity B but then have activity B send TCP data to the .net server and of-course spawn a new thread to listen for incoming TCP data - what would be the best approach to achieve this? At the moment what I have is as follows: activity A spawns a new thread listening for incoming TCP data, activity A can communicate with the .net server perfectly fine. When I navigate to activity B and then want to communicate with the .net server - creating a new socket instance and then listening for incoming data results in activity A's readLine() method receiving the data. Which makes sense, it's still running - but obviously the goal is to have activity B receive this data. An alternative approach I tried was to close down the TCP socket I have in activity A when opening up another TCP socket connection when I need to use TCP in activity B - although this somewhat works it really feels like the wrong way to go about it. And lastly, one other approach I've thought of is to have one activity handling all TCP comms with the .net server and contain all the functionality in this one activity by swapping out .xml layout files when necessary. Obviously this will result in one massive .java file and again is a route which feels wrong. Any advice on how I can go about designing my app given that I want to use TCP functionality in every activity would be greatly appreciated. Many thanks, Tonez -- You received this message because you are subscribed to the Google Groups Android Developers group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en -- Dianne Hackborn Android framework engineer hack...@android.com Note: please don't send private questions to me, as I don't have time to provide private support, and so won't reply to such e-mails. All such questions should be posted on public forums, where I and others can see and answer them. -- You received this message because you are subscribed to the Google Groups Android Developers group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en