从WebView在本机Soundcloud应用程序中打开Soundcloud URL [英] Open Soundcloud URL in native Soundcloud app from WebView

查看:224
本文介绍了从WebView在本机Soundcloud应用程序中打开Soundcloud URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Android应用程序中有一个WebView,其中包含一个嵌入的Soundcloud(来自Embedly).该嵌入有两个按钮:在Soundcloud上播放"和在浏览器中收听".

I have a WebView in my Android app which contains a Soundcloud embed (from Embedly). This embed has two buttons: "Play on Soundcloud" and "Listen in browser".

在Soundcloud上播放"按钮包含格式为intent://tracks:257659076#Intent;scheme=soundcloud;package=com.soundcloud.android;end

The "Play on Soundcloud" button contains a URL in format intent://tracks:257659076#Intent;scheme=soundcloud;package=com.soundcloud.android;end

我的WebView使用自定义的WebViewClient(因为我需要截取一些其他内容的URL).

My WebView uses a custom WebViewClient (because I need to intercept some URLs for some different stuff).

protected class WebViewClient extends android.webkit.WebViewClient {
    public WebViewClient() { }

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        PackageManager packageManager = context.getPackageManager();

        // Create an Intent from the URL.
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));

        // Find out if I have any activities which will handle the URL.
        List<ResolveInfo> resolveInfoList = packageManager.queryIntentActivities(intent, 0);

        // If we have an app installed that can handle the URL, then use it.
        if (resolveInfoList != null && resolveInfoList.size() > 0) {
            Intent viewUrlIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
            context.startActivity(viewUrlIntent);
        }
        else {
            // Do something else.
        }
        return true;
    }
}

问题

单击在浏览器中收听"会播放嵌入内容中的曲目,并且效果很好.单击在Soundcloud上播放"将调用上面的WebViewClient中的shouldOverrideUrlLoading(按预期方式).但是,我找到活动的代码找不到可以处理此Soundcloud URL的任何内容.

Problem

Clicking "Listen in browser" plays the track in the embed itself and works fine. Clicking "Play on Soundcloud" will call into shouldOverrideUrlLoading in the WebViewClient above (as expected). However, my code to find a activity can't find anything that can deal with this Soundcloud URL.

如果我未在WebView上设置WebViewClient(所以它只是做自己的事情),则按需播放Soundcloud"按钮将按预期工作并启动Soundcloud应用.

If I don't set my WebViewClient on the WebView (so it just does its own thing), the "Play on Soundcloud" button will work as expected and launch the Soundcloud app.

通过解析URL以获取轨道ID,然后使用Soundcloud绝对接受的格式来构建新的URL,我设法做到了这一点(感谢的URL.

I've managed to make this do what I want it to do by parsing the URL to get the track ID, then building a new URL using a format that Soundcloud definitely accepts (thanks to this SO post). A URL in the format "soundcloud://tracks:[TRACK_ID]" will be accepted by the Soundcloud app.

我正在做整个找出哪些活动可以处理此URL"错误,或者可能是(?!)WebView使用的默认WebViewClient显式处理此问题?似乎令人难以置信.

Either I am doing the whole "find out what activities can handle this URL" thing wrong, or maybe(?!) the default WebViewClient used by the WebView handles this explicitly?! Seems implausible.

推荐答案

我只是在这里扩展临时(废话)解决方案,所以这不是一个完美的答案,但仍然可以帮助绝对需要此解决方案的人上班,也有私家车道.

I'm just extending the Temporary (crap) solution here, so this is far from a perfect answer, but might still help someone who absolutely needs to get this to work, also with private tracks.

如果轨道是公共轨道,则replace方法有效,但对于私有轨道,则该方法无效,这可能是由于意图URL中缺少秘密令牌所致.

The replace method works if the track is public, but with private tracks this does not work, probably because of the missing secret token in the intent URL.

不幸的是,嵌入播放器不包含我需要生成的所有必要URL片段,除了iframe内部,由于跨域策略我无法访问该URL.因此,除了iframe代码外,我还需要共享链接.

Unfortunately the embed player does not contain all the necessary pieces of the URL I need to generate, except inside the iframe, which I cannot access due to cross-origin policy. So in addition to the iframe code I also need the share link.

我最终要做的是确保包含HTML的页面将共享链接作为JS变量.然后,我使用Java读取该变量,并使用该URL创建一个新的Intent.之所以可行,是因为官方应用程序还注册了所有soundcloud.com URL.

What I ended up doing is making sure that the containing HTML page has the share link as a JS variable. I then read that variable using Java and create a new Intent with that URL. This works, because the official app also registers all soundcloud.com URLs.

因此,对于私人曲目,这将转到HTML页面:

So for private tracks this goes to the HTML page:

<script>var soundCloudURL = "https://soundcloud.com/my-profile/my-track/my-secret-token";</script>

然后在您的Android应用程序中,您将得到以下内容:

Then inside your Android app you would have something like this:

@Override
public boolean shouldOverrideUrlLoading (WebView view, String url) {
    if (uri.getScheme().contains("intent")) {
        openSoundCloudPlayer();
        return true;
    }
}

private void openSoundCloudPlayer() {
    appWebView.evaluateJavascript("(function() { return soundCloudUrl })();", new ValueCallback<String>() {
        @Override
        public void onReceiveValue(String soundCloudUrl) {
            // JS null is converted into a string "null", not Java null.
            if (soundCloudUrl != "null") {
                // Take out the quotes from the string
                soundCloudUrl = soundCloudUrl.replace("\"", "");

                Uri newUri = Uri.parse(soundCloudUrl);
                Intent intent = new Intent(Intent.ACTION_VIEW, newUri);
                startActivity(intent);
            }
        }
    });
}

这篇关于从WebView在本机Soundcloud应用程序中打开Soundcloud URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