• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Webview not recognizing the location permission of phone

Webview not recognizing the location permission of phone

I have a secure website that uses the watchPosition call to show my location. It works fine if I go to it in a browser. I used an online service to create an apk file ( i think it was appgeyser), and it works fine with that apk. I then installed the Android Studio so that I could make my own apk so that I could keep my privacy and have more control. The problem is that everything works just fine EXCEPT it will not recognize the location permission that the phone gives. Everything looks normal, the user is asked to accept the location permission, the location permission is set on the phone, but I still get the 'user denied geolocation' message. I made the code as simple as I know how, and have looked muchly on the internet for solution and have tried many things, but to no avail. Please help so that I can resolve this so that I can continue developing my application. Thanks.

My Phone
Verizon Samsung Galaxy S9
Android 8.0.0 Oreo
Developer Options
USB Degugging
Verify apps via USB
Keep Mobile Data turned on
+ various others

Development
Secure website: https://www.crudepinpoints.com
Android Development Studio 3.2.1

I run the above from Android Studio using my phone plugged into a usb port. The following happens when the program loads on the phone:

Phone asks: "Allow 'CrudePinpoints' to access this device's location. Deny Allow"
I choose Allow.
Phone responds via a toast: "Permission Granted"
My program responds when watchPosition is invoked : "Error: User denied Geolocation"
I go to the settings and location is enabled: Settings -> Apps -> CrudePinpoints = Permissions = Location

All of the code is below, both for the javascript and the java in the android app.

Javascript code in CrudePinpoints.html
Code:
///////////////////////////////////////////////////////////////////////
// showCurrentLocation - show users current location (watchPosition) //
///////////////////////////////////////////////////////////////////////
    function showCurrentLocation(position) {
        currentLatitude = position.coords.latitude;
        currentLongitude = position.coords.longitude;
        currentAccuracy = Math.round( position.coords.accuracy );
        currentPositionMarker.setPosition( new google.maps.LatLng( currentLatitude, currentLongitude));
    };
    function currentLocationError(err) {
        log("currentLocationError " + err.code + " " + err.message)
        if ( err.code == 1 ) {
            message("Error: User denied Geolocation");
        } else if ( err.code == 2 ) {
            message("Error: Position is unavailable! Internal error.");
        } else if ( err.code == 3 ) {
            message("Error: currentLocation Timeout!");
        };
    };
    function startWatchPosition(){
        log("startWatchPosition")
        watchID = navigator.geolocation.watchPosition( showCurrentLocation, currentLocationError, { enableHighAccuracy:true, maximumAge:10000, timeout:10000 } );
    };


    startWatchPosition()

Java xml files and code in my android application

activity_main.xml
Code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <WebView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/webview"/>
</RelativeLayout>
AndroidManifest.xmlns
Code:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.crudepinpoints.locationapplication">
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
    <uses-permission android:name="android.permission.INTERNET"/>
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        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>

MainActivity.java
Code:
package com.crudepinpoints.crudepinpoints;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.Manifest;
import android.content.pm.PackageManager;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    private int LOCATION_PERMISSION_CODE = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        WebView myWebView = (WebView) findViewById(R.id.webview);
        WebSettings webSettings = myWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setDomStorageEnabled(true);

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

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

                              
              
        myWebView.loadUrl("https://www.crudepinpoints.com");

    }
      @Override
      public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
     if (requestCode == LOCATION_PERMISSION_CODE)  {
         if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
             Toast.makeText(this, "Permission GRANTED", Toast.LENGTH_SHORT).show();
         } else {
             Toast.makeText(this, "Permission DENIED", Toast.LENGTH_SHORT).show();
         }
     }
     }
}
 
The code to ActivityCompat.requestPermissions is only in the code once. The extra one is left over where I was trying to access both FINE and COURSE location permissions to see if that made any difference. Sorry.



Webview not recognizing the location permission of phone

I have a secure website that uses the watchPosition call to show my location. It works fine if I go to it in a browser. I used an online service to create an apk file ( i think it was appgeyser), and it works fine with that apk. I then installed the Android Studio so that I could make my own apk so that I could keep my privacy and have more control. The problem is that everything works just fine EXCEPT it will not recognize the location permission that the phone gives. Everything looks normal, the user is asked to accept the location permission, the location permission is set on the phone, but I still get the 'user denied geolocation' message. I made the code as simple as I know how, and have looked muchly on the internet for solution and have tried many things, but to no avail. Please help so that I can resolve this so that I can continue developing my application. Thanks.

My Phone
Verizon Samsung Galaxy S9
Android 8.0.0 Oreo
Developer Options
USB Degugging
Verify apps via USB
Keep Mobile Data turned on
+ various others

Development
Secure website: https://www.crudepinpoints.com
Android Development Studio 3.2.1

I run the above from Android Studio using my phone plugged into a usb port. The following happens when the program loads on the phone:

Phone asks: "Allow 'CrudePinpoints' to access this device's location. Deny Allow"
I choose Allow.
Phone responds via a toast: "Permission Granted"
My program responds when watchPosition is invoked : "Error: User denied Geolocation"
I go to the settings and location is enabled: Settings -> Apps -> CrudePinpoints = Permissions = Location

All of the code is below, both for the javascript and the java in the android app.

Javascript code in CrudePinpoints.html
Code:
///////////////////////////////////////////////////////////////////////
// showCurrentLocation - show users current location (watchPosition) //
///////////////////////////////////////////////////////////////////////
    function showCurrentLocation(position) {
        currentLatitude = position.coords.latitude;
        currentLongitude = position.coords.longitude;
        currentAccuracy = Math.round( position.coords.accuracy );
        currentPositionMarker.setPosition( new google.maps.LatLng( currentLatitude, currentLongitude));
    };
    function currentLocationError(err) {
        log("currentLocationError " + err.code + " " + err.message)
        if ( err.code == 1 ) {
            message("Error: User denied Geolocation");
        } else if ( err.code == 2 ) {
            message("Error: Position is unavailable! Internal error.");
        } else if ( err.code == 3 ) {
            message("Error: currentLocation Timeout!");
        };
    };
    function startWatchPosition(){
        log("startWatchPosition")
        watchID = navigator.geolocation.watchPosition( showCurrentLocation, currentLocationError, { enableHighAccuracy:true, maximumAge:10000, timeout:10000 } );
    };


    startWatchPosition()

Java xml files and code in my android application

activity_main.xml
Code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <WebView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/webview"/>
</RelativeLayout>
AndroidManifest.xmlns
Code:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.crudepinpoints.locationapplication">
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
    <uses-permission android:name="android.permission.INTERNET"/>
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        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>

MainActivity.java
Code:
package com.crudepinpoints.crudepinpoints;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.Manifest;
import android.content.pm.PackageManager;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    private int LOCATION_PERMISSION_CODE = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        WebView myWebView = (WebView) findViewById(R.id.webview);
        WebSettings webSettings = myWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setDomStorageEnabled(true);

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

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

                             
             
        myWebView.loadUrl("https://www.crudepinpoints.com");

    }
      @Override
      public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
     if (requestCode == LOCATION_PERMISSION_CODE)  {
         if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
             Toast.makeText(this, "Permission GRANTED", Toast.LENGTH_SHORT).show();
         } else {
             Toast.makeText(this, "Permission DENIED", Toast.LENGTH_SHORT).show();
         }
     }
     }
}
 
Did I put this post in the wrong place? This is the first time that I have posted a question. I expected something from somebody. Any suggestions would be appreciated. Not nasty ones though. I am stuck and need some advice on how to fix my program. Thanks.
 
Back
Top Bottom