问题
I am creating an app for Android devices using HTML and JavaScript. I am using Crosswalk (15.44.384.12) to bundle this into an Android application, which pretty much creates an android app with a web browser built in, to run my application.
I have everything working on the Android device, but I am struggling to find out how to open a link from my app in the device's default browser using JavaScript.
If I use window.open(), it will just load within my app, which is not what I want.
I have tried using window.open('http://example.com', '_blank'), I have also tried '_system', to no avail.
回答1:
Same here. All hrefs and window.open calls are opened in the WebView.
We can use a workaround that was also possible in Cordova: to intercept URLs in native Java code.
First create a custom XWalkResourceClient to intercept your urls depending on your needs:
XWalkResourceClient myResourceClient = new XWalkResourceClient(xWalkWebView){
...
@Override
public boolean shouldOverrideUrlLoading(XWalkView view, String url) {
if(url.contains("whatever")){
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
return true;
}
return super.shouldOverrideUrlLoading(view, url);
}
};
and then in your activity, you can set that client to the XWalk view:
myXWalkWebView.setResourceClient(myResourceClient);
来源:https://stackoverflow.com/questions/33608106/open-link-in-devices-default-browser-crosswalk-android-application