Android app has warning message of not responding - android

Either you can Force close or Wait.
If I press the Wait then everything will be fine. But how do I disable this annoying message, I don't think something wrong but probably takes some more times to init and runs the App, the wait time bit long.

It sounds like what's happening is that you are running a really long computation (for example, fetching something from the internet) on the UI thread. You can get rid of that message by running that computation in a separate thread.
For more information on how to do this, you can refer to this as a starting point: http://developer.android.com/guide/topics/fundamentals/processes-and-threads.html

Related

Can you indicate to Android the time to wait or tell it that the app is still "active" before displaying "App isn't responding? If so, how?

I'd like to know the code or configuration needed to set that.
In my app, there are some places where I'm willingly make the app to sleep for several seconds, as it's needed for some reasons, with a Thread.sleep(long millis) function.
Problem is that on some Android APIS, at least on 25 and 26, usually that system message pops up in few seconds, confusing the user and maybe even causing the application not to fulfill the needed operations that need to happen while that sleep is happening if the user ends the app, which might cause even malfunctioning of the application.
I'd like to find a way of either forcing Android to wait for a good time like, for example, 1 minute, or to make Android aware that it's not that app isn't responding, that is willingly on a Thread.sleep function.
Is there any way to do that?
I'd like to find a way of either forcing Android to wait for a good time like, for example, 1 minute, or to make Android aware that it's not that app isn't responding, that is willingly on a Thread.sleep function.
TL;DR there is none.
Android apps should at all times be able to yield their position in the foreground to other apps. It's up to the users if they want to wait while some lengthy download is taking place or if they prefer to do something else and come back later.
You can't execute Thread.sleep() on the UI thread for long because this would "freeze the UI".
An example: Users should be able to leave your app by pressing the BACK Button at any time they wish to. If your method is blocking the UI thread, Activity#onBackPressed() can't be executed so the users can't quit.
What can you do? Move the heavy work to another thread (using e.g. AsyncTask or IntentService or some plain worker thread) and show some type of progress indicator to the users if necessary. You can/ should also toggle visibility or enabled state of Buttons etc. if required to avoid clicks which can't be processed at that point in time.
I think you have an implementation problem. The system message, known as ANR (Application Not Responding) occurs when the application cannot respond to user inputs, this may be caused by Ui thread blocking and that may be your case.
To avoid blocking the UI Thread just run your long time operations asynchronously. There are many ways to do that. You could use AsyncTask, AsyncTaskLoader, Thread, RxJava... Here you have some links to help you with that:
https://developer.android.com/training/articles/perf-anr
https://google-developer-training.gitbooks.io/android-developer-fundamentals-course-concepts/content/en/Unit%203/71c_asynctask_and_asynctaskloader_md.html
http://www.vogella.com/tutorials/RxJava/article.html

AsyncTasks will not fire until I leave an Activity

I don't get this, and it's really frustrating me.
All of my long-running operations happen in AsyncTasks. For example, the user interacts with the app somehow and an item is added to a list. This causes an AsyncTask to fire, the data to be written to an sqlite3 database, and then the list is refreshed.
I've recently bumped my Android support up from 2.3.3 to 4.0. I've created a new 4.0 emulator and, for some reason, all of my AsyncTasks seem to queue up and only fire once the user leaves the screen!
Why is this happening????
The code is pretty simple. I'm not doing anything interesting here. For example, here's an AsyncTask fire example:
Log log = data.mFoodLog;
log.setUserId(getUserId());
log.setDay(mBeginningOfToday.getTimeInMillis()/1000);
log.setStatus(DbDefinitions.STATE_POSTING);
(new SaveLogTask()).execute(new SaveObjectTaskParams(getApplicationContext(), log));
The debugger shows this code being executed. I have another breakpoint inside the doInBackground() method, but that breakpoint never gets hit until I leave the screen. I've also put Log.e messages around this code and ran it without the debugger to make sure the debugger isn't doing anything weird. doInBackground() never gets fired until I leave the Activity.
At first sight seems to be what #Cliffroot says, an async task not ending and queuing the ret of them (due to the behaviour changes in Android).
To execute them on diferent threads use: asyncTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, params);
Basically the behaviour of AsyncTask has changed since version 3.0. On 2.3.3 it used to execute AsyncTasks concurrently, but now it does it sequentially.
So your problem might be that you have one AsyncTask that does not finish, and because of that fact the others don't even start (it's hard to say without seeing more of your code).

android app touch unrensponsiveness

