0

I have a webView in my application. The webView initially opens an HTML page I created with a few buttons on it. The buttons do various tasks (I'm trying to learn how to use webViews) like: 1)show android toast, 2)show javascript alert, 3)vibrate phone, 4)show current geolocation, 5)open google maps in webView.

It's this last one that's not working. I've searched this site and many others, but haven't found a solution that works.

I'm fairly certain I have all the manifest file 'stuff' correct (everything else is working, including the initial internet based web page). I'm happy to post that too though if you think it's needed.

My activity_main.xml file has never given me issues, but again, I'm happy to post that if you think it'll help.

Here are snippets from my code:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    myWebView = (WebView) findViewById(R.id.webView);
    myWebView.loadUrl(url); // this one works
    myWebView.setWebViewClient(new MyWebViewClient());
    WebSettings webSettings = myWebView.getSettings();
    webSettings.setBuiltInZoomControls(true);
    webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
    webSettings.setJavaScriptEnabled(true);
    webSettings.setGeolocationEnabled(true);
...
}

That all works and again the other html buttons work. Then there's this:

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();
    }

    @JavascriptInterface
    public void vibrateDevice() {
        Vibrator mVibrator  = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
        mVibrator.vibrate(500);
    }
    @JavascriptInterface
    public void showMapCurPos() {
        new Handler().post(new Runnable() {
            String url2 = "http://www.google.com/maps/@53.001153,-95.0752916,15z";
            @Override
            public void run() {
                myWebView.clearCache(true);//Here you call the methond in UI thread
                myWebView.loadUrl(url2);
            }
        });
    }
}

Everything works there, except for showMapCurPos(). I've already tried just a simple myWebView.loadUrl(...) along with several attempts at intents. Depending on what I've tried, sometimes that method crashes the app, but most of the time the new URL just doesn't get displayed and it stays on the original URL.

Please forgive me if this is a repeat question, but I really have searched this site and many others for an answer (probably for a solid 8 hours or so). I've tried many of those answers to no avail. I'm guessing it's something simple and has to do with my lack of understanding of webViews or activities or intents or all three or maybe even my newness to android development.

  • But where calling `addJavascriptInterface ` ? – ρяσѕρєя K Jan 02 '16 at 16:34
  • It was down later in the code (after the ... in the first block). I had the reader of my question assume that since the other javascript calls worked that I had included that correctly. Sorry for the confusion. – leforgedroid Jan 05 '16 at 00:50

1 Answers1

0

I can't write a comment yet, so I'm writing this as an answer.

You could try this answer:

myWebView.post(new Runnable() {...});

instead of using new Handler

Community
  • 1
  • 1
MoQ93
  • 341
  • 2
  • 12
  • That gets me close. Unfortunately, it's not opening in my webView, but instead is asking me if I want to open with Maps, Chrome, etc... Thank you so much for your help. – leforgedroid Jan 03 '16 at 04:22
  • I just tried it with the same configuration as yours but I used `myWebView.setWebViewClient(new WebViewClient())` instead of `MyWebViewClient` and it does work in the web view without asking. It would be nice to mark the answer as accepted, if it solves your problem ;) – MoQ93 Jan 03 '16 at 20:26
  • THANK YOU! You are awesome! I'm going to have to figure out WHY that worked, but that'll be the fun part. :) Answer accepted... gladly. – leforgedroid Jan 04 '16 at 02:23