【问题标题】:Android Webview Page not Loading Page correctlyAndroid Webview 页面未正确加载页面
【发布时间】:2015-10-14 21:39:50
【问题描述】:

我正在尝试使用 Android Studio Emulator(Nexus 5) 将页面加载到 android 应用 webview 中。但是,页面未正确加载并卡在加载屏幕上。该页面确实需要 GeoLocation 权限,但是我已经启用了它们。

下面是我的代码和应用程​​序的 XML。

MainActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    WebView webview;
    setContentView(R.layout.activity_main);
    webview = (WebView)findViewById(R.id.help_webview);
    //webview use to call own site

    webview.setWebViewClient(new WebViewClient());
    webview.getSettings().setJavaScriptEnabled(true);
    webview.getSettings().setDomStorageEnabled(true);
    webview.getSettings().setAllowContentAccess(true);
    webview.getSettings().setAllowFileAccess(true);
    webview.getSettings().setAppCacheEnabled(true);
    webview.getSettings().setAllowUniversalAccessFromFileURLs(true);
    webview.getSettings().setAllowContentAccess(true);
    webview.getSettings().setGeolocationEnabled(true);
    webview.getSettings().setDatabaseEnabled(true);
    webview.setWebChromeClient(new WebChromeClient() {
        public void onGeolocationPermissionsShowPrompt(String origin, GeolocationPermissions.Callback callback) {
            // callback.invoke(String origin, boolean allow, boolean remember);
            callback.invoke(origin, true, false);
        }
    });


    webview.loadUrl("https://lit-citadel-6989.herokuapp.com");
}

activity_main.xml

<WebView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/help_webview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:scrollbars="none"
    android:largeHeap="true" />

AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.example.example" >
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.BLUETOOTH"/>
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
    <uses-permission android:name="android.permission.ACCESS_GPS" />
    <uses-permission android:name="android.permission.ACCESS_ASSISTED_GPS" />
    <uses-permission android:name="android.permission.ACCESS_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />


    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

更新: 这肯定是地理定位问题。我没有 gps.config 文件。我似乎找不到一种具体的方法来创建一个或我需要的东西。网站上获取位置的javascript是

navigator.geolocation.getCurrentPosition(function(location){
            setLatLng(location);
            locationLoaded = true;
            callback(lat,lng);
});

然后如何在 android 中设置 webview 以传递用户的当前位置?

【问题讨论】:

  • 从 0 开始向 javascript 提供显式静态 lat,lng 值,然后在默认模拟器浏览器中打开页面查看其行为,如果可能,然后尝试获取设备而不是模拟器或至少使用genymotion 用于这类事情,因为它更多地依赖于硬件..顺便说一句,你能粘贴整个 java 类和 javascript 代码吗?

标签: java android xml webview


【解决方案1】:

other users 使用这种方法,它工作正常,问题是你在模拟器上测试它

为了在模拟器上获得有效的 GPS/Location API,您可以使用 geo fix 命令。

您可以使用this GUI tool 在您的模拟器上轻松设置虚假 GPS 位置。

关于geo fix命令的详细信息可以在here找到。

【讨论】:

    【解决方案2】:

    我实现了onConsoleMessage 方法(在WebChromeClient 中)以查看发生了什么并得到了错误:

    @Override
    public boolean onConsoleMessage(android.webkit.ConsoleMessage consoleMessage) {
        Log("WebConsole: " + consoleMessage.message() + " [line:" + consoleMessage.lineNumber() + "]");
    return true;
    }
    
    WebConsole: Uncaught SyntaxError: Unexpected token function [line:1]
    

    【讨论】:

    • 我似乎没有收到此消息。我也不确定这个错误的原因是什么。你能详细说明一下吗?
    【解决方案3】:

    确保已为模拟器启用 GPS。还有另一个问题有一个很好的教程:How to emulate GPS location in the Android Emulator?

    希望对您有所帮助。

    【讨论】:

      猜你喜欢
      • 2018-08-19
      • 2012-11-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-23
      • 1970-01-01
      相关资源
      最近更新 更多