How to resolve the ANR error while invoking the Camera?

非 Y 不嫁゛ 提交于 2019-11-28 14:04:31

You might have gone through this link while searching for the error.

Though I never had such issue, after reading on the Internet, this is what I understand:

Description :

ANR or Application Not Responding error occurs when a process on the main thread takes too long (something like > 5 seconds). Android kills that process and any related by design to spare the device's resources.

The solution is to run resource-expensive tasks on another thread, and then post, or update the main thread accordingly.

See This : Android ANR keyDispatchingTimedOut

So in your case you may be conflicting two different threads from the one that keeps running for long time and Android kills them. The next related code also gets affected.

So better classify your code, write each new task in different Thread,Handler and if you're doing a UI task, use runOnUIThread. Async Task is also very Handy.

Another thing is to try to remove dependency of code on other. Write some default values and from that you can send user back to first task if it's not done properly.

I believe the error is from your coding Style, not because of any specific error in your code.

You need to improve your that particular code to perform efficiently and for that see these 2 links:

  1. Design for Responsiveness
  2. Painless Threading

EDIT:

This I read somewhere and found effective is,

How to investigate ANR ?

First, go over your code and look for vunerable spots and long running operations. Examples may include using sockets, locks, thread sleeps, and other blocking operations from within the event thread. You should make sure these all happen in separate threads. If nothing seems the problem, use DDMS and enable the thread view. This shows all the threads in your application similar to the trace you have. Reproduce the ANR, and refresh the main thread at the same time. That should show you precisely whats going on at the time of the ANR

Also If ANR is caused because of Threads ?

you can user Service for that, So your app can do the time consuming tasks inside service.onStart(), passing data (for example) in the intent used to start the service.

However, Services execute on the main application thread. If a separate thread is needed, it can be created by the service inside onStart().

There is already a built-in class that does this: IntentService

Also found one helpful library application SalomonBrys/ANR-WatchDog

Android applications normally run entirely on a single (i.e. main) thread. This means that anything your application is doing in the main thread that takes a long time to complete can trigger the ANR dialog because your application is not giving itself a chance to handle the input event or Intent broadcast.So in that case you can use StrictMode to help find potentially long running operations such as network or database operations that you might accidentally be doing your main thread. If you find violations that you feel are problematic, there are a variety of tools to help solve them: threads, Handler, AsyncTask, IntentService, etc.Also remember that it came from API Level 9

Example code to enable from early in your Application, Activity, or other application component's onCreate() method:

 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
                 .detectDiskReads()
                 .detectDiskWrites()
                 .detectNetwork()   // or .detectAll() for all detectable problems
                 .penaltyLog()
                 .build());
         StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
                 .detectLeakedSqlLiteObjects()
                 .detectLeakedClosableObjects()
                 .penaltyLog()
                 .penaltyDeath()
                 .build());

I think you already know about this,

The single thread model can yield poor performance in Android applications that do not consider the implications. Since everything happens on a single thread performing long operations, like network access or database queries, on this thread will block the whole user interface. No event can be dispatched, including drawing events, while the long operation is underway. *From the user's perspective, the application appears hung*. Even worse, if the UI thread is blocked for more than a few seconds (about 5 seconds currently) the user is presented with the infamous "application not responding" (ANR) dialog.

An 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.

What Triggers ANR?

In Android, application responsiveness is monitored by the Activity Manager and Window Manager system services. Android will display the ANR dialog for a particular application when it detects one of the following conditions:

* No response to an input event (e.g. key press, screen touch) within 5 seconds
* A BroadcastReceiver hasn't finished executing within 10 seconds

How to resolve it please look at Designing for Responsiveness

EDIT:

So, Try to use other separate worker thread, handler, AsyncTask or UIThread for your camera stuff (media recorder) instead of main thread of activity..

1). Do "adb shell cat /data/anr/traces.txt" to see what your app was busy doing at that point. This is the very first step you need to take: understand what your app is doing that is causing the ANR.

When the ANR happens all of the process's stacks are written to this file, with the process being accused of the ANR first. All this is doing is dumping the file so you can look at it. So you just want to look at it some time after the ANR, before another ANR happens.

2). If nothing seems the problem, use DDMS and enable the thread view. This shows all the threads in your application similar to the trace you have. Reproduce the ANR, and refresh the main thread at the same time. That should show you precisely whats going on at the time of the ANR.

Fore more just go through this http://android-developers.blogspot.com/2009/05/painless-threading.html

May be you initialize and launch the media recorder in main thread itself either in Oncreate() or in Onresume(), instead of launching camera in main thread use separeate Handler to launch a camera for taking photos, it will act like child of main UI thread .

Using Handler you reduce the time and ANR will not be occur.

Mandar Limaye

It seems that the camera resource is not being release, thus causing the ANR.

The code looks a bit messy. You can take a look at the api example for camera preview:

You will have to implement onPause and onResume for it to work reliably.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!