Android webview location detect javascript - javascript

I have a web page in which location is getting detected in web browser successfully. And based on location my results are filtered out. I am running same website url in a webview of android app. But in android app that location detection javascript is not working. Any suggestions what to do here?
Thanks in advance.

Doing the above still didn't work for me, but adding some additional code did - see Android Webview Geolocation
I added the following to my webview object
// enable geolocationEnabled
webview.getSettings().setGeolocationEnabled(true);
// enable JavaScriptCanOpenWindowsAutomatically, not sure if this is needed
webview.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
// set setWebChromeClient
webview.setWebChromeClient(new WebChromeClient() {
public void onGeolocationPermissionsShowPrompt(String origin, android.webkit.GeolocationPermissions.Callback callback) {
callback.invoke(origin, true, false);
}
});
For some reason android.webkit.GeolocationPermissions.Callback had to be a fully qualify className, after adding it as an import it was still not recognized by the Eclipse IDE.

You should enable javascript for webview.
To do that,
wv = (WebView) findViewById(R.id.web_view);
wv.getSettings().setJavascriptEnabled(true);

Just one question: Does the location detection JS work in the Android Browser (not in the Android WebView)?
One thing to try is add the following to your manifest file:
<manifest ... >
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
...
</manifest>
The second is to setJavaScriptEnabled(true) in your webview. I just noticed someone answered this below.
Read more here: http://developer.android.com/guide/topics/location/obtaining-user-location.html

Related

Android WebView push notification?

I need to send a notification (not necessarily a push notification) through an android webview. I saw that the Notification API was not compatible with Android Webview on MDN. The other APIs I saw seemed to be based off of window.notification.
Does anyone know of any API or JS that sends a notification through an android webview?
I saw this post from 6 months ago with essentially no activity except a vague mention of firebase. Would that be helpful?
Thanks for your answers!
I don't have enough "reputation" to post a comment but could that be useful to you?
Android Push Notification with WebView?
https://github.com/ashraf-alsamman/android-webview-app-with-push-notification
https://medium.com/shibinco/creating-a-webview-android-app-with-push-notification-7fd48541a913
Hopefuly you can make it work from one of those example
Read the documentation entry for Binding JavaScript code to Android code.
This allows you to use javascript to trigger the execution of android code.
First you have to register the Javascript Interface on android, so that you can trigger android code from javascript.
JAVA
WebView webView = (WebView) findViewById(R.id.webview);
webView.addJavascriptInterface(new WebAppInterface(this), "Android");
And define a method which does your action if the javascript is called. In this example show a toast.
public class WebAppInterface {
Context mContext;
/** Instantiate the interface and set the context */
WebAppInterface(Context c) {
mContext = c;
}
/** Show a toast from the web page */
#JavascriptInterface
public void showToast(String toast) {
Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
}
}
↑ You need to change that part to show your push notification instead. ↑
Then you can trigger the android code from javascript like this:
<input type="button" value="Say hello" onClick="showAndroidToast('Hello Android!')" />
<script type="text/javascript">
function showAndroidToast(toast) {
Android.showToast(toast);
}
</script>
I did not tried it myself. But I would try to create a javascript method which makes an ajax request to a server in certain intervals and checks if there are new notifications to send and if true then call the android code to show the message.
However, you will have to make sure to only show the notification once somehow... maybe set a cookie containing the notification ID and set it to true, so that the android code is not getting triggered again.
You would need to provide the notifications for example as a .json file in JSON format. You can upload that .json file to your webserver somewhere. Then pass the content to android and parse it accordingly.
One Simple workaround use JSInterafce - communicate webview to native.tutorial
In that JSInterface pass, the desired parameter for notification and then use android system notification API to generate the notification.

Android Webview can not redirect to a website directly and via getElementById().click()

I'm facing a problem about http redirection. A website that I need to redirect to doesn't allow me to redirect to it. I have to use javascript:getElementById('someid').click()to redirect to it.
The problem is it only works with Web Browser, in Android webview it doesn't work with javascript code such as
javascript:getElementById('someid').click()
I don't know why, can anyone please explain and help me out.
Thank you very much.
If you want to use Javascript, you will need to do something along these lines instead of getElementById('someid').click()
window.location = "http://www.yoururl.com";
You must enable javascript in webview, by default as I know it's off ))
mWebView.getSettings().setJavaScriptEnabled(true);
JavaScript is disabled in a WebView by default. You should enable JavaScript for working with.
WebView myWebView = (WebView) findViewById(R.id.webview);
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);

AngularJS dont render html - Android 19 - 4.4.4 and Safari 8.0.5

