如何防止“完整操作使用” Android应用程序中的对话框

发布于 2024-11-27 13:44:24 字数 306 浏览 0 评论 0原文

我正在使用代码在 Android 应用程序中播放 YouTube 视频
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(youtube_video_url)));
当我尝试在设备上播放视频时,它会弹出一个对话框“使用完整操作”,其中包含 Internet 和 YouTube 选项。我怎样才能防止这个弹出窗口并直接在 YouTube 播放器中打开 YouTube 视频。此外,有没有办法像 Engadget 那样直接在视频播放器中播放 YouTube 视频(避免使用 YouTube 播放器)。

I am playing YouTube videos in android application using code
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(youtube_video_url)));
when i try to play video on device, it pop up a dialog box "complete action using" with options Internet and YouTube. how can i prevent this pop up and open the YouTube video directly in YouTube player. Further more, is there any way to play YouTube videos directly in video player (by avoiding the YouTube player ) like Engadget do.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

戒ㄋ 2024-12-04 13:44:24

这是您有兴趣调用的 WatchActivity

<activity android:name="WatchActivity" android:screenOrientation="sensor" android:configChanges="keyboardHidden|orientation" android:allowTaskReparenting="true">
    <intent-filter>
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />
        <data android:scheme="http" android:host="www.youtube.com" android:pathPrefix="/watch" />
    </intent-filter>
    <intent-filter>
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />
        <data android:scheme="http" android:host="www.youtube.com" android:pathPrefix="/v/" />
    </intent-filter>
    <intent-filter>
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />
        <data android:scheme="http" android:host="www.youtube.com" android:pathPrefix="/e/" />
    </intent-filter>
    <intent-filter>
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />
        <data android:scheme="vnd.youtube" />
    </intent-filter>
</activity>

不是默认活动,也就是说,为了直接调用它,您必须尝试以下代码

String youtubePackage = "com.google.android.youtube";
ArrayList<IntentFilter> intentFilters = new ArrayList<IntentFilter>();
/*
 * Filter to match
    <intent-filter>
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />
        <data android:scheme="http" android:host="www.youtube.com" android:pathPrefix="/watch" />
    </intent-filter>
 */
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_VIEW);
filter.addCategory(Intent.CATEGORY_DEFAULT);
filter.addCategory(Intent.CATEGORY_BROWSABLE);
//filter.addDataScheme("http");
//filter.addDataAuthority("www.youtube.com", null);//port null will allow any port
//filter.addDataPath("/watch", PatternMatcher.PATTERN_PREFIX);
intentFilters.add(filter);
ArrayList<ComponentName> outActivities = new ArrayList<ComponentName>();
getPackageManager().getPreferredActivities(intentFilters, outActivities, youtubePackage);

在最终调用之后, outActivities 变量将保存匹配的活动(很可能只有一个)。事实上,它并没有给我带来任何回报。

为了确保Activity与您的Intent相匹配,您可以查询包

Intent videoIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://m.youtube.com/watch?v=EUXnJraKM3k"));         
List<ResolveInfo> matchingActivities = getPackageManager().queryIntentActivities(videoIntent, PackageManager.MATCH_DEFAULT_ONLY);

或者放弃所有冗长的过程,只需使用

Intent videoIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("vnd.youtube:<video id>"));
startActivity(videoIntent);

vnd.youtube即可正如您所看到的,WatchActivity 过滤器接受的方案。

Here's the WatchActivity which you are interested to invoke

<activity android:name="WatchActivity" android:screenOrientation="sensor" android:configChanges="keyboardHidden|orientation" android:allowTaskReparenting="true">
    <intent-filter>
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />
        <data android:scheme="http" android:host="www.youtube.com" android:pathPrefix="/watch" />
    </intent-filter>
    <intent-filter>
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />
        <data android:scheme="http" android:host="www.youtube.com" android:pathPrefix="/v/" />
    </intent-filter>
    <intent-filter>
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />
        <data android:scheme="http" android:host="www.youtube.com" android:pathPrefix="/e/" />
    </intent-filter>
    <intent-filter>
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />
        <data android:scheme="vnd.youtube" />
    </intent-filter>
</activity>

It's not the default activity, that is in order to call it directly, you'll have to try the following code

String youtubePackage = "com.google.android.youtube";
ArrayList<IntentFilter> intentFilters = new ArrayList<IntentFilter>();
/*
 * Filter to match
    <intent-filter>
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />
        <data android:scheme="http" android:host="www.youtube.com" android:pathPrefix="/watch" />
    </intent-filter>
 */
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_VIEW);
filter.addCategory(Intent.CATEGORY_DEFAULT);
filter.addCategory(Intent.CATEGORY_BROWSABLE);
//filter.addDataScheme("http");
//filter.addDataAuthority("www.youtube.com", null);//port null will allow any port
//filter.addDataPath("/watch", PatternMatcher.PATTERN_PREFIX);
intentFilters.add(filter);
ArrayList<ComponentName> outActivities = new ArrayList<ComponentName>();
getPackageManager().getPreferredActivities(intentFilters, outActivities, youtubePackage);

After the final call, the outActivities variable will hold matching activities (most likely just one). As a matter of fact, it doesn't return anything for me.

To make sure what Activitys match your Intent you can query the package

Intent videoIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://m.youtube.com/watch?v=EUXnJraKM3k"));         
List<ResolveInfo> matchingActivities = getPackageManager().queryIntentActivities(videoIntent, PackageManager.MATCH_DEFAULT_ONLY);

Or ditch all that long procedure and just go with

Intent videoIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("vnd.youtube:<video id>"));
startActivity(videoIntent);

vnd.youtube is a scheme accepted by the WatchActivity filter as you see.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文