I am writing an android application to make server socket and receive messages from server
when I write the same application using Thread, it works pretty well but I am unable to access UI elements in Thread
whereas in case of services, startService method hangs on while(true){ socket.accept()}
You can read about threads and Android on the official documentation. Also, take a look at AsyncTask, that should help you a great lot.
Related
I am working on an android application, which is sends and gets data from MS SQL server via java servlets.
I need to update the UI of the application when Database has been updated.
I was thinking of implementing a looper class which will call a servlet via HTTP asyncTask, and call the servlet every few minutes. But it will be lots of work for the application and will slow down and also the UI needs to be updated as soon as the database has been updated.
Is there any way I can invoke the android application, from the servlet as soon as the Database has been updated? I cannot wait for the android application to make a call to servlet and check if database has been updated.
Any help would be greatly appreciated.
This is the first time I am writing a question on SO, please pardon my mistakes and suggest me how to improve.
Big thanks,
Ashley
A java servlet is stateless. This means that the connection is not maintained. The java servlet knows nothing of your application and cannot communicate directly with it.
If you must maintain the servlet setup then you can use Google Cloud Messaging to push the update to the user's phone. This is not exactly instantaneous though and there is a delay.
The thing that would work best for you is to change your java servlet into a java application that has a socket. You would then communicate with your java application via the socket and keep it open in a background thread. This thread will hold a callback to your activity which can populate it when a response is sent back over the thread to say data has been received.
I'm getting to a point in the development of an Android app where I've reached a stumbling block: how to create, manage, and connect to a Socket in Android.
My app needs to keep a persistent TCP connection to the server in order to exchange JSON formatted strings back and forth. After reading up on the subject, I've determined the best way forward is to create a Service when the app starts up (by extending the Application class and starting the Service in onCreate()), then read from/write to the Socket as needed. But how do I do that?
I obviously know how to create a Service and how to create and work with a Socket. But I don't know the best way to interact with one in an Android environment. Should I create an AsyncTask whenever I want to write data? Should I use Intents? Any help on the subject would be wonderful. And if my question isn't clear, I'll be more than happy to clarify anything.
Within the service, you can simply listen as you would in a regular Java application. This means you can safely wait for IO as you normally would.
You will have to use Intent when you wish to notify your activities about new data arrival using sendBroadcast and receiving it to your activities by either registering a BroadcastReceiver using registerReceiver or modifying your manifest file.
This is a good tutorial that may help you with broadcasting for Service <-> Activity communication.
Apparently I was thinking about this problem incorrectly. What I need is to implement a "Bound Service", which will give my Activity an interface to interact with in order to send and receive messages.
Here's a link to the tutorial I found which gives an excellent overview of how to do this.
Is it possible that a java program is running and it works on commands from another java program simultaneously running in same machine? For example: if a second java program sends a query database command to first running java program, the first one will execute a query in the database connected to it and reply back to second one.
Please help.
Thanks in advance.
You could use socket programming to do this.
Make a server and make it listen to incoming messages.
http://docs.oracle.com/javase/tutorial/networking/sockets/
You could learn about sockets from the above link.
It is possible.
Socket programming is good but in this case you have to implement synchronization and multi threading request handling.
Another way is using web service for storing data in db.
http://docs.oracle.com/javaee/6/tutorial/doc/gijvh.html
Probably you can use any of
remote invocations (RMI), see http://docs.oracle.com/javase/tutorial/rmi/
or
manged beans (JMX), see http://docs.oracle.com/javase/tutorial/jmx/
As already answered you can use socket proramming but you would have to implement your own protocol.
It should be easier to use RMI which lets you invoke remote methods as if they were local but it is limited to java.
A "bit" heavier (in resources and implementation) solution is using web services but it is a standard which is not limited to java world.
You can also use JMS but I think it should be overkill (you need a server such as activeMQ)
I want to implement an android app which could communicate with a Server via internet. Since everytime the Android app connects to Server, there will be a connection established, and Android app will maintain this connection for the purpose of receiving msgs from Server. However, at the same time the Android app has to deal with User inputs, so these two tasks have to run concurrently that needs to realize by two Threads. I do not have experience about multi-threads programming. any suggestions? Thanks. leon.
The user interface is being handled by the main thread, so really you just have to set up one thread handling the networking (in fact, on 3.0 and on opening a socket connection from the main thread will throw an exception!).
Set up and maintain the connection to the server using an AsyncTask - this is a fairly easy way of doing multi threading. Take a look at this documentation: http://developer.android.com/resources/articles/painless-threading.html
Did you try to put the server interaction into a thread? This should be no problem at all
Thread thread = new Thread(this){
public void run() {
// do your server interaction
}
}
thread.start();
You may want to consider Google's Cloud To Device Messaging (C2DM). They manage to keep a connection open to a device for long periods and will push your server notifications to any android device. I believe its still currently in the 'labs' stage though.
I have an android app that listens for json commands over a socket. I am wondering how I can implement this as a background service. The service would receive the commands and depending on which commands they are, notify the user, or update data within the main program. Some code examples would be great if anyone has them. I've got an example of how to build a background service however it uses timers. I'd like for socket to always be listening.
In this case you would use a Service which responds to events sent over the socket.
The service should be started by the activity (could also be started on boot if needed).
For code example I would recommend commonwares projects on GitHub. There are many great examples using Services there.
(There is also a service example in an ongoing app I have here.)