Launch custom Android application from WebView

半城伤御伤魂 提交于 2019-12-30 03:25:06

问题


I have an HTML file which is launching an app if I open it in the Android native browser, but when I try to open the same in a WebView, it is not able to launch that application, and "Webpage not available" is shown. I think my WebView is not able to handle the scheme "my.special.scheme://" defined for the application.

I read Launching an Android Application from the Browser, but it does not cover information about launching an app from a WebView.


回答1:


I'm not sure, but I believe that WebView simply doesn't handle custom URI schemes.

The workaround is to override WebViewClient.shouldOverrideUrlLoading() and manually test if the URL uses your URI scheme, launching your app and returning true if it matches, otherwise returning false.




回答2:


It's true, links with a custom URI scheme don't load automatically launch apps from a WebView.

What you need to do is add a custom WebViewClient to your WebView:

webView.setWebViewClient(new CustomWebViewClient());

and then in the shouldOverrideUrlLoading(), have the following code:

public boolean shouldOverrideUrlLoading(final WebView webView, final String url) {

    if (url.startsWith("my.special.scheme://")) {

        final Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));

        // The following flags launch the app outside the current app
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);

        activity.startActivity(intent);

        return true;
    }  

    return false;
}



回答3:


I perform an ACTION_VIEW with the URL to make the URL open in the default browser, which will do the redirecting to the concerning app (I had to fix this concerning payments via a bank app)



来源:https://stackoverflow.com/questions/11242230/launch-custom-android-application-from-webview

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