phonegap function not working in webview - android

I am trying to build a simple app using webview but unable to get phonegap functions working when I use the webview class. If I use "super" then the phonegap function works fine. Excuse my odd vocabulary.
///////////////// JAVA //////////////////////////
public class MainStart extends DroidGap {
HTML5WebView mWebView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
CookieSyncManager.createInstance(this);
CookieSyncManager.getInstance().sync();
mWebView = new HTML5WebView(this);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.addJavascriptInterface(new JavaScriptInterface(this), "MyAndroid");
mWebView.getSettings().setPluginsEnabled(true);
mWebView.loadUrl("file:///android_asset/www/index.html"); // phonegap function does not work
super.loadUrl("file:///android_asset/www/index.html"); // phonegap function works here
}
}
///////////////// JAVASCRIPT //////////////////////////
function onDeviceReady(){
navigator.notification.alert("PhoneGap is working");
}

the WebView that PhoneGap sets up include a multitude of features which you are not even referencing in your own WebView - like the Javascript bridge - without it there's no way you can call Native code from within your html page!
may I ask why exactly are you trying to re-create your own WebView, instead of just using the standard supplied one?

Related

Payment processing infinitely in WebView

I have a testing website where PayUMoney payment processing in test mode is working perfect.
I used the same url in webview and also enabled javascript, but the payment is processing infinitely(Loading forever).
Please help me figure out this. Thanks!
public class MainActivity extends AppCompatActivity {
private WebView webview;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webview =(WebView)findViewById(R.id.webView);
webview.setWebViewClient(new WebViewClient());
webview.getSettings().setJavaScriptEnabled(true);
webview.getSettings().setDomStorageEnabled(true);
webview.setOverScrollMode(WebView.OVER_SCROLL_NEVER);
webview.loadUrl("https://gatewaypayment.000webhostapp.com");
}
#Override
// This method is used to detect back button
public void onBackPressed() {
if(webview.canGoBack()) {
webview.goBack();
} else {
// Let the system handle the back button
super.onBackPressed();
}
}
}
I faced a similar situation but finally figured out that the reason behind the long loading is due to the test details getting updated frequently, or the test environment that PayUMoney provides itself is down. Also make sure of the following:
To test the Bolt Checkout in the test mode, you need to change the JavaScript libraries used as-well as use Test Mode key and salt for hash generation.
So while integrating in Test Mode, include the script as shown below in the head section of your payment request page.
<script id="bolt" src="https://sboxcheckout-static.citruspay.com/bolt/run/bolt.min.js" bolt-
color="<color-code>"
bolt-logo="<image path>">
</script>
Once your integration is done and tested on the sandbox, replace the sandbox JavaScript library with the production library provided by PayU.
<script id="bolt" src="https://checkout-static.citruspay.com/bolt/run/bolt.min.js" bolt-color="<color-
code>"
bolt-logo="<image path>">
</script>

Android emulator can't get api data threw website

I have frontend and backend ready and working on localhost. I am displaying the frontend website threw the android emulator:
public class MainActivity extends AppCompatActivity {
private WebView webview;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webview =(WebView)findViewById(R.id.webView);
webview.setWebViewClient(new WebViewClient());
webview.getSettings().setJavaScriptEnabled(true);
webview.getSettings().setDomStorageEnabled(true);
webview.setOverScrollMode(WebView.OVER_SCROLL_NEVER);
webview.loadUrl("http://10.0.2.2:4200/mobile");
}
}
The problem is that emulator can't get the data from database or send request to api to recieve the data so that it's not displayed. How it looks on a website:
Once again the problem is straight with emulator. I tried building the frontend in production mode with no success. This issue looks strange to me because I am just "retranslating" the working website. Maybe I should declare a special permission or sth like that.
Android Studio Error:

navigator.share is not working in a WebView

Is there a reason why the navigator.share() JavaScript function is not working inside a WebView of my Android app? When accessing the same URL through the web browser, the native Android share dialog pops up, but not when accessing the URL from the WebView of my app.
The URL is using https.
The share action is user-triggered by an onClick.
setJavaScriptEnabled is set to true.
setDomStorageEnabled is also set to true.
Eventually, I used the following settings for my webviews. There is a bit more to it for me than just the navigator.share, but what works for me might work for others. Worth a try.
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setDomStorageEnabled(true);
mWebView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
mWebView.getSettings().setAppCacheEnabled(false);
mWebView.getSettings().setAllowFileAccess(true);
mWebView.getSettings().setAllowContentAccess(true);
mWebView.getSettings().setMediaPlaybackRequiresUserGesture(false);
mWebView.addJavascriptInterface(new JavaScriptShareInterface(), "AndroidShareHandler");
The AndroidShareHandler will become a global JavaScript function which will be available in the webview and can be triggered by a button click for example:
Java:
package com.your.package;
import android.webkit.JavascriptInterface;
public class JavaScriptShareInterface {
#JavascriptInterface
public void share(String url) {
// your share action
}
}
JavaScript:
shareButton.addEventListener('click', () => {
window.AndroidShareHandler.share('https://stackoverflow.com');
});

How do you get location data through HTML5 embedded in an Android app?

I'm working on an Android app where, basically, different web pages are embedded in fragments and a list fragment is used to switch between pages. I'm able to get HTML5 location data working through a browser but not when the page is embedded in the app.
Forgetting fragments, I was able to and I can get JavaScript working in the WebView. But even with WebViewClient I can't figure out what to do for getting the geolocation data working. Can someone point me in the right direction?
You have first to Enable Javascript and GeoLocation in your WebView
WebView mWebView;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mWebView = (WebView) findViewById(R.id.webView1);
// Brower niceties -- pinch / zoom, follow links in place
mWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
mWebView.getSettings().setBuiltInZoomControls(true);
mWebView.setWebViewClient(new GeoWebViewClient());
// Below required for geolocation
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setGeolocationEnabled(true);
mWebView.setWebChromeClient(new GeoWebChromeClient());
}
then you can get location data using navigator..
function getLocation() {
navigator.geolocation.getCurrentPosition(displayLocation, handleError);
}
Take a look at this Post

how to run local web application in android webview

can anybody tell how to run the local webapplication using android webview
I want to run my own webpages in android using web view
Thanks
I'm guessing you want something like this:
private WebView mWebView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.yourLayoutWithAWebView);
mWebView = (WebView)findViewById(R.id.yourWebViewIdFromTheLayout);
mWebView.getSettings().setJavaScriptEnabled(true); // Enable JavaScript
mWebView.loadData(yourHtmlData, "text/html", "utf-8");
}
Hopefully, that at least points you in the right direction.
Also, I'd recommend reading the documentation on the WebView, it's pretty thorough.
Just run your application on the emulator, as you normally do (and be sure to have your computer connected to internet).

Categories

Resources