How to display Custom message before force close or ANR message will occur
How i can display custom error message before foreclose or Application not responding message will happen for an application.
Or
Can i display Custom message like "Please wait....." instead of Application not respon开发者_运维知识库ding message.
Thank you
You have to make sure you are handling any exceptions that are thrown. That is what causes a force close, an uncaught exception. as for the "not responding" message, again, this is up to the developer. You need to spawn new threads and do async tasks, that way processing does not block the main thread, which is what the UI runs on. When the main thread is blocked, that is when the "not responding" message happens. If you spawn a new thread, then you can display what ever kind of message you want to the user to notify them that something is processing.
I'll add to Ryan's answer that you can handle all critical exceptions without the need to wrap most of your code in try-catch. There is a beautiful method for that: Thread.setDefaultUncaughtExceptionHandler
. It is mainly useful for bug reporting. For the purpose of showing a message you have to set the handler in Application.onCreate
(you'll have to set <application>
's android:name
in the manifest to your custom Application class), save the application context and then use it to show a notification in the status bar - it remains after your application has been force closed. Also, in order to have the Force Close dialog appear instead of just freezing after your exception handler has worked, you should call defaultHandler = Thread.getDefaultUncaughtExceptionHandler();
in the handler's constructor and then in the end of public void uncaughtException(...)
call defaultHandler.uncaughtException(thread, ex);
.
精彩评论