I am downloading many files (50+) from DownloadManager by enqueue it in a for loop. On start of the download the UI freezes for a while and produces ANR. I am using volley library to access the files from the server and enqueue them in the DownloadManager.
if you want to get clarity to prevent an ANR error then you could visit this link
When the UI thread of an Android app is blocked for too long, an
"Application Not Responding" (ANR) error is triggered.
When app needs to do long operations with background thread based like file download the you must handle in Service
Once the operation done as you needed in service then you can handle it with the help of BroadcastReceiver to make the UI changes based on the background operations.
Note:
If you have good clarity on the android components(Activity, BroadcastReceiver, and Service) then you'll not face this kind of issues at your case.
Related
Our app needs to download files with the following requirements:
User can dynamically add or cancel downloads
Files are downloaded one at a time
Sometimes in background we need to schedule several file downloads
It would be nice to display a notification displaying download progress and a cancel button
We had all this implented in a foreground service that would maintain a queue of tasks and having an aidl interface with methods that allowed to enqueue new downloads or cancel active/enqueued.
Since Android 12 we can no longer start foreground service when the app is in background, so we can't reliably download files anymore in this situation (requirement #3)
As far as I can understand, the recommended way of implementing such task is using WorkManager, but I can't find a good way of doing it.
I consider two approaches, but both are far from perfect:
Every downloaded file is a separate Work. It's easy to cancel when needed and we only need to suply a file URL and that's it.
But the downsides are: there's no way to enqueue several downloads at once (requirement #3), we need to wait for previous work to finish and then enqueue the next one.
Using ExistingWorkPolicy.APPEND doesn't help here - our downloads are independent and if one is cancelled or fails, others should stay in the queue.
Another annoying issue with this approach is that if we display a notification from our ListenableWorker via startForeground(), then for each file download it will be shown and hidded instead of just updating its contents for every new downloaded file.
Use a long running ListenableWorker that would download many files. But this requires somehow delivering enqueue and cancel(fileUrl) messages to the running worker instance (what we did previously using our service with the aidl/binder stuff). As far as I can see, the WorkManager API doesn't support anything like that. So the only thing we can do is to use some static vars to deliver those messages, which would work (if our worker works in the same process as the main app - hopefully, I can rely on that). But using statics in such a way is always kind of a code smell, I would avoid it if possible.
Are there any other possibilities to do this using WorkManager? Maybe I'm missing some part of the API?
I have a service that does some heavy work on a background thread. It basically downloads a large zip (>5MB), stores it, unzips it, parses the XML and stores the information in a SQLite database.
While the service is doing its job, I sometimes have to run a quick background task (like logging in the user). However, since the service is very busy (usually extracting the zip file), my short task takes a while to run (and the user has to wait).
Is there a way I could completely pause my service (and it's background AsyncTask) so I can run my new higher-priority task?
Thanks!
According to this:
stackoverflow.com/questions/18480206/asynctask-vs-thread-in-android
whay dont you try simple java thread instead of using AsyncTask. Also you can try giving it bigger priority, but in my opinion priorities are not so effective.
it is logical question so I don't have code.
My question is:
How may I run background task that make Network work (waiting for messages from the server) and in the same time updates the UI when Message arrive!!
the Paradox "Android prevent access to UI from another nonUI thread", and "prevent network accesss from UI thread!!!"
Important: I want to run my method all the time that the application run, and scan network buffers and when I get message I want to update the UI and messages List...
That sounds like a perfect description for a Service. You can use IntentService or build your own Service (be careful, the last one doesn't start in a new thread by default).
There are also many examples out there how to update the UI from a Service (i.e. using Notifications or Broadcast).
Android provides built in class to perform network operation. For this purpose you can use AsyncTask class.
AsyncTask enables proper and easy use of the UI thread. This class
allows to perform background operations and publish results on the UI
thread without having to manipulate threads and/or handlers.
You can get details from here Android AsyncTask
But if you want to check message continuously then Service is the best solution as #AlexS explained it perfectly.
I am new in android and working on a maintenance project
Application crashes if no internet connection while creating Soap Request
Following is screenshot of LogCat, Can any body tell me Whats the reason for crashing.
Amit Battan
You are getting a ANR. It stands for Application Not Resoponding.
Android kills applications which are unresponsive to user interactions. Basically when you are doing heavy processing and the user clicks on a button if your application doesn't respond to the click event within 5 seconds.
In your case, if the internet connection is not there I think your application keeps trying to connect and never comes out of this. Hence the ANR. Consider having a timeout for the request..
Check this link for more details on ANR.
ANR happens when some long operation takes place in the "main" thread. This is the event loop thread, and if it is busy, Android cannot process any further GUI events in the application, and thus throws up an ANR dialog.
Any task that will take more time should not be performed in the UI thread, and should be moved to either an AsyncTask or a Thread & Handler.
Check the /data/anr/traces.txt file.
put the all the downloading task in a thread and check that once....
Actually android allocates some time to every process if the process does not complete its action in that particular time then ANR error will come.
I'm working on android application which uses WebServices. In that I'm fetchhing images from web and displaying in imageview. It takes time to fetch images from web and sometimes it forcecloses saying "Activity is not responding" and at that time logcat shows warning as
"Key dispatching timed out sending to
com.Test.TestProject/com.Test.TestProject.Activity1
"
I'm not getting why this is happening.Please help me.
Thanks,
Vishakha.
Read the article on painless threading. You are doing the downloading on the UI thread, which will throw ANRs (Activity Not Responding Exceptions) if it hangs for 30 sec or so. You need to do the downloading in a background task, and update the UI on completion.
You should avoid perform long running tasks on the ui thread. Consider using AsyncTask with ProgressDialog bounded.
The error happens because UI Thread is busy with images downloading, so no key events could be dispatched. The os detecs this and closes your app.