[英]How Android OS recognizes network calls on main thread and throws NetworkOnMainThreadException
I'm trying to find the part of AOSP's source code which is responsible for guarding for making network calls on main thread and throwing NetworkOnMainThreadException
as a result but so far I failed. 我正在尝试查找AOSP源代码的一部分,该部分负责保护在主线程上进行网络调用并因此NetworkOnMainThreadException
,但到目前为止,我还是失败了。
So how Android recognizes network calls on main thread? 那么Android如何识别主线程上的网络调用? Does it check if some network component (maybe it happens in HW layer?) of the OS was touched? 它是否检查操作系统的某些网络组件(可能发生在硬件层?)?
Thanks for Michael Dodd answer I found where the exception is thrown (you can find it by this search ). 感谢Michael Dodd的回答,我找到了引发异常的地方(可以通过此搜索找到它)。 Now the question is who and when calls the android.os.StrictMode.onNetwork()
method. 现在的问题是谁和何时调用android.os.StrictMode.onNetwork()
方法。
Thanks for Michael Dodd answer I found how it works, eg for Android 8 there is: 感谢Michael Dodd的回答,我发现它是如何工作的,例如对于Android 8,有:
/**
* Resolves a hostname to its IP addresses using a cache.
*
* @param host the hostname to resolve.
* @param netId the network to perform resolution upon.
* @return the IP addresses of the host.
*/
private static InetAddress[] lookupHostByName(String host, int netId)
throws UnknownHostException {
BlockGuard.getThreadPolicy().onNetwork();
// ...
}
and (thanks to Michael Dodd) in android.os.StrictMode you can find: 和(感谢Michael Dodd)在android.os.StrictMode中可以找到:
// Part of BlockGuard.Policy interface:
public void onNetwork() {
if ((mPolicyMask & DETECT_NETWORK) == 0) {
return;
}
if ((mPolicyMask & PENALTY_DEATH_ON_NETWORK) != 0) {
throw new NetworkOnMainThreadException();
}
if (tooManyViolationsThisLoop()) {
return;
}
BlockGuard.BlockGuardPolicyException e = new StrictModeNetworkViolation(mPolicyMask);
e.fillInStackTrace();
startHandlingViolationException(e);
}
The mPolicyMask
sets the PENALTY_DEATH_ON_NETWORK
via android.os.StrictMode.enableDeathOnNetwork()
from android.app.ActivityThread.java : 所述mPolicyMask
设置PENALTY_DEATH_ON_NETWORK
经由android.os.StrictMode.enableDeathOnNetwork()
从android.app.ActivityThread.java :
/**
* For apps targetting Honeycomb or later, we don't allow network usage
* on the main event loop / UI thread. This is what ultimately throws
* {@link NetworkOnMainThreadException}.
*/
if (data.appInfo.targetSdkVersion >= Build.VERSION_CODES.HONEYCOMB) {
StrictMode.enableDeathOnNetwork();
}
With thanks to Anton A. for the initial pointer, the section of code responsible for throwing the NetworkOnMainThreadException
is StrictMode.java
, located within frameworks/base/core/java/android/os
. 多亏了Anton A.作为初始指针,负责NetworkOnMainThreadException
的代码部分是StrictMode.java
,它位于frameworks/base/core/java/android/os
。
This is the function in question. 这是有问题的功能。 As far as I'm aware this has remained the same since at least Android 4.3. 据我所知,至少从Android 4.3开始,这一点一直保持不变。
// Part of BlockGuard.Policy interface:
public void onNetwork() {
if ((mPolicyMask & DETECT_NETWORK) == 0) {
return;
}
if ((mPolicyMask & PENALTY_DEATH_ON_NETWORK) != 0) {
throw new NetworkOnMainThreadException();
}
if (tooManyViolationsThisLoop()) {
return;
}
BlockGuard.BlockGuardPolicyException e = new StrictModeNetworkViolation(mPolicyMask);
e.fillInStackTrace();
startHandlingViolationException(e);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.