Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Java for Android
  • Android Studio
  • Android Kotlin
  • Kotlin
  • Flutter
  • Dart
  • Android Project
  • Android Interview
Open In App
Next Article:
What is ANR and How it Can be Prevented in Android?
Next article icon

What is ANR and How it Can be Prevented in Android?

Last Updated : 06 Feb, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

ANR stands for Application Not Responding. An ANR will occur if you're running a process on the UI thread which takes an extended time, usually around 5 seconds. During this point, the GUI (Graphical User Interface) will lock up which can end in anything the user presses won't 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 appliance isn't responding and can give the user the choice to either wait, in the hope that the app will eventually recover, or to force close the app. This is how the ANR dialog displayed to the user.

ANR dialog displayed to the user

A crash is when an exception within the app has been thrown which has not been handled. For example, if you are trying to set the text of an EditText component, but the EditText is null and there's no try-catch statement to catch the exception then your app will crash and can be force closed. The user won't see what caused the crash, they're going to be shown a dialogue telling that the app has force closed unexpectedly and can give them the choice to send a bug report. For example, an error caused by java.lang.NullPointerException is an ANR. An ANR happens when some long operation takes place in the "main" thread. This is the event loop thread, and if it's busy, Android cannot process any longer GUI events within the application, and thus presents an ANR dialog. Now, within the trace you posted, most thread seems to be doing fine, there's no problem. It is idling within the MessageQueue, expecting another message to return. In your case, the ANR was likely a longer operation, rather than something that blocked the thread permanently, so the event thread recovered after the operation finished, and your trace went through after the ANR.

How to prevent an ANR?

Stop doing heavy tasks on the main thread. Instead use worker threads such as IntentService, AsyncTask Handler, or another Thread simply. Detecting where ANRs happen is straightforward if it's a permanent block (deadlock acquiring some locks for instance), but harder if it's just a short-lived delay. First, re-evaluate your code and appearance for vulnerable 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 matter, 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 most thread at an equivalent time. That should show you exactly what's happening at the time of the ANR. An ANR is going to be triggered for your app when one among the subsequent conditions occur:

  1. While your activity is within the foreground, your app has not fed with an input event or BroadcastReceiver (such as key press or screen touch events) within 5 seconds.
  2. While you aren't doing an activity within the foreground, your BroadcastReceiver hasn't finished executing within a substantial amount of your time.

If your app is experiencing ANRs, you'll use the guidance during this article to diagnose and fix the matter.

Android vitals

Android vitals can help improve your app's performance by alerting you, via the Play Console, when your app is exhibiting excessive ANRs. Android vitals considers ANRs excessive when an app:

  1. Exhibits a minimum of one ANR in a minimum of 0.47% of its daily sessions.
  2. Exhibits 2 or more ANRs in a minimum of 0.24% of its daily sessions.
  3. Exhibits 3 or more ANRs in a minimum of 0.17% of its daily sessions.
  4. Exhibits 3 or more ANRs in a minimum of 0.14% of its daily sessions.

Diagnosing ANRs

The ANRs could be solved easily as the Android Interface Provide Efficient Tools to remove them! Finding them may include (but are not limited to):

  1. The app is doing slow operations which involves the I/O operation of the operating system.
  2. The main thread is doing a synchronous binder call to a different process, which another process is taking an extended time to return.
  3. The main thread is during a deadlock with another thread, either in your process or via a binder call. the most thread isn't just expecting an extended operation to end but is during a deadlock situation.
  4. The app is doing an extended calculation on the basic thread.

Using the Android Strict Mode (API Level > 9)

Starting API Level 9 using StrictMode will help you discover certain accidental I/O operations on the most thread while you’re developing your app. you'll use StrictMode at the appliance or activity level. Read more about how to use StrictMode here.

Moreover, you can also find about the ANRs by pulling a Traces File from Android ADB as Follows:

adb root adb shell ls /data/anr adb pull /data/anr/<filename>

But if your users are still experiencing ANRs, you are bound to check out the status of the prime a.k.a. main thread in Android Device Monitor. Usually, the main thread is in the RUNNABLE state if it’s ready to update the UI and is generally responsive if everything is fine!

An Example

Java
@Override public void onClick(View view) {     // This task works on the      // main thread and will cause an ANR.     MergeSort.sort(data); } 

In the above-mentioned case what we need to do is to remove the sorting process from the main thread and place it into another class or thread, so that the system can handle it asynchronously.

A Tip

When an ANR occurs and your app crashes out of nowhere, Android Studio logs some info related to the case in a text file on the device itself. You can use ADB to gather logs and have a look. You can also add Crashlytics from Firebase to catch hold of ANRs when they occur in your app, and get their frequency, and event the activity thread on which it happened! View More Details about ANR by visiting Here.


Next Article
What is ANR and How it Can be Prevented in Android?

T

therebootedcoder
Improve
Article Tags :
  • Android

Similar Reads

    What's Android Interface Definition Language (AIDL) in Android?
    The Android Interface Definition Language (AIDL) is analogous to other IDLs you would possibly have worked with. It allows you to define the programming interface that both the client and repair agree upon so as to speak with one another using interprocess communication (IPC). Traditionally the Andr
    7 min read
    How to Hide Apps on an Android Phone?
    Want to keep certain apps private on your Android phone? Hiding apps can help you maintain privacy and declutter your home screen. Whether you want to conceal personal apps or just keep your phone organized, there are several methods to hide apps on an Android phone.In this guide, we will provide st
    15 min read
    What is "Don’t Keep Activities" in Android?
    Have you ever came across an option under Developer Settings on your mobile phone and wondered what is it all about? If you play around with your phone's settings, then you will discover an option depicting "Don't keep activities" hidden under "Developer Options". For this, you have to be in develop
    3 min read
    How to Handle Network Operations in an Android Application?
    Handling network operations in an Android application is an essential task for developers. Network operations typically involve making HTTP requests to web services or APIs and receiving responses from them. These operations can include fetching data from a server, uploading files or data, authentic
    5 min read
    What are the Reasons For the Exit in Android Application?
    Developing Android Apps can be a tedious job, and frustrating too if our app is exiting unknowingly! In order to log exits in Android, we rely on a number of third-party libraries. With Android R, we can now log the exits in our app, which can assist us in resolving any issues that may arise. In thi
    5 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences