Android background thread that updates the UI in multiple activities - android

I'm very new to Android, this is my first app. I'm just looking for someone to point me in the right direction.
I am building an app that will query a server over tcp/ip every minute. I would like to show the status of this server (up/down) at the top of every activity/screen.
My questions are:
1) What should I use to continuously ping a server in the background across multiple activities? For instance, is there a way to spawn a thread from one activity in such a way that it will continue even if the user goes to a different screen?
2) How can I allow this thread to update a status label on whatever screen the user happens to be viewing?

Use services. Please get familiar with documentation first.
You can use service binding or local broadcast messaging to notify your current/front activity about changes from your service

Related

Rest service response with activity in background

What's the best Android architecture for calls to a REST service with the following scenario.
1) The user presses the login button.
2) The application starts a background process and displays a progress bar on the screen.
3) The user presses Home button and the application goes to background.
4) Request ends and the answer comes to the device with the screen still in the background.
5) The user returns to the application and the screen returns to foreground.
How can I get the status of the request, so I can display the data on the screen or continue displaying the progress bar, according to the result?
According to the architecture of Virgil Dobjanschi at Google I / O 2010, it suggests to write to the database and access later. But this involves much boilerplate code. Is there a better way?
It really is not a thing of the kind that is ready around, but come on!
If you need to perform some task even when the application is in the background, you need to use a Service. With this in mind u could use a Service to make requests and through broadcast or EventBus communicate the UI thread on the status of the request.
Try to use RoboSpice is a modular android library that makes writing asynchronous network requests easy !
Some main features of RoboSpice
notifies your activities (or any other context) of the result of the
network request with respect to their lifecycles.
notifies your activities (or any other context) on the UI Thread
no memory leaks at all, like Android Loaders, unlike Android
AsyncTasks
RoboSpice I believe that is the implementation more closer than you research
hope this helps!

How to design an app which executes tests on webpage in background, and let the user use his phone normally?

I am currently trying to design a particular application and I don't really know how. The app has to run like this.
What I have to do :
The user launches the application. This action starts something independent of the UI (a Service currently), which has to request a remote server to get test scenario, execute the tests and send results to the same server, even if the UI of the application is not on the screen.
Second constraint : the user chooses, at the beginning, the time between two requests of scenario from the server.
Example : The user starts the app, chooses "do one request of test scenario every 5 min" and after, he can do what ever he wants on his phone. And the app, every 5 min, send a request to get test scenario (not one, but a various number, 50 for example), do the test scenario, and send the results to the server ; and then, wait for the next request.
During this, I think a notification icon in the status bar could be a good thing to access to the UI of the app. With that, when the user feels that the app has done enough tests, he can access to the UI and stop the app.
What I have already done :
I create an Activity which creates a Service. I also set an alarm with AlarmManager, received with an AlarmReceiver which calls the Service every X min/seconds/... and starts in the Service a request to the server to get the scenario (the http request is done in an AsyncTask). The service puts a notification in the status bar in its onCreate() method. I also managed to parse JSON, send HttpRequest and just technical things like that.
What my problems are :
I find that the Service stops itself at the end of the onStartCommand(). It finishes the method and then go to onDestroy() (where I don't put stopSelf()), but is the Service really closed ? If yes, how could I managed to have it "up" during all the duration of the application ?
The notification in the status bar just appeared and disappeared with the rythme given by the AlarmManager. How could I "stabilize" it ?
If you have already designed an app like that (a mail app, I think, is a good example, which request the mail server every X minutes to check new mail, and send a notification), how can I reach my goal ? To reuse the mail app example : is the service which checks the new mail on the server is always running ?
I really don't know how to design this king of app, so any help is welcome.
Sorry for my bad English. If you want precisions, code of the Service, the Activity, just ask.
Regards
Looks like your Service is running on the UI thread. You should create a new thread inside your Service, where all networking operations will work. So, there is no need in AlarmManager - just wait X milliseconds inside your Thread.
P.S. If you don't know how to create new thread:
new Thread(new Runnable(){
public void run(){
//your code here
}
}).start();
but I highly recommend you to read about multithreading first.

How to create a chat screen in Android?

I'm creating an Android chat application. I want chat screen to include features like a new UI screen for chat thread with a different user. But I don't know to move further with it. Should I use service or asyntask or is there any other way. Please help me get started.
No. if you use each thread for each user, you will spawn many threads (memory, and time to create and clear...).
You should use a service to listen when receive message (this is an easy work by using intent), and you can do in your main UI Thread.
The chat gets closed (The network connection with the server/another device which the user was chatting with) once the activity closes, so I don't see a reason to use a serivce/AsyncTask. Just have a thread waiting for data from the socket streams, and show that data to the user once it arrives.

Any way to run a background thread behind multiple Activity and show alert when done?

My application is using a REST based webservice to show multiple activity. To enable the user offline browsing, I am storing the REST data into local SQLiteDatabase with AsyncTask (with some dialogue to wait). However, it takes a long time to load the db (10 mins) and its just bad experience for the user. So I was wondering if there is a way to run the DBLoading thread in background and let the user continue using the app (With multiple activity) with the REST service. When DBLoading thread is done, I want to push a notification in the current Activity in focus letting the user know the Loading is done.
You can run it in a service and let the service inform your activities or your currently focused activity of certain events.
It's actually just a simple observer pattern.

Android Service Multiple Thread Design

A new question about android and services. Currently I'm developing a App that should send images to a server. It should also be possible to send more images parallel.
I made a service that creates for every image a new image. The activity can bind to that service and gather information about the progress. I want to show the current status for every image in a notification (and when the user clicks a notification, an activity with the progress for that image should be shown).
But I get several problems with that approach. There are errors with binding, the notification pending event starts the activity completly new, so I lose information about currently sending images and so on.
Can someone plase tell me, how I could design such a problem in a appropriate way.
thx
I would use a Controller object that is created in your application class.
This controller is the same for all your activities. If an image upload is started the controller creates a new service/thread and monitors it state and shows the notification. Every activity now can ask the controller for the state of its process and show the corresponded information to the user. This way the controller living in the application lifecyle will bind to the service and not the short lived activity
There was a talk from mark brady on droidcon about this you can find the slides on slideshare. It gets interesting in the later part of the slides when the architecture mark used is explained. I fought with the same problem for some weeks and came to the same results and I'm glad that finally some kind of resource exists on the net on this topic.

Categories

Resources