The ANR window is generated for a variety of reasons.ProgramThe main thread is blocked because of Io read/write or network congestion, external storage devices are monopolized, or the system load is too high (that is, it is not a problem of self-compiled programs, may be a problem caused by the system or other third-party programs), may lead to the emergence of ANR windows.
From android
In 2.3, a new strictmode class is provided to help developers improve their android applications. strictmode can be used to capture time-consuming disks, network access, or function calls that occur in the main thread of an application, it can help developers to improve their programs, so that the main thread can process the UI and animation more smoothly in disk read/write and network operations, to avoid the main thread being blocked, leading to the occurrence of ANR windows.
The following describes how strictmode, a new feature of Android 2.3, works. For more information, seeCode:
Public Void Oncreate (){
If (Performance_mode ){
Strictmode. setthreadpolicy ( New Strictmode. threadpolicy. Builder ()
. Detectdiskreads ()
. Detectdiskwrites ()
. Detectnetwork () // Here we can replace it with detectall (), which includes disk read/write and network I/O.
. Penaltylog () // Print logcat. You can also locate the Dropbox and save the corresponding log through the file.
. Build ());
Strictmode. setvmpolicy ( New Strictmode. vmpolicy. Builder ()
. Detectleakedsqlliteobjects () // Test SQLite database operations
. Penaltylog () // Print logcat
. Penaltydeath ()
. Build ());
}
Super . Oncreate ();
}
The above code can be added in oncreate of application, so that monitoring can be performed at the very beginning of the program startup.
The output log is as follows:
02-27 10:03:56. 122: Debug/strictmode (16210): strictmode policy violation ;~ Duration = 696 MS: Android. OS. strictmode $ strictmodediskreadviolation: Policy = 23 violation = 2
02-27 10:03:56. 122: Debug/strictmode (16210): at Android. OS. strictmode $ androidblockguardpolicy. onreadfromdisk (strictmode. Java: 745)
02-27 10:03:56. 122: Debug/strictmode (16210): At Dalvik. system. blockguard $ wrappedfilesystem. Open (blockguard. Java: 228)
02-27 10:03:56. 122: Debug/strictmode (16210): At java. Io. fileoutputstream. < Init > (Fileoutputstream. Java: 94)
02-27 10:03:56. 122: Debug/strictmode (16210): At java. Io. fileoutputstream. < Init > (Fileoutputstream. Java: 66)
02-27 10:03:56. 122: Debug/strictmode (16210): At java. Io. filewriter. < Init > (Filewriter. Java: 42)
02-27 10:03:56. 122: Debug/strictmode (16210): At org. zelos. ASM. Main. writefile (main. Java: 30)
02-27 10:03:56. 122: Debug/strictmode (16210): At org. zelos. ASM. Main. oncreate (main. Java: 19)
02-27 10:03:56. 122: Debug/strictmode (16210): at Android. App. instrumentation. callactivityoncreate (instrumentation. Java: 1047)
02-27 10:03:56. 122: Debug/strictmode (16210): at Android. App. activitythread. initialize mlaunchactivity (activitythread. Java: 1611)
02-27 10:03:56. 122: Debug/strictmode (16210): at Android. App. activitythread. handlelaunchactivity (activitythread. Java: 1663)
02-27 10:03:56. 122: Debug/strictmode (16210): at Android. App. activitythread. Access $1500 (activitythread. Java: 117)
02-27 10:03:56. 122: Debug/strictmode (16210): at Android. App. activitythread $ H. handlemessage (activitythread. Java: 931)
02-27 10:03:56. 122: Debug/strictmode (16210): at Android. OS. handler. dispatchmessage (handler. Java: 99)
02-27 10:03:56. 122: Debug/strictmode (16210): at Android. OS. login. loop (login. Java: 123)
02-27 10:03:56. 122: Debug/strictmode (16210): at Android. App. activitythread. Main (activitythread. Java: 3683)
02-27 10:03:56. 122: Debug/strictmode (16210): At java. Lang. Reflect. method. invokenative (native method)
02-27 10:03:56. 122: Debug/strictmode (16210): At java. Lang. Reflect. method. Invoke (method. Java: 507)
02-27 10:03:56. 122: Debug/strictmode (16210): At com. Android. Internal. OS. zygoteinit $ methodandargscaller. Run (zygoteinit. Java: 839)
02-27 10:03:56. 122: Debug/strictmode (16210): At com. Android. Internal. OS. zygoteinit. Main (zygoteinit. Java: 597)
02-27 10:03:56. 122: Debug/strictmode (16210): At Dalvik. system. nativestart. Main (native method)
02-27 10:03:56. 162: Debug/strictmode (16210): strictmode policy violation ;~ Duration= 619 MS: Android. OS. strictmode $ strictmodediskwriteviolation: Policy = 23 violation = 1
02-27 10:03:56. 162: Debug/strictmode (16210): at Android. OS. strictmode $ androidblockguardpolicy. onwritetodisk (strictmode. Java: 732)
02-27 10:03:56. 162: Debug/strictmode (16210): At Dalvik. system. blockguard $ wrappedfilesystem. Open (blockguard. Java: 230)
02-27 10:03:56. 162: Debug/strictmode (16210): At java. Io. fileoutputstream. < Init > (Fileoutputstream. Java: 94)
02-27 10:03:56. 162: Debug/strictmode (16210): At java. Io. fileoutputstream. < Init > (Fileoutputstream. Java: 66)
02-27 10:03:56. 162: Debug/strictmode (16210): At java. Io. filewriter. < Init > (Filewriter. Java: 42)
02-27 10:03:56. 162: Debug/strictmode (16210): At org. zelos. ASM. Main. writefile (main. Java: 30)
02-27 10:03:56. 162: Debug/strictmode (16210): At org. zelos. ASM. Main. oncreate (main. Java: 19)
02-27 10:03:56. 162: Debug/strictmode (16210): at Android. App. instrumentation. callactivityoncreate (instrumentation. Java: 1047)
02-27 10:03:56. 162: Debug/strictmode (16210): at Android. App. activitythread. initialize mlaunchactivity (activitythread. Java: 1611)
02-27 10:03:56. 162: Debug/strictmode (16210): at Android. App. activitythread. handlelaunchactivity (activitythread. Java: 1663)
02-27 10:03:56. 162: Debug/strictmode (16210): at Android. App. activitythread. Access $1500 (activitythread. Java: 117)
02-27 10:03:56. 162: Debug/strictmode (16210): at Android. App. activitythread $ H. handlemessage (activitythread. Java: 931)
02-27 10:03:56. 162: Debug/strictmode (16210): at Android. OS. handler. dispatchmessage (handler. Java: 99)
02-27 10:03:56. 162: Debug/strictmode (16210): at Android. OS. login. loop (login. Java: 123)
02-27 10:03:56. 162: Debug/strictmode (16210): at Android. App. activitythread. Main (activitythread. Java: 3683)
02-27 10:03:56. 162: Debug/strictmode (16210): At java. Lang. Reflect. method. invokenative (native method)
02-27 10:03:56. 162: Debug/strictmode (16210): At java. Lang. Reflect. method. Invoke (method. Java: 507)
02-27 10:03:56. 162: Debug/strictmode (16210): At com. Android. Internal. OS. zygoteinit $ methodandargscaller. Run (zygoteinit. Java: 839)
02-27 10:03:56. 162: Debug/strictmode (16210): At com. Android. Internal. OS. zygoteinit. Main (zygoteinit. Java: 597)
02-27 10:03:56. 162: Debug/strictmode (16210): At Dalvik. system. nativestart. Main (native method)