Android 中的 ANR 和崩溃有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20213482/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 02:28:28  来源:igfitidea点击:

What is the difference between ANR and crash in Android?

android

提问by user3035740

I have searched on the internet regarding what an ANR is. And I studied those references as well. But I don't get details regarding a crash in Android.

我在互联网上搜索过 ANR 是什么。我也研究了这些参考文献。但我没有得到有关 Android 崩溃的详细信息。

Can someone tell me the difference between ANR(Android not Responding) and a crash in Android?

有人能告诉我 ANR(Android 无响应)和 Android 崩溃之间的区别吗?

回答by Boardy

ANR stands for Application Not Responding.

ANR代表pplication ÑOT řesponding。

An ANR will occur if you are running a process on the UI thread which takes a long time, usually around 5 seconds. During this time the GUI (Graphical User Interface) will lock up which will result in anything the user presses will not be actioned. After the 5 seconds approx has occurred, if the thread still hasn't recovered then an ANR dialogue box is shown informing the user that the application is not responding and will give the user the choice to either wait, in the hope that the app will eventually recover, or to force close the app.

如果您在 UI 线程上运行一个需要很长时间(通常大约 5 秒)的进程,则会发生 ANR。在此期间,GUI(图形用户界面)将锁定,这将导致用户按下的任何操作都不会被执行。大约 5 秒后,如果线程仍未恢复,则会显示一个 ANR 对话框,通知用户应用程序没有响应,并将让用户选择等待,希望应用程序能够最终恢复,或强制关闭应用程序。

A crash is when an exception within the app has been thrown which has not been handled. For example, if you try to set the text of an EditTextcomponent, but the EditTextis null and there is no try catch statement to catch the exception that your app will crash and will be force closed. The user will not see what caused the crash, they will be shown a dialogue telling that the app has force closed unexpectedly and will give them the option to send a bug report. In this example if you were to look in the bug report you would see the error caused by java.lang.NullPointerException.

崩溃是指应用程序中引发了尚未处理的异常。例如,如果您尝试设置EditText组件的文本,但是它EditText为 null 并且没有 try catch 语句来捕获您的应用程序将崩溃并强制关闭的异常。用户不会看到导致崩溃的原因,他们将看到一个对话框,告诉他们应用程序已意外强制关闭,并为他们提供发送错误报告的选项。在本例中,如果您查看错误报告,您将看到由java.lang.NullPointerException.

Hope this helps.

希望这可以帮助。

回答by Shakeeb Ayaz

ANR(Application Not Responding )is due to handling long running task in Main Thread(UI thread).If the main thread is stopped for more than 5 sec you get ANR.

ANRpplication ñOT [Responding)是由于处理long running task in Main Thread(UI线程)。如果主线程停止超过5秒,你会得到ANR。

Crashare due to exception and errorlike Nullpoint,classNotfound, typecast ,parse error etc. ANR also causes crash of application.

崩溃是由于exception and error像 Nullpoint、classNotfound、typecast、解析错误等。ANR 也会导致应用程序崩溃。

Note: Never run long running task on UI thread

注意:切勿在 UI 线程上运行长时间运行的任务

Reference ANR

参考ANR

回答by zeeali

ANR and Crash Examples:

ANR 和崩溃示例:

This question already has an accepted answer, but I am adding 2 simple examples to understand ANR and Crash better.

这个问题已经有一个公认的答案,但我添加了 2 个简单的例子来更好地理解 ANR 和崩溃。

ANR:

ANR:

// this will produce an ANR on your app
int i = 0;
while(true) {
    i++;
}

Crash:

碰撞:

// this will crash your app : will produce java.lang.ArithmeticException
int value = 5, i = 0;
int result = value / i;

回答by Shri

Application Not Responding (ANR):

应用程序无响应 (ANR):

ANR will display in the following conditions:

ANR 将在以下情况下显示:

  • Response to the input event (such as key press or screen touch even) within 5 Sec.

  • A Broadcast Receiver hasn't finished executing within 10 Sec.

  • 5秒内响应输入事件(如按键或屏幕触摸)。

  • 广播接收器在 10 秒内没有完成执行。

How to avoid ANRs?

如何避免 ANR?

  • Create a different worker thread for long running operations like database operations, network operations etc.

  • 为长时间运行的操作(如数据库操作、网络操作等)创建不同的工作线程。

