Home > Blockchain >  Android Studio WebView Unable to ask user for their geolocation
Android Studio WebView Unable to ask user for their geolocation

Time:12-15

I have been developing a website and decided to wrap it into an app, so I am using Android Studio(Kotlin not java). I am very new to it and so far was able to launch the app with my website but can not get the location from the user, the permission wont pop up. Normaly the browser asked for permission but app does not. I was doing a lot searching and was trying to find a solution and the closest I got to the solution is this https://developer.android.com/training/location/permissions, but I am very fresh and all of this looks to me very confusing, I tried to paste the code and find that I need to create some classes for the location services so this brings me to this link https://thakkarkomal009.medium.com/update-location-in-background-using-foreground-service-android-7aee9de1a6d6 and the tutorial is still not clear to me. The guy tells me to create class, I do not know where to create the class, all of this direcories in android studio just confuse me. He also have a link to his project and I just cant understand all of this files. I was thinking that in kotlin in MainActivity I would just need to write a few lines of code to get the users location permission for the webview app and not go through all of this.

My MainActivity.kt looks like this

package com.example.hav

import android.os.Bundle
import android.webkit.WebView
import android.webkit.WebViewClient
import androidx.appcompat.app.AppCompatActivity


class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val myWeb = findViewById<WebView>(R.id.myWebView)
        
        myWeb.webViewClient= WebViewClient()
        
        myWeb.apply{
            loadUrl("linktomywebsite.com")
            settings.javaScriptEnabled = true
            settings.setGeolocationEnabled(true)
        }
        
    }
}

and my AndroidManifest.xml looks like this

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.hav">
    <uses-permission android:name="android.permission.INTERNET"/>
    <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" />
    <uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
    <application
        android:allowBackup="true"
        android:usesCleartextTraffic="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.Hav">
        <activity
            android:name=".MainActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <service
            android:name="dontknowwhattoputheresomeclass?"
            android:foregroundServiceType="location"
            android:exported="false" />
    </application>

</manifest>

I really hope that there is an easier solution to this problem, help noob solve it please if you had same issue before

CodePudding user response:

You have to request user to give permission for location manually. Check the official documentation Request Permissions for more info.

From the documentation I infer that the request permission code should be something like this.

// Register the permissions callback, which handles the user's response to the
val requestPermissionLauncher =
    registerForActivityResult(RequestPermission()
    ) { isGranted: Boolean ->
        if (isGranted) {
            // Permission is granted. Continue the action or workflow in your app.
        } else {
            // Not Granted
        }
    }

Check if permission is granted otherwise request using requestPermissionLauncher.launch(Manifest.permission.ACCESS_FINE_LOCATION).

when {
    ContextCompat.checkSelfPermission(
            CONTEXT,
            Manifest.permission.ACCESS_FINE_LOCATION
            ) == PackageManager.PERMISSION_GRANTED -> {
        // You can use the API that requires the permission.
        performAction(...)
    }
    shouldShowRequestPermissionRationale(...) -> {
        // In an educational UI, explain to the user why your app requires this
        // permission for a specific feature to behave as expected, and what
        // features are disabled if it's declined. In this UI, include a
        // "cancel" or "no thanks" button that lets the user continue
        // using your app without granting the permission.
        showInContextUI(...)
    }
    else -> {
        requestPermissionLauncher.launch(
            Manifest.permission.ACCESS_FINE_LOCATION)
    }
}

CodePudding user response:

You have to use WebChromeClient() as the Webview client. And override the onGeolocationPermissionsShowPrompt(String origin, GeolocationPermissions.Callback callback) method. ask the runtime permission if permission is not given by the user yet. and invoke

callback.invoke(origin, true, false);    
  • Related