I am developing an AngularJS app displayed in a Webview - Android.
It was working ok until yesterday. Angular sometimes is not rendering the DOM.
I was testing the app in multiple Android versions: API 19, 20... without results.
This is the code that initialize the webview.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
mWebView = (WebView) findViewById(R.id.wv);
// Clear cache
mWebView.clearCache(true);
// Enable Javascript
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
mWebView.setWebViewClient(new WebViewClient());
mWebView.loadUrl("http://demo.example.com");
}
If I run this webpage in a regular Chrome Tablet is working ok.
but the issue comes when it is desplayed into the webview.
I was searching help for a while.
Thank you so much.
UPDATED:
The same error happens on Mac OS Maverick Safari 8.0.5 and iPad too.
I think this is because a new update released both Apple and Google recently, that blocks AngularJS in some way...
SOLUTION
To identify the issue, I commented parts of my code until I see these very simple calls:
$("#modal-change").show();
$('#modal-confirm').modal('show');
In some way one of those jQuery/Bootstrap functions starts a conflict between AngularJS and jQuery and stop render the DOM when Angular need it...
Just replace it with my own functions and that's it.
I don't know if this is a perfect solution but it works for me and I post it if help someone.
Solved for me.

How to navigate to phone setting screen from a webapp

I am developing a web app using sencha touch for all mobile devices. My application uses GPS feature. I would like to know how can I navigate to the native setting screen of the phone from my web app for the user to switch ON the GPS feature.
If the GPS is not ON, it will prompt user a popup which will take the user to location settings screen to turn ON the GPS.
Is this possible ?
Please let know. I have seen this feature in many apps, even in google maps.
It is not cording practice to do that. You can simply state the steps to be followed.
IF you are running webapp on Phonegap or Android web view, then you can do like this.
Read this article how to bind Javascript function to Native app.
http://developer.android.com/guide/webapps/webview.html#BindingJavaScript
Android Code
public class WebAppInterface {
/** Show a toast from the web page */
#JavascriptInterface
public void goToSettingPage() {
startActivityForResult(new Intent(android.provider.Settings.ACTION_SETTINGS), 0);
}
}
WebView webView = (WebView) findViewById(R.id.webview);
webView.addJavascriptInterface(new WebAppInterface(this), "Android");
JavaScript Code
<input type="button" value="Say hello" onClick="openSettingPage()" />
<script type="text/javascript">
function openSettingPage() {
Android.goToSettingPage();
}
</script>

Why is cross-domain scripting possible for local files on a mobile device?

What I did:
I created a index.html with a xss.js, which calls the jQuery.get() function. Then I've opened the index.html in a browser (Firefox, Chrome, IE and Opera) and tried to trigger the ajax request.
The Code
Here is my index.html:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>XSS</title>
<script src="libs/js/jquery-1.7.2.js" ></script>
</head>
<body>
<button id="request" >fire</button>
<script src="libs/js/xss.js" ></script>
</body>
</html>
and my xss.js:
function init()
{
$('#request').click(loadContent);
}
function loadContent()
{
$.get('http://www.example.com/', null, function(data){
alert('success');
$('body').html(data);
}, 'html');
}
init();
If I open the index.html within a browser (file:///C:/workspace/xss%20test/index.html), I get the following responses after clicking the button:
Firefox: no error code (HTTP/1.1 200 OK), but the answer is empty
IE: no answer
Chrome:
XMLHttpRequest cannot load http://www.example.com/. Origin null is not allowed by Access-Control-Allow-Origin.
Opera: no error code (HTTP/1.1 200 OK) and the complete html file as answer, but nothing will be displayed (the success callback is not being triggered)
This code will load the index.html into my Android WebView:
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView webview = (WebView) findViewById(R.id.webview);
webview.getSettings().setJavaScriptEnabled(true);
webview.setWebChromeClient(new WebChromeClient());
webview.setWebViewClient(new WebViewClient());
webview.loadUrl("file:///android_asset/www/index.html");
}
}
calls the success callback and also displays the content of www.example.com in the body of my index.html file, after the button is triggered.
(The same is possible on iPhone devices - I haven't tested this on Windows Phone devices).
tl;dr - The Question:
Why is it possible to load content from a remote server to my mobile device - isn't this a case of cross-domain scripting or am I missing something?
Due to browser security restrictions, most "Ajax" requests are subject to the same origin policy; the request can not successfully retrieve data from a different domain, subdomain, or protocol.
Also: Why does Opera receive an answer but does not display anything?
Thanks in advance.
Actually, your code fails on mobile browsers including ICS and Chrome on Android as well as Safari on iPhone. However, what you have shown is not loading the html file in a browser - it is loading it into a WebView - a different animal altogether.
A WebView or Webkit is just a UI widget that implements browser-like functionality. They are not browsers. They do not provide stuff like the usual browser chrome and they have very liberal security models by default compared to browsers. Though, you can add code to implement things like same-origin-policy etc. if you want.
It's not only on mobile devices. Try creating a Webkit app on the desktop and you'll see the same thing.
I believe the reason for this is that WebViews and Webkits are assumed to be used to display content that you control 100%. Unlike browsers where users can enter any URL in the address bar. Therefore it's up to you to vet weather the things you're loading are safe or not.

Categories

Resources