11

I am trying to create WebView which would get GPS localization via js, but when i click on button which should show localization: in android 4.1.1(emulator): "Error code 2. Failed to start Geolocation service in android 4.1.2(phone) just nothing happens in android 6.0(emulator) the same, just nothing happens

My permissions:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>

My settings:

webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setGeolocationEnabled(true);
        webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
        webView.getSettings().setBuiltInZoomControls(true);

        webView.getSettings().setSaveFormData(false);
        webView.getSettings().setSavePassword(false);
        webView.getSettings().setAppCacheEnabled(true);
        webView.getSettings().setDatabaseEnabled(true);
        webView.getSettings().setDomStorageEnabled(true);
        webView.getSettings().setGeolocationDatabasePath(getFilesDir().getPath());

WebChromeClient:

WebChromeClient webChromeClient = new WebChromeClient(){

            @Override
            public void onGeolocationPermissionsShowPrompt(String origin, GeolocationPermissions.Callback callback) {
                // callback.invoke(String origin, boolean allow, boolean remember);
                callback.invoke(origin, true, false);
            }

            public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture){
                mUploadMessage = uploadMsg;
                Intent i = new Intent(Intent.ACTION_GET_CONTENT);
                i.addCategory(Intent.CATEGORY_OPENABLE);
                i.setType("*/*");
                MainActivity.this.startActivityForResult(Intent.createChooser(i, "File Chooser"), MainActivity.FILECHOOSER_RESULTCODE);

            }

            public boolean onShowFileChooser (WebView webView, ValueCallback<Uri[]> filePathCallback, WebChromeClient.FileChooserParams fileChooserParams){
                Intent i = new Intent(Intent.ACTION_GET_CONTENT);
                i.addCategory(Intent.CATEGORY_OPENABLE);
                i.setType("*/*");
                MainActivity.this.startActivityForResult(Intent.createChooser(i, "File Chooser"), MainActivity.FILECHOOSER_RESULTCODE);
                return false;
            }

        };

WebViewClient:

WebViewClient webViewClient = new WebViewClient(){
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url){
                view.loadUrl(url);
                return true;
            }

            public void onPageFinished(WebView view, String url){
                progressBar.setVisibility(View.GONE);
                webView.setVisibility(View.VISIBLE);
                refreshBtn.setVisibility(View.VISIBLE);
            }
        };

at the end, i have this line of code:

ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION}, 0);

and js code (not my code):

    <script>
function getLocationConstant()
{
  if(navigator.geolocation)
  {
   navigator.geolocation.getCurrentPosition(onGeoSuccess,onGeoError);
  } else {
   alert("Brak obsługi GPS");
  }
}


function onGeoSuccess(event)
{
  document.getElementById("skad").value =  event.coords.latitude+", "+event.coords.longitude;
 document.getElementById('szk').click();

}


function onGeoError(event)
{
  alert("Error code " + event.code + ". " + event.message);
}


</script>

<input type="text" name="skad" style="width:278px;" id="skad" >
Qiteq
  • 675
  • 1
  • 6
  • 22
  • there would be some stack trace. add it here. – Rohit Arya Mar 24 '16 at 21:08
  • I have it from 4.1.1 only: W/EGL_emulation: eglSurfaceAttrib not implemented, android 6.0 and 4.1.2 phone print nothing – Qiteq Mar 24 '16 at 21:20
  • 1
    Im running into same issue, especially when user disables locationing in their settings, have you found out the cause? – Jeremy Aug 20 '16 at 02:51
  • I've solved it, but now I don't remember what was the problem. Unfortunately, I will be able to check it only at the evening. – Qiteq Aug 20 '16 at 07:57
  • @Jeremy : I am facing the issue when location is turned off in settings.. No error log is printed. Unable to apply any fix without log. In chrome browser same page.. when location is turned off in settings.. it shows a prompt to the user. – srj Jun 22 '18 at 14:17

2 Answers2

18

I am not sure why, but now it works. There is code for simply app with webView which use gps location.
MainActivity:

package com.qiteq.gpswebview;

import android.Manifest;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.webkit.GeolocationPermissions;
import android.webkit.WebChromeClient;
import android.webkit.WebView;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        WebView wv = new WebView(this);
        wv.loadUrl("http://qiteq.pl/stack/index.html");
        setContentView(wv);

        ActivityCompat.requestPermissions(this, new String[]{
                Manifest.permission.ACCESS_FINE_LOCATION,
                Manifest.permission.ACCESS_COARSE_LOCATION
        }, 0);

        wv.getSettings().setJavaScriptEnabled(true);


        wv.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onGeolocationPermissionsShowPrompt(String origin, GeolocationPermissions.Callback callback) {
                callback.invoke(origin, true, false);
            }
        });


    }
}

Manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.qiteq.gpswebview">

    <uses-permission android:name="android.permission.INTERNET" />
    <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">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>

And index.html:

<script>
function getLocationConstant()
{
  if(navigator.geolocation)
  {
   navigator.geolocation.getCurrentPosition(onGeoSuccess,onGeoError);
  } else {
   alert("No GPS support");
  }
}


function onGeoSuccess(event)
{
    document.getElementById("location").value =  event.coords.latitude+", "+event.coords.longitude;
    alert("Success: "+event.coords.latitude+", "+event.coords.longitude);
}


function onGeoError(event)
{
  alert("Error code " + event.code + ". " + event.message);
}


</script>

<input type="text" name="location" id="location" style="width:278px;">
<button onclick="getLocationConstant()" >Click</button>
Qiteq
  • 675
  • 1
  • 6
  • 22
1

check this code function so good, its necesary for sdk >4 implements the next, join with requestpermisision.

@Override
        public void onGeolocationPermissionsShowPrompt(final String origin, final GeolocationPermissions.Callback callback) {
            //Log.i(TAG, "onGeolocationPermissionsShowPrompt()");

            final boolean remember = true;
            AlertDialog.Builder builder = new AlertDialog.Builder(vistamaps.this);
            builder.setTitle("Locations");
            builder.setMessage("Would like to use your Current Location ")
                    .setCancelable(true).setPositiveButton("Allow", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    // origin, allow, remember
                    callback.invoke(origin, true, remember);
                    int result = ContextCompat.checkSelfPermission(getBaseContext(), Manifest.permission.ACCESS_COARSE_LOCATION);
                    if (result == PackageManager.PERMISSION_GRANTED) {

                    } else {
                        ActivityCompat.requestPermissions(vistamaps.this, new String[]{Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION}, 101);
                    }
                }
            }).setNegativeButton("Don't Allow", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    // origin, allow, remember
                    callback.invoke(origin, false, remember);
                }
            });
            AlertDialog alert = builder.create();
            alert.show();
        }
  • and the next code its to see what happen, implements into class webChromeClient -- @Override public void onPermissionRequest(PermissionRequest request) { Log.e("WebView", "onPermissionRequest: "+request.toString() ); super.onPermissionRequest(request); } – Oscar Omar Cruz Bernal May 30 '18 at 21:51