Open link in device's default browser. Crosswalk android application

社会主义新天地 提交于 2019-12-10 21:10:14

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!