Possibility of an ANR in Standalone Service
Can an android process that does not contain any activities (i.e. a standalone service) receive an ANR?
Android mentions: "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开发者_StackOverflow社区 one of the following conditions:"
Does this mean a service without a UI interface can or cannot cause an ANR?
Yes, you can get an ANR in a process that contains a standalone service (no Activities). You will see the ANR in the logcat (it is technically an ANR). As an example, I've seen this in the following situation:
We have a BroadcastReceiver that runs in the same process as the standalone service. This runs on the main thread . If the broadcast intent cannot be dispatched to the BroadcastReceiver within a reasonable amount of time (because there is something blocking the main thread), the OS generates an ANR. The documentation seems to indicate that 10 seconds is the limit for BroadcastReceiver completion.
In general, the only thing that runs on the main thread in a standalone service process are the lifecycle methods (onCreate, onDestroy, etc.), BroadcastReceivers and anything dispatched by a Handler that you created on the main Thread. The rest of the code is running in other threads.
I would like to have answered your comment to CommonWare's answer, but I don't have enough reputation for that. So I've created another answer which hopefully contains the information you want.
Can an android process that does not contain any activities (i.e. a standalone service) receive an ANR?
In a manner of speaking, yes. It may not raise a dialog, but your service will be terminated if it takes too long on the main application thread.
精彩评论