Geolocation not working on webview android - android

I 'm trying to create an application where it will find the location of a user and it will show his location on a map. I have read the other articles about this issue and the suggested solutions but nothing worked for me. This Web app runs good in browsers (mobile and pc) but when I am trying to run it as an application stacks in the javascript of html file. I think that is a problem of permissions in javascript. This is the code of this project.
JAVA Code
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.GeolocationPermissions;
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
WebView webview = (WebView) findViewById(R.id.webview);
webview.getSettings().setJavaScriptEnabled(true);
webview.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
webview.getSettings().setGeolocationEnabled(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("file:///android_asset/www/geoMap.html");
}
}
AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="gr.######.navigator"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<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:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
HTML
<!DOCTYPE html>
<html>
<head>
<script src="http://code.jquery.com/jquery-1.9.0rc1.js"></script>
<script>
jQuery(window).ready(function(){
$("#btnInit").click(function()
{
alert('click button');
initiate_geolocation();
});
});
function initiate_geolocation() {
alert('function initiate_geolocation');
navigator.geolocation.getCurrentPosition(handle_geolocation_query,handle_errors);
}
function handle_errors(error)
{
alert('function handle_errors');
switch(error.code)
{
case error.PERMISSION_DENIED: alert("user did not share geolocation data");
break;
case error.POSITION_UNAVAILABLE: alert("could not detect current position");
break;
case error.TIMEOUT: alert("retrieving position timed out");
break;
default: alert("unknown error");
break;
}
}
function handle_geolocation_query(position){
alert('function handle_geolocation_query');
alert('Lat: ' + position.coords.latitude +
' Lon: ' + position.coords.longitude);
}
</script>
</head>
<body>
<div>
<button id="btnInit" >Find my location</button>
</div>
</body>
</html>
I 've spend several hours of searching and trying suggestion from the web but nothing worked. If someone knows something more about this issue please help.
Thanks a lot.

Bro, I too faced the same problem , i fixed by adding these below lines.hope you will also
webView.getSettings().setAppCacheEnabled(true);
webView.getSettings().setDatabaseEnabled(true);
webView.getSettings().setDomStorageEnabled(true);

Related

How to provide access to video source to an Android webview app?

I'm embedding a website that uses video source from a device to classify certain objects. I am using Android Studio and Webview for embedding the website but I'm getting stuck in reading the video source from the app. Below is the code that I've used so far.
MainActivity.java
package com.example.xxx;
import android.os.Bundle;
import android.webkit.PermissionRequest;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import com.example.xxx.R;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
String websiteURL = "https://smartphone.edgeimpulse.com/classifier.html?apiKey=ei_c728c47b9eeeaf5604fb4d666a5a48e2813513647e75d9ab322fe16f867a6f92"; // sets web url
private WebView webview;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Webview stuff
webview = findViewById(R.id.webView);
webview.getSettings().setJavaScriptEnabled(true);
webview.getSettings().setDomStorageEnabled(true);
webview.setOverScrollMode(WebView.OVER_SCROLL_NEVER);
webview.loadUrl(websiteURL);
webview.setWebChromeClient(new WebChromeClient() {
#Override
public void onPermissionRequest(final PermissionRequest request) {
request.grant(request.getResources());
}
});
}}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.edgeimpulse">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.CAPTURE_VIDEO_OUTPUT" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<application
android:allowBackup="true"
android:dataExtractionRules="#xml/data_extraction_rules"
android:fullBackupContent="#xml/backup_rules"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/Theme.EdgeImpulse"
tools:targetApi="31">
<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>
</application>
</manifest>
Here is the error I'm getting:
Can someone help me out with this?

Why my android studio web view app goes blank white after splash screen in signed release and works perfect id debug?

Some how it works on older devices but i tried in 2 new devices the release app doesnt work its shows a white blank screeen after splash screen
And there is no error showing please answer me
main activity
private WebView mywebview;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mywebview = (WebView) findViewById(R.id.webview);
WebSettings webSettings = mywebview.getSettings();
mywebview.setWebViewClient(new WebViewClient());
mywebview.loadUrl("https://netixshop.com/");
webSettings.setJavaScriptEnabled(true);
webSettings.setDomStorageEnabled(true);
}
public class myWebClient extends WebViewClient {
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
#Override
public void onBackPressed() {
if (mywebview.canGoBack()) {
mywebview.goBack();
}else{
super.onBackPressed();
}
}
}
here is my manifest
pleasse check this out and find why is the error caused
android manifest
<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:usesCleartextTraffic="true"
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"
tools:ignore="GoogleAppIndexingWarning">
<activity android:name=".MainActivity"></activity>
<activity android:name=".splashactivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>```
Probably you didn't add the internet permission to Manifest file while it is added to debug manifest file by default. So you need to add the internet permission;
<uses-permission android:name="android.permission.INTERNET" />
and if you work with android 8 or above, probably you will need to add this line under application tag for accessing to non-https web urls;
android:usesCleartextTraffic="true"
finally it will look like this;
<?xml version="1.0" encoding="utf-8"?>
<manifest ...>
<uses-permission android:name="android.permission.INTERNET" />
<application
...
android:usesCleartextTraffic="true"
...>
...
</application>
</manifest>

Android local storage

I have a problem with a webview which may access to the localStorage by app. My index.html informs that local storage is'nt supported by my browser (ie. the webview). If you have any suggestion.. Please take a look at my code :
Activity.java:
package com.exercise.AndroidHTML;
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;
public class AndroidHTMLActivity extends Activity {
WebView myBrowser;
;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myBrowser = (WebView)findViewById(R.id.mybrowser);
myBrowser.loadUrl("file:///android_asset/index.html");
WebSettings webSettings = myBrowser.getSettings();
//enable JavaScript in webview
webSettings.setJavaScriptEnabled(true);
//Enable and setup JS localStorage
webSettings.setDomStorageEnabled(true);
webSettings.setDatabaseEnabled(true);
}
}
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.exercise.AndroidHTML"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="4" />
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name=".AndroidHTMLActivity"
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>
setDatabasePath() method was deprecated in API level 19. I advise you to use storage locale like this:
webView.getSettings().setDomStorageEnabled(true);
webView.getSettings().setDatabaseEnabled(true);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
webView.getSettings().setDatabasePath("/data/data/" + webView.getContext().getPackageName() + "/databases/");
}
You may have to set the database path:
webView.getSettings().setDatabasePath("/data/data/" + webView.getContext().getPackageName() + "/databases/");

application runs but no app icon in emulator

In my code I have a webview. the code runs and shows the web content.But after pressing the backward key in emulator i can not see the app icon in it for reopening. the logcat shows the following error also
WebActivity.java
package example.webapp;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class WebActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web);
WebView mainWebView = (WebView) findViewById(R.id.mainWebView);
WebSettings webSettings = mainWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
mainWebView.setWebViewClient(new MyCustomWebViewClient());
mainWebView.loadUrl("http://google.com");
mainWebView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
}
private class MyCustomWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
}
logcat
08-22 17:53:11.464: E/geolocationService(278): Caught security exception registering for location updates from system. This should only happen in DumpRenderTree. please give me an answer
manifest file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="example.webapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="8" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="example.webapp.WebActivity"
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>

Keep Android application active on screen timeout

I'm fairly new to making Android application so I got a question.
If I got my application running and I wait for a few seconds my screen timeouts. The problem is that when I unlock my screen again my application is closed. I want the application to stay active when the screen times out, so that I don't have to start the application all over again.
I don't want the application to run in the background and also I do not want to disable the screen timeout. I just don't want the application to close itself after the screen automatically times out.
Is there any chance of doing so?
_EDIT > Added some of my code.
 
Android Manifest
<supports-screens
android:largeScreens="true"
android:normalScreens="true"
android:smallScreens="true"
android:resizeable="true"
android:anyDensity="true"
/>
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.VIBRATE" />
<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_LOCATION_EXTRA_COMMANDS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.BROADCAST_STICKY" />
<uses-sdk android:minSdkVersion="10" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name=".SIOM_AndroidActivity"
android:label="#string/app_name"
android:screenOrientation="portrait"
android:configChanges="orientation|keyboardHidden"
android:theme="#android:style/Theme.NoTitleBar" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.phonegap.DroidGap"
android:label="#string/app_name"
android:screenOrientation="portrait"
android:configChanges="orientation|keyboardHidden" >
<intent-filter>
</intent-filter>
</activity>
</application>
</manifest>
 
Android Activity
package com.siom.android;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.KeyEvent;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import com.phonegap.*;
public class SIOM_AndroidActivity extends DroidGap {
/** Called when the activity is first created. */
/** #Override */
WebView mWebView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.init();
super.clearCache();
setContentView(R.layout.main);
mWebView = (WebView) findViewById(R.id.webview);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
mWebView.setWebViewClient(new HelloWebViewClient());
mWebView.setWebChromeClient(new MyWebChromeClient());
mWebView.loadUrl("http://www.example.com/test.html");
}
// Maakt een WebChromeClient class aan
final Context myApp = this;
final class MyWebChromeClient extends WebChromeClient {
// Zorgt er voor dat confirm werkt in WebView
#Override
public boolean onJsConfirm(WebView view, String url, String message, final JsResult result) {
new AlertDialog.Builder(myApp)
.setTitle("Title here")
.setMessage(message)
.setPositiveButton(android.R.string.ok,
new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int which)
{
result.confirm();
}
})
.setNegativeButton(android.R.string.cancel,
new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int which)
{
result.cancel();
}
})
.create()
.show();
return true;
}
// Zorgt er voor dat alert werkt in WebView
#Override
public boolean onJsAlert(WebView view, String url, String message, final JsResult result) {
new AlertDialog.Builder(myApp)
.setTitle("Title here")
.setMessage(message)
.setPositiveButton(android.R.string.ok,
new AlertDialog.OnClickListener()
{
public void onClick(DialogInterface dialog, int which)
{
result.confirm();
}
})
.setCancelable(false)
.create()
.show();
return true;
}
}
// Alle links worden in webview geladen
private class HelloWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
// Geeft de back knop een goback actie
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {
mWebView.goBack();
return true;
}
if (keyCode == KeyEvent.KEYCODE_MENU) {
return true;
}
return super.onKeyDown(keyCode, event);
}
}
There is no direct answer to ur question. Have a look at Figure 1. The activity lifecycle
The concept says that when screen is locked your activity is no longer visible (onPause()). When screen is turned back on onResume() is called. You can use these methods to get the desired results.
Generally, I initialize everything onCreate() of the activity and refresh the values if any onResume().
Hope that helps.
After so many hours, days, weeks and even months trying to solve this problem I finally found it.
If you look at the code I added in my first post you can see the follow two lines of code:
super.init();
super.clearCache();
These two little guys crashed my application over and over and now after I look at it it's no wonder they did. Added these because I was first working with PhoneGap and followed some tutorial somewhere that told me to add these lines. After removing these two lines again my application worked as usual again. Only removing one line didn't work either.
Sometimes programming can really be a hell.

Categories

Resources