i'm trying to build an application that every given time, will connect to a remote server and get JSON object from it.
as i searched the web for answer i wasn't able to realize exactly how to setup a service and run it as long as the application is running.
i want my main.xml screen have some kind of TextView which will update from the service.
couldn't found anywhere how to build a service which update a TextView when needed..
i'm looking for a simple example - as for i'm newbie to android development.
What you should look for -- perhaps instead of a service in this instance -- is an AsyncTask. This is what you use when you need to update the UI from the background and not hang around too long on the main thread. Here's one AsyncTask tutorial, and here's what the Android SDK docs have to say about it.
If you need to do things like download JSON every so often from a server, a Service might be a good solution. To communicate back and forth between a Service and an Activity you will use a Messenger and Handler example. You can find an example of how to use the messenger / handler pattern for services and activities in the API demos included with the SDK (here). this SO thread is also relevant.
If you need to keep your service running every so often, you'll want to look at using an AlarmManager to grab the data, store it somewhere, and then refresh the display in the Activity (perhaps through a database in your app). But basically, if you need to quickly download some stuff and update an Activity, use an AsyncTask, if you need something longer term, bind a service and then communicate back and forth between it and the Activity using a Messenger / Handler pair (or AIDL, but that's more complicated..)
You might be able to use a bound service to achieve it. http://developer.android.com/guide/topics/fundamentals/bound-services.html
Related
I've decided to use a service for the Android Studio app I'm working on; however, I can't seem to find a way to reference the service in my project. The service will enable the mic to continuously record and using a thread, notify the user if the sound is over a certain loudness and then update the phone's location accordingly. So within the service, if the mic listens into something above a certain loudness threshold it will pass the information to the Google Maps activity. The service is a started service that starts after a button in another activity is pressed.
Is there a way to reference the service, possibly a variable within the service, from my Google Maps activity java code? Do I have to use a bounded service instead?
You describing a service that should have connection to ui, notify it about it state and possibly allow changing it. You should you use bounded service for that, they have exactly what you need: https://developer.android.com/guide/components/bound-services
I was reading about background service limitation in Android 8 and from what I read it seems that you can't run your service in the background for a long time. This seems reasonable but because I use background service to keep connection to server - currently pooling new stuff, sending location and responses I am a bit confused. The responses are OK, I can respond only when interacting with the app, but the pooling new stuff is problematic because it needs to get an stuff from server and if something new come present the user with a notification to respond to it.
If I understand it correctly I can use JobScheduler to schedule some job every several seconds. I can basically schedule the pooling. For the background locations, well there are those restrictions so only foreground service is an option to get updates in requested time.
I will be migrating to websockets and then the pooling is off, the connection to server will be persistent and the app will get updates from server, I was planing to do this in the background service so something would receive stuff from server everytime. However it seems I can't since Android 8. How would you solve this? Should I use foreground service for location and server connection? Or is there a better way to do background networking in an android app on android 8?
Thanks
Here are a few options for performing background work on Android O:
Use JobScheduler. You already seem to have a good grasp on this one- the downside is that it is periodic, not persistent.
Use GCM/FCM or a similar push service to push data to your app when it is relevant instead of constantly holding a connection to your server.
Use a foreground service. This will allow you to continue performing your background work without your app being in the foreground, but will put a notification in the status bar to inform your user that you are doing that work.
Before you select one of these methods, you should take a moment to step back and look at the data that you need from your server and determine why you need a persistent connection and whether the first or second options might be sufficient.
If you absolutely need a persistent connection to your server, the last option is your best option. The idea behind the changes in O is to still allow background work such as what you are describing, but to make it painfully obvious to the user that your app is doing so. That way if they don't think your data is as important as you do, they can take action.
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.
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.)