简体   繁体   English

Android OS如何识别主线程上的网络调用并引发NetworkOnMainThreadException

[英]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.

相关问题 “NetworkOnMainThreadException”,但在主线程上没有网络调用 - “NetworkOnMainThreadException”, but have no network call on main thread android.os.NetworkOnMainThreadException如何修改在单独线程中运行的这段代码 - android.os.NetworkOnMainThreadException how to adapt this code that it runs in a seperate thread android os网络主线程异常 - android os network on main thread exception 网络操作导致 android.os.NetworkOnMainThreadException - Network operation results in android.os.NetworkOnMainThreadException Android 9.0在主线程上创建套接字导致android.os.NetworkOnMainThreadException - Android 9.0 creating a socket out on main thread cause android.os.NetworkOnMainThreadException android.os.NetworkOnMainThreadException 新线程内 - android.os.NetworkOnMainThreadException Inside new thread android.os.networkonmainthreadexception在新的Thread中 - android.os.networkonmainthreadexception inside a new Thread 在主线程上解析XML,但通过AsyncTask下载-“无效的流或编码:android.os.NetworkOnMainThreadException” - Parsing XML on main thread but downloaded via AsyncTask - “Invalid stream or encoding: android.os.NetworkOnMainThreadException” 来自非主线程的Android networkonmainthreadexception - Android networkonmainthreadexception from non main thread 为什么活动被破坏后在服务上抛出android.os.NetworkOnMainThreadException? - Why it throws android.os.NetworkOnMainThreadException on service after activity destroyed?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM