Reputation: 27
Help! I'm completely stuck with my webview app. The website page in my webview app shows a Google maps with the current posistion of the user. But i can't figure out why my code is not working properly? My webview ask for Geolocation permission when the user enter the app, but after giving the Geolocation permissions, the GPS is not turn on, and the G-maps shows nothing? In app-info >> permissions >> location is allowed. In chrome browser this all works perfect, but in my webview app nothing happens,
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
ActivityCompat.requestPermissions(
this, arrayOf(
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_COARSE_LOCATION
), 0
)
val UrlWindow: WebView = findViewById(R.id.UrlWindow)
UrlWindow.webViewClient = object : WebViewClient (){
override fun shouldOverrideUrlLoading(view: WebView?, url: String?): Boolean {
if (url != null) {
view?.loadUrl(url)
}
return true
}
}
UrlWindow.settings.javaScriptCanOpenWindowsAutomatically = true
UrlWindow.settings.databaseEnabled = true
UrlWindow.setWebChromeClient(object : WebChromeClient() {
override fun onGeolocationPermissionsShowPrompt(
origin: String,
callback: GeolocationPermissions.Callback
) {
callback.invoke(origin, true, false)
}
})
UrlWindow.settings.javaScriptEnabled = true
UrlWindow.settings.setGeolocationEnabled(true)
UrlWindow.loadUrl("https://mywebsite.com")
}
override fun onBackPressed() {
if (UrlWindow.canGoBack()) {
UrlWindow.goBack()
} else {
super.onBackPressed()
}
}
}
and my manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myapp" >
<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" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:usesCleartextTraffic="true"
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>
Any help is welcome!!
Upvotes: 0
Views: 253
Reputation: 453
Do the initialization of the webview once you got the result of the permissions in OnRequestPermissionResult
callback.
override fun onRequestPermissionsResult(
requestCode: Int,
permissions: Array<out String>,
grantResults: IntArray
) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
//Do the permission check here
initializeWebView()
}
Also the recommended steps are,
OnRequestPermissionResult
callback.Upvotes: 1