What is the best way to send a big file from an Android App to a distant server ?
Since there is one thread for the UI, I would like to keep 1 thread for the network connection (SFTP protocol) without reconnecting at every file I send.
* AsyncTask is bad because the operation will last way too long and the UI will freeze.
* IntentService is not the best since it has to reconnect for every file (when the Thread ends, it auto disconnects and I don't see how to keep the connection open).
Something nice would be : 1 thread for the UI, 1 thread to keep the connection open, 1 other thread for downloading/uploading to the server.
Maybe with a standard Runnable class ?
Thanks in advance !
Services are the right tool, especially as a service can run in the background to perform work even while the user is in a different application.
Related
I want to know which is better to download files, async task or service?
My app has eight buttons, which one starts one direfferent download (which download has ~10MB). When the user clicks on one button or more to download data is better to use async task or service?
Thanks!
In any case you should use AsyncTask because even service runs in the main (GUI) thread where no networking should be done. Whether to run the AsyncTask in a service or an activity depends on whether you want that download to continue in background.
all above answers have good points. but the life-cycle issue is the most important thing you should consider. for instance, let's say you use asyncTask. so the user starts downloading and suddenly he/she rotates the screen and because you tied the asyncTask life-cycle to activity another asyncTask operation will be kicked off and result in a compulsive 10mb download. so considering this you should use service and asyncTask together to maintain life-cycle issue and UI thread networking issue.
update: Intent-service is a better solution because it receives requests in its own thread and goes offline when it doesn't have anything to do
AsyncTask -- AsyncTask manipulate threads and/or handlers, if you can do that better with Looper and stuff why bother? AsyncTask is designed to be a helper class around Thread and Handler, and it should ideally be used for short operations (a few seconds at the most.).. how can you tell in production mode whether is not gonna take long? probably bad network, slow network,jammed network, phone restarting - and all these will probably make your downloading either corrupt or unfinished.. i am a user of apps, and i get pissed when i waste bundle on nothing..
if you ask me, use
Service
--Serviceis made to run irrespective of what app/screen is visible and make if communicate with the UI if only it is available if not continue with download and save it, AsyncTask does not constitute a generic threading framework. always use threads, its cool, we all love it.
Good morning everyone,
I am sending data to a device from android each 40ms. Up until now, I have been using a while(true) thread and thread.sleep because I didn't know better :). Now I see I have a lot of "better" options like:
TimerTask
Asynctask
ScheduledThreadPoolExecutor
Which is the best one for my scenario? Keep in mind that there may be an exception thrown if the device disconnects so I will need to stop sending values until the connection is restored. Furthermore, the data must be sent at pretty precise intervals and it should, in no case, be sent less than 40ms before the previous one.
Thanks!
Plenty of options, however, just prior to that AsyncTask does not really belong to that list. Asynctask is simply used to perform an operation in a background thread outside of the main UI thread and not really used for scheduling repeating tasks.
For repeating tasks, the options are:
Android: execute code in regular intervals
Use a countdowntimer as the countdowntimer executes in the main thread (if that is what you want)
Or use a TimerTask.
My suggestion for your case is option 1 or 3.
-V
I am in the middle of developing an android application and I have stumbled across something that I don't really know the best way to solve.
What I wwant to achieve is, when a user logs into the application, I want to start a thread if the device is connected to a network(what kind of network doesn't matter)
The thread should perform an action every 10 minutes.
What this thread needs to do is, loop trough a list, a queue to be more exact.
This queue will have objects, and based on the objects in the queue when there is a connection available, execute.
The queue will be filled trough the flow of the application.
For example filling in a questionary.
The answers need to be synched to the server. Every question can include pictures takebn from the camera etc, so I want to save certain data as an object, put them in a queue, and have a thread handle the http requests. This way the UI won't be blocked. It's of great importance to sync whenever possible.
What I want to avoid is having another process run aside from my own APP. That's why I haven't used a service. Or do I missunderstand the concept of services as a whole?
Are there specific queue objects or lists?
I want to loop trough the queue list that can be filled at anytime while the program is alive, with a thread.sleep like method when the list is completely empty.
Please leave me hints and tips on what way to go with this.
A service isn't it's own process... from the Documentation: "A Service is not a separate process. The Service object itself does not imply it is running in its own process; unless otherwise specified, it runs in the same process as the application it is part of."
A service really is the best choice for what you're talking about. You spawn your own thread in the service that then does the following: check your queue for objects and send any to the server (since you're already not on the UI thread, you can do this without spawning yet another thread if you want). If the queue is empty, use a Timer to schedule another invocation of your upload method.
I have developed an app to communicate with my own server and published it. However, sometimes the app force closes. I know there is no bug in the code because the app works properly most of the time, but sometimes it is waiting for an answer from the server forever. I think this is due to the fact that so many people are using the app, and the app refreshes every 1 second or so, so this heavy traffic causes the server to take a large amount of time to respond. So how do you take care of such a use case? should I have a use case where if the server does not respond after some time you just stop the app and throw a message saying that the server is not responding or something like that?
Right now, your main application is timing out due to server load. If you put your connection details in a thread, you will be able to avoid having that main thread time out. You can check for updated data from the connection thread (through some shared object) and then present a message to the user if the data has stopped.
It sounds like you have your server communication code within your main Activity. Any code running in this Activity will be run in the main UI thread. If your code sends a request to your server, and is then waiting for a response, the main UI thread is blocked until your server responds. The Android OS recognises that the UI thread has effectively hung, and kills your app.
What you need to do is to separate out the UI code in your Activity from the server communication code. You should move this into an AsyncTask or a new Thread/Handler combination. This will allow the UI to remain responsive even when your server is under load.
Documentation for AsyncTask
Designing for Responsiveness
Android Thread Messaging
Thread example
All,
I'm developing an Android application that connects to other hardware on start up via TCP (over WiFi) . I'm pretty happy with the software that handles the connection -- it does a good job of establishing the socket connection as well as handling things when the connection is unexpectedly lost.
Unfortunately, my application currently just displays a blank, empty screen until the connection is established, and I expect that this sort of thing may produce unwarranted worry on the part of my users.
I can't figure out how to put up a start-up message informing the user that I have a towel and that there's no need to panic. Can anybody point me to a method for accomplishing this? I'll be happy with just about anything that's legible, whether graphical or textual.
Thanks,
R.
Whatever you choose, you need to get the startup screen displayed and more importantly start responding to UI events before the TCP connection is made - ie, you shouldn't do the TCP connection attempt on the UI thread, as if it takes longer than expected you may get an application not responding error.
Do the networking in AsyncTask (another thread, so it won't block the UI). Then you can display all kinds of progress indicators in the UI.
You could use a ProgressDialog. http://developer.android.com/reference/android/app/ProgressDialog.html