Reinforce Responsiveness: In android app usually, 100 to 200 ms is the threshold beyond which user will feel that app is slow. Following are the tips through which we can show application more responsive.

Reinforce Responsiveness:在Android应用程序中,通常100到200毫秒是用户会觉得应用程序很慢的阈值。以下是我们可以显示应用程序响应更快的提示。

  • Show progress dialog whenever you are doing any background work and a user is waiting for the response.

  • For games specifically, do calculations for moves in the worker thread.

  • Show splash screen if your application has time-consuming initial setup.
  • 每当您进行任何后台工作并且用户正在等待响应时,显示进度对话框。

  • 特别是对于游戏,在工作线程中计算移动。

  • 如果您的应用程序具有耗时的初始设置,则显示启动画面。

Crash:The crash is unhandled condition into the application and it will forcefully close our application. Some of the examples of crashes are like Nullpointer exception, Illegal state exception etc.

崩溃:崩溃是应用程序中未处理的情况,它将强制关闭我们的应用程序。一些崩溃的例子如空指针异常、非法状态异常等。

回答by Display name

ANR stands for Application Not Responding, which means that your app does not register events on the UI Thread anymore because a long running operation is executed there

ANR 代表 Application Not Responding,这意味着您的应用程序不再在 UI 线程上注册事件,因为在那里执行了长时间运行的操作

回答by nitesh

ANR: It is called when anything your application is doing in the UI thread that 
takes a long time to complete (5 sec approx)

Reference: ANR

参考: ANR

Crash: It is called when  your Application gets some Error or Exception raised by the DVM

回答by KishuDroid

ANR stands for Application Not Responding.

ANR 代表应用程序无响应

It can occur due to many reasons like if an application blocks on some I/O operation on the UI thread so the system can't process incoming user input events. Or perhaps the app spends too much time building an elaborate in-memory structure or computing the next move in the UI thread.

发生这种情况的原因有很多,例如应用程序阻塞 UI 线程上的某些 I/O 操作,因此系统无法处理传入的用户输入事件。或者应用程序花费太多时间构建复杂的内存结构或计算 UI 线程中的下一步。

Blocking the main thread, won't result in a crash, but a popup will be displayed to let users kills the app after 5 seconds.

阻塞主线程不会导致崩溃,但会显示一个弹出窗口,让用户在 5 秒后杀死应用程序。

But For Crash, the main reason is the human errors. Most of the time an app crashes is because of a coding/design error made by human

但是对于Crash 来说,主要原因是人为错误。大多数情况下,应用程序崩溃是因为人为编码/设计错误

Human Errors

人为错误

Lack of testing

缺乏测试

Null Pointer exception

空指针异常

OutofMemory

内存不足

Example :

例子 :

This is common when a programmer makes a reference to an object or variable that does not exist, basically creating a null-pointer error.

当程序员引用不存在的对象或变量时,这很常见,基本上会产生空指针错误。

If you have a bad connection, that can also make your apps crash. The app could also have memory management problems.

如果您的连接不良,这也会使您的应用程序崩溃。该应用程序也可能存在内存管理问题。

Please see my answer for the type of android specific exception which may cause the crash.

请参阅我对可能导致崩溃的 android 特定异常类型的回答。

Android Specific Exception

Android 特定异常

回答by Anirban

ANR also caused by-

ANR也由-

  1. No response to an input event (such as key press or screen touch events) within 5 seconds.
  2. A BroadcastReceiver hasn't finished executing within 10 seconds.
  1. 5 秒内对输入事件(例如按键或屏幕触摸事件)没有响应。
  2. BroadcastReceiver 未在 10 秒内完成执行。

回答by zohaib

ANR stands for Application Not Responding and its occurs when long operation takes place into Main thread......

ANR 代表应用程序无响应,它发生在主线程中进行长时间操作时......

Crash are due to exception and error like Nullpoint,

崩溃是由于像 Nullpoint 这样的异常和错误,

回答by Hardik Darji

This is good article at developer portal. It gives clarity in detail about ANR. https://developer.android.com/training/articles/perf-anr.html

这是开发人员门户上的好文章。它详细说明了 ANR。 https://developer.android.com/training/articles/perf-anr.html