I'd like to see my app in the list of player ("continue action using...") that pops up when I try to open an audio file (ie. from file browser or gmail attachment). Here are the intent filters I've tried for my MainActivity:

    <intent-filter>
        <action android:name="android.intent.action.MAIN" />

        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>

    <intent-filter>
        <action android:name="android.intent.action.SEARCH" />
    </intent-filter>

    <intent-filter>
        <action android:name="android.intent.action.MUSIC_PLAYER" />
    </intent-filter>

    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.CATEGORY_APP_MUSIC" />
    </intent-filter>

    <intent-filter>
        <action android:name="android.intent.action.VIEW" />
        <data android:mimeType="audio/mpeg" />
    </intent-filter>

thanks for your help !

有帮助吗?

解决方案

You always need a <category> on an <activity> <intent-filter>, as there is always at least one category on the Intent used with startActivity().

Here is what the AOSP Music app uses:

        <intent-filter>
            <action android:name="android.intent.action.VIEW" />
            <category android:name="android.intent.category.DEFAULT" />
            <data android:scheme="file"/>
            <data android:mimeType="audio/*"/>
            <data android:mimeType="application/ogg"/>
            <data android:mimeType="application/x-ogg"/>
            <data android:mimeType="application/itunes"/>
        </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" />
            <data android:mimeType="audio/*"/>
            <data android:mimeType="application/ogg"/>
            <data android:mimeType="application/x-ogg"/>
            <data android:mimeType="application/itunes"/>
        </intent-filter>
        <intent-filter
            android:priority="-1">
            <action android:name="android.intent.action.VIEW" />
            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.BROWSABLE" />
            <data android:scheme="content" />
            <data android:mimeType="audio/*"/>
            <data android:mimeType="application/ogg"/>
            <data android:mimeType="application/x-ogg"/>
            <data android:mimeType="application/itunes"/>
        </intent-filter>

其他提示

This worked for me:

AndroidManifest:

 <intent-filter>
            <action android:name="android.intent.action.VIEW"/>
            <category android:name="android.intent.category.DEFAULT"/>
            <data android:scheme="file" android:pathPattern=".*mp3" android:mimeType="audio/mpeg" />
 </intent-filter>

MainActivity:

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {  

    if (Intent.ACTION_VIEW.equals(getIntent().getAction()))
    {
        File file = new File(getIntent().getData().getPath());

       // do what you want with the file...       
    }

This works for me

<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="content"/>
    <data android:scheme="file"/>
    <data android:mimeType="application/x-flac"/>
    <data android:mimeType="audio/*"/>
    <data android:mimeType="application/ogg"/>
    <data android:mimeType="application/x-ogg"/>
    <data android:mimeType="application/itunes"/>
</intent-filter>

The CommonsWare answer regarding Manifest is good and it works.

"But when I select song and select my app from list, it opens my app successfully but not playing that selected song. what to do for that?? "

If your app is not in background, in your activity OnCreate you can do

 Uri data = getIntent().getData()
 if (data!=null){/*use this Uri like you want*/}

In case your app is in background the Intent you have to intercept will pass through the onNewIntent() method that you have to ovveride.

Here is a complete code example:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    /*...*/
    interceptExternalIntentAndPlaySongFromUri(getIntent());
}

@Override
protected void onNewIntent(Intent intent) {
    interceptExternalIntentAndPlaySongFromUri(intent);
}

private boolean interceptExternalIntentAndPlaySongFromUri(Intent intent){
    Uri data = intent.getData();
    if (data != null && intent.getAction() != null && 
intent.getAction().equals(Intent.ACTION_VIEW)){      
        String scheme = data.getScheme();
        String filename = "";
        if ("file".equals(scheme)) {
            filename = data.getPath();
        } else {
            filename = data.toString();
        }
        player.setDataSource(filename);
        setIntent(new Intent());        //this is to remove the last intent 
//without the above line, last request is reloaded when the user open another app 
//and return in this
        return true;        //has intercepted an external intent. so you can load this uri.
    }
    return false;           //has not intent to intercept
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top