7

I am trying to load a page into a android app webview using an Android Studio Emulator(Nexus 5). 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.

Below is my code and the XML for the app.

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>

UPDATE: It's a geolocation issue for sure. I don't have a gps.config file. 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

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?

applecrusher
  • 5,508
  • 5
  • 39
  • 89
  • go from 0 provide explicit static lat,lng values to javascript , secondly open the page in the default emulator browser see how it behaves , if its possible then try to get a device instead of emulator or atleast use genymotion for these kind of things since its more hardware dependant .. btw can you paste the whole class of java and code of javascript too thx – Ahmad Oct 22 '15 at 14:55

3 Answers3

5

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.

You can use this GUI tool to easly set a fake GPS position on your emulator.

Detailed information about the geo fix command can be found here.

Community
  • 1
  • 1
bonnyz
  • 13,458
  • 5
  • 46
  • 70
3

I implemented onConsoleMessage method (in WebChromeClient) to see what's going on and got an error:

@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]
Ozgur
  • 3,738
  • 17
  • 34
  • 1
    I don't seem to be getting this message. I am also not sure too what the cause of this error would be. Can you elaborate? – applecrusher Oct 15 '15 at 00:09
1

Make sure that GPS is enabled for the Emulator. There is another question with an good tutorial for this: How to emulate GPS location in the Android Emulator?

Hope that helps.

Community
  • 1
  • 1
Felix Gerber
  • 1,615
  • 3
  • 30
  • 40