[android-developers] Re: Variable values just get lost!??!?
I don't if it was a typo or not, but shouldn't this code be like this?: public static ServerConnection getInstance() { if(m_connection == null) m_connection = new ServerConnection(); return m_connection; } (added 'm_connection = ' to the second line of the function's body). On Feb 6, 7:08 am, Florian Lettner wrote: > Hey guys, > I seriously need help with a quite strange problem. I created a client > which communicates with a server. Therefore, I have three classes. An > activity providing a nice dialog to configure some data, a background > service which checks the connection status periodically and a server > class which handles the socket connection. Originally, the background > service owned a server connection object but because I could not > manage to provide data from the activity to the service, I decided to > create the server connection class as singleton. > > The user is now able to change data in the activity (IP, Port, > Username, Password) which shall be transmitted to the server object, > if the save button is pressed. The data is read correctly, the > activity calls the setter of the server class and the logcat also says > that this data is correctly applied to the variable that stores the > data in the server class. However, if I try to start the socket > connection still the old values are stored in my variables although > they've been overwritten before. What I am doing wrong, or how can I > fix this? > > My singleton looks like this. > > static ServerConnection m_connection; > > public static ServerConnection getInstance() { > if(m_connection == null) > new ServerConnection(); > > return m_connection; > > } > > All getters/setters and other variables are non-static. > > Best regards, > Florian -- 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
[android-developers] Re: Variable values just get lost!??!?
In addition to the comment/correction by Streets of Boston... It's unclear from your description just where you're storing your non- static variables. The right way would appear to be in your ServerConnection instance, since that's the static singleton. Your activity will have a much shorter lifespan. There's a fair bit more that's unclear to me from your description as well. It may be that you need to create and bind a service (android.app.Service). I'm not sure you're using "ServerConnection", and "server connection" in the sense of a connection to an android.app.Service, or to a service on some other system accessed via your socket connection. I'm not sure why you had trouble passing data from your activity to your service. If it was an android.app.Service, look at aidl. Without an android.app.Service, your entire application could be going away in between times, if it's not in the foreground. Be sure you understand the application and activity lifecycle. Your choice of whether to use an android.app.Service should be based on how the application lifecycle matches up with when you need this connection to exist and what you're doing with it. If you need it to persist solely to avoid authentication, consider getting a time-limited authentication token back instead, and persisting that. This would allow your activity, service, connection, and entire application to go away, and be restarted, and the user would still avoid re-authenticating. You can refresh the token with a new time-limited token on each reconnect or access, so timeout will only happen if the user is idle for an extended period. This can give a more robust user experience. Still, I think there's a good chance the problem is that you wrote: public static ServerConnection getInstance() { if(m_connection == null) return new ServerConnection(); // instead of m_connection = return m_connection; } I hope this helps, somehow. I know it's hard to do when you're lost, but if you can better describe your circumstance, you can get more useful answers. (Sometimes, doing so even leads you to your own answer!) On Feb 6, 4:08 am, Florian Lettner wrote: > Hey guys, > I seriously need help with a quite strange problem. I created a client > which communicates with a server. Therefore, I have three classes. An > activity providing a nice dialog to configure some data, a background > service which checks the connection status periodically and a server > class which handles the socket connection. Originally, the background > service owned a server connection object but because I could not > manage to provide data from the activity to the service, I decided to > create the server connection class as singleton. > > The user is now able to change data in the activity (IP, Port, > Username, Password) which shall be transmitted to the server object, > if the save button is pressed. The data is read correctly, the > activity calls the setter of the server class and the logcat also says > that this data is correctly applied to the variable that stores the > data in the server class. However, if I try to start the socket > connection still the old values are stored in my variables although > they've been overwritten before. What I am doing wrong, or how can I > fix this? > > My singleton looks like this. > > static ServerConnection m_connection; > > public static ServerConnection getInstance() { > if(m_connection == null) > new ServerConnection(); > > return m_connection; > > } > > All getters/setters and other variables are non-static. > > Best regards, > Florian -- 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
[android-developers] Re: Variable values just get lost!??!?
> public static ServerConnection getInstance() { > if(m_connection == null) > m_connection = new ServerConnection(); > > return m_connection; > > } Actually I've done it this way, was just a writing error -- 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
[android-developers] Re: Variable values just get lost!??!?
However, thank you very much for your help. Also tried to implement it without singleton now and I just have the same weired problem :-( On 6 Feb., 18:49, Bob Kerns wrote: > In addition to the comment/correction by Streets of Boston... > > It's unclear from your description just where you're storing your non- > static variables. The right way would appear to be in your > ServerConnection instance, since that's the static singleton. Your > activity will have a much shorter lifespan. > > There's a fair bit more that's unclear to me from your description as > well. It may be that you need to create and bind a service > (android.app.Service). I'm not sure you're using "ServerConnection", > and "server connection" in the sense of a connection to an > android.app.Service, or to a service on some other system accessed via > your socket connection. > > I'm not sure why you had trouble passing data from your activity to > your service. If it was an android.app.Service, look at aidl. > > Without an android.app.Service, your entire application could be going > away in between times, if it's not in the foreground. Be sure you > understand the application and activity lifecycle. Your choice of > whether to use an android.app.Service should be based on how the > application lifecycle matches up with when you need this connection to > exist and what you're doing with it. > > If you need it to persist solely to avoid authentication, consider > getting a time-limited authentication token back instead, and > persisting that. This would allow your activity, service, connection, > and entire application to go away, and be restarted, and the user > would still avoid re-authenticating. You can refresh the token with a > new time-limited token on each reconnect or access, so timeout will > only happen if the user is idle for an extended period. This can give > a more robust user experience. > > Still, I think there's a good chance the problem is that you wrote: > > public static ServerConnection getInstance() { > if(m_connection == null) > return new ServerConnection(); // instead of m_connection = > return m_connection; > > } > > I hope this helps, somehow. I know it's hard to do when you're lost, > but if you can better describe your circumstance, you can get more > useful answers. (Sometimes, doing so even leads you to your own > answer!) > > On Feb 6, 4:08 am, Florian Lettner wrote: > > > Hey guys, > > I seriously need help with a quite strange problem. I created a client > > which communicates with a server. Therefore, I have three classes. An > > activity providing a nice dialog to configure some data, a background > > service which checks the connection status periodically and a server > > class which handles the socket connection. Originally, the background > > service owned a server connection object but because I could not > > manage to provide data from the activity to the service, I decided to > > create the server connection class as singleton. > > > The user is now able to change data in the activity (IP, Port, > > Username, Password) which shall be transmitted to the server object, > > if the save button is pressed. The data is read correctly, the > > activity calls the setter of the server class and the logcat also says > > that this data is correctly applied to the variable that stores the > > data in the server class. However, if I try to start the socket > > connection still the old values are stored in my variables although > > they've been overwritten before. What I am doing wrong, or how can I > > fix this? > > > My singleton looks like this. > > > static ServerConnection m_connection; > > > public static ServerConnection getInstance() { > > if(m_connection == null) > > new ServerConnection(); > > > return m_connection; > > > } > > > All getters/setters and other variables are non-static. > > > Best regards, > > Florian -- 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
[android-developers] Re: Variable values just get lost!??!?
> It's unclear from your description just where you're storing your non- > static variables. The right way would appear to be in your > ServerConnection instance, since that's the static singleton. Your > activity will have a much shorter lifespan. The variables are stored as class members in the server connection class, which is the static singleton. The singleon works fine, I checked that. The private ctor is only called once, if the getInstance() function is called for the first time. If a call setProperty(x); the x value is transported properly to the server connection class and applied to the member variable. Checked that too, after calling the method the member variable has the desired value. If afterwards connect() is called which uses the member variable, the member has got its initial value again, like it is a different object. However, only one server connection object exists for the application (ctor is definitely called only once): > There's a fair bit more that's unclear to me from your description as > well. It may be that you need to create and bind a service > (android.app.Service). I'm not sure you're using "ServerConnection", > and "server connection" in the sense of a connection to an > android.app.Service, or to a service on some other system accessed via > your socket connection. The service is needed to keep the application up and running since there is additional hardware used for device input (Anoto Pen). It is quite complex, but however the service is needed to react on any action of the pen also if the application is not in foreground. The server connection is needed to send input data to the server (using sockets), however, the server connection is not the service. In a previous version (before the server connection became a singleton) the background service owned a server connection object. Both work well, the server connection connects to the external server, sends data and receives data and the background service runs to keep track of the anoto pen. Additionally the service receives status messages from the server class (e.g. socket error, login error, data lost, ...). This all works well. The only thing that does not work is that if I set the port or IP from the background service or my activity, the data gets lost although the debugger tells me that it is in the variable after calling the setter. > I'm not sure why you had trouble passing data from your activity to > your service. If it was an android.app.Service, look at aidl. > > Without an android.app.Service, your entire application could be going > away in between times, if it's not in the foreground. Be sure you > understand the application and activity lifecycle. Your choice of > whether to use an android.app.Service should be based on how the > application lifecycle matches up with when you need this connection to > exist and what you're doing with it. Life cycle is clear and correctly implemented (works on other platforms like Qt, Symbian and J2ME). Service is needed because of background communication with external hardware that is connected via bluetooth. Has actually nothing to do with the server connection. Its only purpose regarding the server connection is to re-login if the connection is lost. > If you need it to persist solely to avoid authentication, consider > getting a time-limited authentication token back instead, and > persisting that. This would allow your activity, service, connection, > and entire application to go away, and be restarted, and the user > would still avoid re-authenticating. You can refresh the token with a > new time-limited token on each reconnect or access, so timeout will > only happen if the user is idle for an extended period. This can give > a more robust user experience. I would prefer that solution, however the server is developed and provided by Vodafone since this is a bigger university related project. So I have to work with their protocol although I would love to change some things. > I hope this helps, somehow. I know it's hard to do when you're lost, > but if you can better describe your circumstance, you can get more > useful answers. (Sometimes, doing so even leads you to your own > answer!) Thank you very much for the detailed explanations, however I am still trapped. Never had such a problem before. -- 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
[android-developers] Re: Variable values just get lost!??!?
Did you check if you service is being destroyed (onDestroy) or being killed (entire service process is being killed by Android) and re- created later at points in your code that are unexpected? Put break-points in your service's onCreate, onDestroy and onBind and see what's going on. Another question: Do you run your service in a different process or in the same process as your Activities? On Feb 8, 5:58 am, Florian Lettner wrote: > > It's unclear from your description just where you're storing your non- > > static variables. The right way would appear to be in your > > ServerConnection instance, since that's the static singleton. Your > > activity will have a much shorter lifespan. > > The variables are stored as class members in the server connection > class, which is the static singleton. The singleon works fine, I > checked that. The private ctor is only called once, if the > getInstance() function is called for the first time. If a call > setProperty(x); the x value is transported properly to the server > connection class and applied to the member variable. Checked that too, > after calling the method the member variable has the desired value. If > afterwards connect() is called which uses the member variable, the > member has got its initial value again, like it is a different object. > However, only one server connection object exists for the application > (ctor is definitely called only once): > > > There's a fair bit more that's unclear to me from your description as > > well. It may be that you need to create and bind a service > > (android.app.Service). I'm not sure you're using "ServerConnection", > > and "server connection" in the sense of a connection to an > > android.app.Service, or to a service on some other system accessed via > > your socket connection. > > The service is needed to keep the application up and running since > there is additional hardware used for device input (Anoto Pen). It is > quite complex, but however the service is needed to react on any > action of the pen also if the application is not in foreground. The > server connection is needed to send input data to the server (using > sockets), however, the server connection is not the service. In a > previous version (before the server connection became a singleton) the > background service owned a server connection object. Both work well, > the server connection connects to the external server, sends data and > receives data and the background service runs to keep track of the > anoto pen. Additionally the service receives status messages from the > server class (e.g. socket error, login error, data lost, ...). This > all works well. The only thing that does not work is that if I set the > port or IP from the background service or my activity, the data gets > lost although the debugger tells me that it is in the variable after > calling the setter. > > > I'm not sure why you had trouble passing data from your activity to > > your service. If it was an android.app.Service, look at aidl. > > > Without an android.app.Service, your entire application could be going > > away in between times, if it's not in the foreground. Be sure you > > understand the application and activity lifecycle. Your choice of > > whether to use an android.app.Service should be based on how the > > application lifecycle matches up with when you need this connection to > > exist and what you're doing with it. > > Life cycle is clear and correctly implemented (works on other > platforms like Qt, Symbian and J2ME). Service is needed because of > background communication with external hardware that is connected via > bluetooth. Has actually nothing to do with the server connection. Its > only purpose regarding the server connection is to re-login if the > connection is lost. > > > If you need it to persist solely to avoid authentication, consider > > getting a time-limited authentication token back instead, and > > persisting that. This would allow your activity, service, connection, > > and entire application to go away, and be restarted, and the user > > would still avoid re-authenticating. You can refresh the token with a > > new time-limited token on each reconnect or access, so timeout will > > only happen if the user is idle for an extended period. This can give > > a more robust user experience. > > I would prefer that solution, however the server is developed and > provided by Vodafone since this is a bigger university related > project. So I have to work with their protocol although I would love > to change some things. > > > I hope this helps, somehow. I know it's hard to do when you're lost, > > but if you can better describe your circumstance, you can get more > > useful answers. (Sometimes, doing so even leads you to your own > > answer!) > > Thank you very much for the detailed explanations, however I am still > trapped. Never had such a problem before. -- You received this message because you are subscribed to the Google Groups "Andr
[android-developers] Re: Variable values just get lost!??!?
But in that case the ctor of the singleton class must have been called a second time, right? Since I've only got one single call of the ctor I guess the instance only exists one time!? I thought of that too. So what I do is, I call the startService() method from my acivity to create a new service. I'm using logcats every where in the application to produce output whenever I can. On 8 Feb., 17:35, Michael MacDonald wrote: > On 02/08/10 10:30, Streets Of Boston wrote: > > > Did you check if you service is being destroyed (onDestroy) or being > > killed (entire service process is being killed by Android) and re- > > created later at points in your code that are unexpected? > > > Put break-points in your service's onCreate, onDestroy and onBind and > > see what's going on. > > > Another question: Do you run your service in a different process or in > > the same process as your Activities? > > > On Feb 8, 5:58 am, Florian Lettner wrote: > > >>> It's unclear from your description just where you're storing your non- > >>> static variables. The right way would appear to be in your > >>> ServerConnection instance, since that's the static singleton. Your > >>> activity will have a much shorter lifespan. > > >> The variables are stored as class members in the server connection > >> class, which is the static singleton. The singleon works fine, I > >> checked that. The private ctor is only called once, if the > >> getInstance() function is called for the first time. If a call > >> setProperty(x); the x value is transported properly to the server > >> connection class and applied to the member variable. Checked that too, > >> after calling the method the member variable has the desired value. If > >> afterwards connect() is called which uses the member variable, the > >> member has got its initial value again, like it is a different object. > >> However, only one server connection object exists for the application > >> (ctor is definitely called only once): > > >>> There's a fair bit more that's unclear to me from your description as > >>> well. It may be that you need to create and bind a service > >>> (android.app.Service). I'm not sure you're using "ServerConnection", > >>> and "server connection" in the sense of a connection to an > >>> android.app.Service, or to a service on some other system accessed via > >>> your socket connection. > > >> The service is needed to keep the application up and running since > >> there is additional hardware used for device input (Anoto Pen). It is > >> quite complex, but however the service is needed to react on any > >> action of the pen also if the application is not in foreground. The > >> server connection is needed to send input data to the server (using > >> sockets), however, the server connection is not the service. In a > >> previous version (before the server connection became a singleton) the > >> background service owned a server connection object. Both work well, > >> the server connection connects to the external server, sends data and > >> receives data and the background service runs to keep track of the > >> anoto pen. Additionally the service receives status messages from the > >> server class (e.g. socket error, login error, data lost, ...). This > >> all works well. The only thing that does not work is that if I set the > >> port or IP from the background service or my activity, the data gets > >> lost although the debugger tells me that it is in the variable after > >> calling the setter. > > >>> I'm not sure why you had trouble passing data from your activity to > >>> your service. If it was an android.app.Service, look at aidl. > > >>> Without an android.app.Service, your entire application could be going > >>> away in between times, if it's not in the foreground. Be sure you > >>> understand the application and activity lifecycle. Your choice of > >>> whether to use an android.app.Service should be based on how the > >>> application lifecycle matches up with when you need this connection to > >>> exist and what you're doing with it. > > >> Life cycle is clear and correctly implemented (works on other > >> platforms like Qt, Symbian and J2ME). Service is needed because of > >> background communication with external hardware that is connected via > >> bluetooth. Has actually nothing to do with the server connection. Its > >> only purpose regarding the server connection is to re-login if the > >> connection is lost. > > >>> If you need it to persist solely to avoid authentication, consider > >>> getting a time-limited authentication token back instead, and > >>> persisting that. This would allow your activity, service, connection, > >>> and entire application to go away, and be restarted, and the user > >>> would still avoid re-authenticating. You can refresh the token with a > >>> new time-limited token on each reconnect or access, so timeout will > >>> only happen if the user is idle for an extended period. This can give > >>> a more robust user experien
[android-developers] Re: Variable values just get lost!??!?
When I unexpectedly saw important, small, static strings get cleared, my solution was to surround them with getters and setters, and check if they were null each time. In the setter I would write them to private internal storage, and in the getter, if I found a null I'd check if they were stored locally. -- 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
[android-developers] Re: Variable values just get lost!??!?
Some thoughts, Is your service delcared "remote" in the manifest? If so you do know how to debug into both processes simultaneously? The order of initialization of class variables can get complicated, try moving everything into explicit new's. mike -- 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
[android-developers] Re: Variable values just get lost!??!?
Mhm, I guess this could be the problem. I will check it. Otherwise I will use a shared property which will finally solve this issue. On 8 Feb., 21:24, Mike Collins wrote: > Some thoughts, > > Is your service delcared "remote" in the manifest? If so you do know > how to > debug into both processes simultaneously? > > The order of initialization of class variables can get complicated, > try moving > everything into explicit new's. > > mike -- 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: Variable values just get lost!??!?
On 2/8/2010 11:58 AM, Florian Lettner wrote: afterwards connect() is called which uses the member variable, the member has got its initial value again, like it is a different object. Is it the inital value from a variable initialization? (e.g. int variable = 0; ) If so, what I ran into is that the variable initialization is applied *after* constructor methods or methods in the constructor are run (I'm still somewhat puzzled by that behavior, but anyways). So simply removing variable initialization fixed my problem. Which might be different than yours, but worth a try :-) Michael -- 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: Variable values just get lost!??!?
On 02/08/10 10:30, Streets Of Boston wrote: > Did you check if you service is being destroyed (onDestroy) or being > killed (entire service process is being killed by Android) and re- > created later at points in your code that are unexpected? > > Put break-points in your service's onCreate, onDestroy and onBind and > see what's going on. > > Another question: Do you run your service in a different process or in > the same process as your Activities? > > On Feb 8, 5:58 am, Florian Lettner wrote: > >>> It's unclear from your description just where you're storing your non- >>> static variables. The right way would appear to be in your >>> ServerConnection instance, since that's the static singleton. Your >>> activity will have a much shorter lifespan. >>> >> The variables are stored as class members in the server connection >> class, which is the static singleton. The singleon works fine, I >> checked that. The private ctor is only called once, if the >> getInstance() function is called for the first time. If a call >> setProperty(x); the x value is transported properly to the server >> connection class and applied to the member variable. Checked that too, >> after calling the method the member variable has the desired value. If >> afterwards connect() is called which uses the member variable, the >> member has got its initial value again, like it is a different object. >> However, only one server connection object exists for the application >> (ctor is definitely called only once): >> >> >>> There's a fair bit more that's unclear to me from your description as >>> well. It may be that you need to create and bind a service >>> (android.app.Service). I'm not sure you're using "ServerConnection", >>> and "server connection" in the sense of a connection to an >>> android.app.Service, or to a service on some other system accessed via >>> your socket connection. >>> >> The service is needed to keep the application up and running since >> there is additional hardware used for device input (Anoto Pen). It is >> quite complex, but however the service is needed to react on any >> action of the pen also if the application is not in foreground. The >> server connection is needed to send input data to the server (using >> sockets), however, the server connection is not the service. In a >> previous version (before the server connection became a singleton) the >> background service owned a server connection object. Both work well, >> the server connection connects to the external server, sends data and >> receives data and the background service runs to keep track of the >> anoto pen. Additionally the service receives status messages from the >> server class (e.g. socket error, login error, data lost, ...). This >> all works well. The only thing that does not work is that if I set the >> port or IP from the background service or my activity, the data gets >> lost although the debugger tells me that it is in the variable after >> calling the setter. >> >> >>> I'm not sure why you had trouble passing data from your activity to >>> your service. If it was an android.app.Service, look at aidl. >>> >> >>> Without an android.app.Service, your entire application could be going >>> away in between times, if it's not in the foreground. Be sure you >>> understand the application and activity lifecycle. Your choice of >>> whether to use an android.app.Service should be based on how the >>> application lifecycle matches up with when you need this connection to >>> exist and what you're doing with it. >>> >> Life cycle is clear and correctly implemented (works on other >> platforms like Qt, Symbian and J2ME). Service is needed because of >> background communication with external hardware that is connected via >> bluetooth. Has actually nothing to do with the server connection. Its >> only purpose regarding the server connection is to re-login if the >> connection is lost. >> >> >>> If you need it to persist solely to avoid authentication, consider >>> getting a time-limited authentication token back instead, and >>> persisting that. This would allow your activity, service, connection, >>> and entire application to go away, and be restarted, and the user >>> would still avoid re-authenticating. You can refresh the token with a >>> new time-limited token on each reconnect or access, so timeout will >>> only happen if the user is idle for an extended period. This can give >>> a more robust user experience. >>> >> I would prefer that solution, however the server is developed and >> provided by Vodafone since this is a bigger university related >> project. So I have to work with their protocol although I would love >> to change some things. >> >> >>> I hope this helps, somehow. I know it's hard to do when you're lost, >>> but if you can better describe your circumstance, you can get more >>> useful answers. (Sometimes, doing so even leads you to your own >>> answer!) >>>