简体   繁体   English

Android Webview页面未正确加载页面

[英]Android Webview Page not Loading Page correctly

I am trying to load a page into a android app webview using an Android Studio Emulator(Nexus 5). 我正在尝试使用Android Studio模拟器(Nexus 5)将页面加载到Android应用程序webview中。 However, the page is not loading correctly and is stuck at the loading screen. 但是,页面未正确加载并卡在加载屏幕上。 The page does require GeoLocation permissions however I have them already enabled. 该页面确实需要GeoLocation权限,但我已经启用了它们。

Below is my code and the XML for the app. 下面是我的代码和应用程​​序的XML。

MainActivity.java 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 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 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>

UPDATE: It's a geolocation issue for sure. 更新:这肯定是一个地理位置问题。 I don't have a gps.config file. 我没有gps.config文件。 I can't seem to find a concrete way to create one or what I need for that. 我似乎无法找到一种具体的方法来创建一个或我需要的东西。 The javascript on the website to get the location is 获取位置的网站上的javascript是

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

How can I then setup the webview in android to pass a user's current location? 我怎样才能在android中设置webview来传递用户的当前位置?

There are other users using this approach and it works fine, the problem is that you are testing this on an emulator . 还有其他用户使用这种方法,它工作正常, 问题是你在模拟器上测试它

In order to get a working GPS/Location API on the emulator you can use the geo fix command. 为了在模拟器上获得有效的GPS / Location API,您可以使用geo fix命令。

You can use this GUI tool to easly set a fake GPS position on your emulator. 您可以使用此GUI工具轻松地在模拟器上设置假的GPS位置。

Detailed information about the geo fix command can be found here . 有关geo fix命令的详细信息,请参见此处

I implemented onConsoleMessage method (in WebChromeClient ) to see what's going on and got an error: 我实现了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]

Make sure that GPS is enabled for the Emulator. 确保为仿真器启用了GPS。 There is another question with an good tutorial for this: How to emulate GPS location in the Android Emulator? 还有另一个问题,有一个很好的教程: 如何在Android模拟器中模拟GPS位置?

Hope that helps. 希望有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM