Android + PhoneGap URL capture (iOS equivalent shouldStartLoadWithRequest)

My PhoneGap wrapped, locally hosted Sencha Touch application makes some false URL callbacks to communicate with the native shell. (i.e. app_callback://do_function_a).

In iOS, I implement the following

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType;

Check the urls app_callback://, call the native function and return NO (to stop the actual navigation).

Is there an equivalent in Android that I can implement?

Thanks in advance!

+5
source share
5 answers
@Override
public void onCreate(Bundle savedInstanceState) {
    super.setBooleanProperty("showTitle", true);
    super.onCreate(savedInstanceState);

    //creates super.appView and calls setContentView(root) in DroidGap.java
    init();

    this.appView.clearCache(true);
    this.appView.clearHistory();  
    this.appView.setWebViewClient(new CordovaWebViewClient(this, this.appView) {

        @Override
        public WebResourceResponse shouldInterceptRequest(WebView view, String url) {

            Log.d("DEBUG", "Should intercept request" +url);
            //Implement your code
            return super.shouldInterceptRequest(view, url);
        }

        @Override
        public void onLoadResource(WebView view, String url) {
            Log.d("DEBUG", "onLoadResource" +url);
            //Implement your code
            super.onLoadResource(view, url);
        }



        @Override
        public void onPageFinished(WebView view, String url) {
            Log.d("DEBUG", "On page finished "+url);
            //Implement your code
            super.onPageFinished(view, url);
        }


        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {

            Log.d("DEBUG", "should override url loading "+url);
            //Implement your code
            return super.shouldOverrideUrlLoading(view, url);


        }

    });super.loadUrl("file:///android_asset/www/index.html");}

This is for API versions 9-17. It is also important to add onLoadResource

+10
source
public class MainActivity extends DroidGap {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.setIntegerProperty("loadUrlTimeoutValue", 60000);

        this.init();

        this.appView.clearCache(true);
        this.appView.clearHistory();  

        this.appView.setWebViewClient(new CordovaWebViewClient(this, this.appView) {

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {

                if(url.contains("app://")) {
                     url = url.replace("app://", "");
                     Log.d("DEBUG", url);

                     // DO STUFF

                     return true;
                } else {
                 //view.loadUrl(url);
                 return super.shouldOverrideUrlLoading(view, url);
                }

            }

        });

        super.loadUrl("file:///android_asset/www/index.html");
    }

}
+6
source

, shouldOverrideUrlLoading WebViewClient.

+1

, "URL- Android-", , , . , URL-, "tel: 01234567890". iOS mustStartLoadWithRequest, . Android , , PhoneGap .

config.xml , , URL . , URL, . , JSON yourdomain.com, :

<access origin="http://www.yourdomain.com/*" />
<access origin="https://www.yourdomain.com/*" />
<access origin="tel:*" launch-external="yes" />
<access origin="http:*" launch-external="yes" />
<access origin="https:*" launch-external="yes" />

URL- WebView, , . , URL- Intent.

, - , , CordovaUriHelper.java, shouldOverrideUrlLoading. , , - .

+1

shouldOverrideUrl . , android SDK >= 11 shouldInterceptRequest.

webview shouldinterceptrequest

0

All Articles