I have developed an application that is used very intensively for hours, makes a lot of web services calls, uses a lot of async tasks and does a lot of operations on an sqlite database. The problem is that absolutely randomly the display dims, as if it goes in power saving mode (this is happened also when the battery was charged) and the UI does not respond at all (the log written for buttons click are not written). If I click the home button the phone works correctly and every app works fine. If I go back to my app the display dims again. I really don't have any idea about the cause of this behavior, I really hope some of you can help me because my boss wants an explanation because the customer wants an answer.
EDIT: I've noticed that when the problem occurs, it's just after unlocking the screen, so it should have something to do with app resuming, but I don't really have any idea of what is causing this behavior.
It seems that some operations may be blocking the main UI thread and the app goes to not responding state. Check if any such intensive operations are done in UI thread.
As jaibatrik says, this might be caused by doing too much work in the UI thread rather than in background threads, AsyncTasks etc. One way this may be achieved which is less obvious is if all the work is correctly done in a background thread (of some type) but a UI thread operation is waiting for the outcome of a background thread operation.
you could prevent display dim like this.
ll.setKeepScreenOn(true);
you should handle onresume(), onpause() & co.
maybe you create memoryleaks within your backgroundtasks or services.

How to time relinquish an Android thread?

I'm in front of a very pretty annoying problem.
I have a code to execute that can take tens of seconds. In final I need to obtain the result of that computation.
If I execute the code merely on the main thread, Android will pop up telling that the thread is blocked and asking if we want to force block.
Well the principle is normal, every OS kernel needs to know our code is still alive and not blocked.
So my question is how to inform Android we are not dead?
For instance the equivalent of a Sleep(0) or ProcessMessage() or anything... but that informs Android that we are not dead, because we are just waiting or performing something pretty long...
Please don't answer me: "let make your computation in a separate thread" since the problem would be exactly the same. The main thread would still need to sit down to know when the thread completes and its result.
You should not run any process that access a database, the internet, or takes longer then .2 seconds on the UI thread.
Asynctask is a very powerful method that allows you to thread computations, while still being able to update the UI at predetermined points. Learn to love it.
As far as letting the user know, make a please wait spinner dialog appear on the pre-execute block, and make it go away on the post execute block.
Edit: To dig into this a bit: The asynctask has three blocks that run on the UI thread onPreExecute, onPostExecute, and onProgressUpdate). In these blocks you can update the UI. Within the doInBackground block, it is its own thread, and so will not block the UI as it processes.
In practice you can set things up to notify that a process is happening in onPreExecute, notify the user of progress during a onProgressUpdate, and then present the final information/clear any please wait dialogs during the onPostExecute block. It was specifically designed to tackle the exact problem you are discussing.
Any process that locks up the system for more then 4 seconds by running on the UI thread will cause a not-responding error to be presented to the user.
http://developer.android.com/reference/android/os/AsyncTask.html
You should compute in another thread and then call back to the UI thread using http://developer.android.com/reference/android/app/Activity.html#runOnUiThread(java.lang.Runnable)
see http://developer.android.com/guide/components/processes-and-threads.html for more details.
I'll probably catch flack for this but, really, only use AsyncTask where appropriate! (read: quit it!)
Virgil Dobjanschi's answer here:
http://www.youtube.com/watch?v=xHXn3Kg2IQE
.. is really, really good. It is a little more complicated, but it is frequently no more complicated than the actual problem.
While there aren't a lot of details in the original question, it is likely that the best way to solve the problem is (as all answers agree) to use a separate thread. The best way to get to that other thread, though, is likely to be an intent fired at an IntentService. ... and then runOnUiThread, or a Handler, to get the response back.

AsyncTask randomly stops

I have a pull based app that has to pull from the Server every 15 seconds. For that I used a normal AsyncTask, my code basically looks like this:
protected Void doInBackground(Context... arg0) {
while (true && this.pull_on ) {
pull_data();
TimeUnit.SECONDS.sleep(15)
};
Now I have the massive Problem that, the app just stops pulling out of the blue. No exeptions, the Status of the AsyncTask is 'running' the 'pull_on' variable is set to 'true' but it just stops pulling.
I can not reproduce this, it just happens sometimes, I can work perfectly for a long time and then it just stops.
Does anybody have an idea how this can happen?
Should I refactor and implement this with a timer?
Also what benefit would using a Service have over what I do know? Battery-power is not an issue.
Thanks for your help.
P.S. I should add that sometimes I start Activitys form the AsyncTask. Like this 'ApplicationManager.getCurrentActivity().startActivity( myIntent )' but I does not seam to be a Problem.
P.P.S.
Timer seams to be to limited for what I want to use, I do want to do something every 15 seconds but in some special case I want to stop the the pinging and then restart it again. The amount of hacking for that seams to be more work then just doing it with a AsyncTask.
That is a terrible design. You do not want to have an infinite loop on the background every 15 seconds. That will drain battery when the app is pushed to the background, as one of the main problems.
One proper way to do it is to have an Alarm and react to that. A timer is not a bad option either.
I suggest you look into moving this to a service, which is what it looks like
A Service is an application component that can perform long-running operations in the background and does not provide a user interface
It talks about long-running operations, but recurrent operations also make sense to be moved to a service.
Official documentation on Services is here: http://developer.android.com/guide/components/services.html

Categories

Resources