问题
My PhoneGap wrapped, locally hosted Sencha Touch app makes some fake URL callbacks to communicate with the native wrapper. (ie. app_callback://do_function_a
).
In iOS I implement the following
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType;
Check for the app_callback://
URLs, call a native function and return NO (to stop navigation actually happening).
Is there an equivalent in Android I can implement?
Thanks in advance!
回答1:
@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 Important is also add onLoadResource
回答2:
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");
}
}
回答3:
Yes, shouldOverrideUrlLoading of WebViewClient
.
回答4:
Since this is the first search result that comes up for "phonegap android intercept url" and I had the same problem, I thought I'd add a more up-to-date answer. I was looking to launch the dialer using a URL like "tel:01234567890". On iOS I used the shouldStartLoadWithRequest, which works great. On Android I needed to find an alternative, and it turns out that the latest PhoneGap has a feature for it built in.
In the config.xml file you can, and should, define access origins, to say which URL patterns are ok to work in your app. This is a security feature, but you can also specify URL patterns that will instead be launched in external applications. For example, if your app makes JSON requests to yourdomain.com then you would maybe have entries like this:
<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" />
When a URL is visited in the WebView, Cordova will firstly check if any of the internal rules match, and then check if any of the external rules match. If none of the internal rules match but one of the external rules match, the URL is launched in an Intent.
That worked great for me, but if you're needing something more customised, I found that the code to process this is in CordovaUriHelper.java, in the function shouldOverrideUrlLoading. So if you wanted to customise that a little you could do anything.
回答5:
Actually shouldOverrideUrl loading is not likely to work. Rather, for android with SDK>=11 you should use shouldInterceptRequest.
webview shouldinterceptrequest example
来源:https://stackoverflow.com/questions/12601491/android-phonegap-intercept-url-equivalent-of-ios-shouldstartloadwithrequest