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?
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.
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]
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With