开发者

How to prevent "complete action using" dialogbox in android application

开发者 https://www.devze.com 2023-03-25 03:31 出处:网络
I am playing YouTube videos in android 开发者_开发知识库application using code startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(youtube_video_url)));

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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号