【问题标题】:Why does Google recommend this code for WebView debugging?为什么 Google 推荐这段代码用于 WebView 调试?
【发布时间】:2015-07-22 08:02:30
【问题描述】:

根据 debuggable 标志启用 WebView 调试,Google recommends the following code

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
    if (0 != (getApplicationInfo().flags &= ApplicationInfo.FLAG_DEBUGGABLE)) {     
        WebView.setWebContentsDebuggingEnabled(true);
    }
}

这不会有副作用吗,因为&= 运算符重新分配了flag 字段?我假设在此调用之后应用程序标志减少到FLAG_DEBUGGABLE。为什么要在此处使用 &= 运算符而不是 &

【问题讨论】:

    标签: android google-chrome android-webview


    【解决方案1】:

    是的,这是文档中的错误。代码被破坏了。它确实从ApplicationInfo.flags 中删除了除FLAG_DEBUGGABLE 之外的任何标志。正确的检查是getApplicationInfo().flags & ApplicationInfo.FLAG_DEBUGGABLE。此检查不会修改 flags 字段。

    【讨论】:

      【解决方案2】:

      不,&= 表示 getApplicationInfo().flags = getApplicationInfo().flags & ApplicationInfo.FLAG_DEBUGGABLE,它只是将该标志从 0 设置为 1

      就像+=|= 一样缩短了表达式。

      将保留所有现有标志。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-09-19
        • 2011-05-27
        • 1970-01-01
        • 2012-02-17
        • 1970-01-01
        • 2012-04-22
        • 1970-01-01
        相关资源
        最近更新 更多