11

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 !

elgui
  • 3,303
  • 4
  • 28
  • 37
  • hey this answer worked well for me. 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?? please help! –  Nov 01 '16 at 13:26

4 Answers4

15

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>
CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Two questions... What if we only wanted to be the default for mp3 files... not all audio files... and question two, how do we then get the file info / path in the Activity? – DeNitE Appz Nov 17 '15 at 15:28
  • @DeNitE: "What if we only wanted to be the default for mp3 files" -- you are not going to be the default for anything, unless the user chooses to make you the default. The user can choose to make you the default for any `Intent` structure that you elect to support via your set of `` elements. "how do we then get the file info / path in the Activity?" -- call `getIntent()` to retrieve the `Intent` that was used to start your activity. That won't be a file, usually (`content:` scheme is more likely). – CommonsWare Nov 17 '15 at 15:29
  • ok, that makes sense... let me rephrase... what we only wanted to supply support for only mp3 files, not all audio files? – DeNitE Appz Nov 17 '15 at 15:31
  • @DeNitE: Then choose the MIME type(s) that you want, and only use those in the `` elements. I think `audio/mp3` is the MIME type for MP3, but you would need to validate that. – CommonsWare Nov 17 '15 at 15:32
  • hey this worked well for me. 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?? please help! @CommonsWare –  Nov 01 '16 at 13:18
  • To whoever sees this, you would need to get the music file from the Intent in order to play it. – John Glen Aug 17 '21 at 16:54
4

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...       
    }
DeNitE Appz
  • 1,003
  • 2
  • 12
  • 18
0

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>
Paul Roub
  • 36,322
  • 27
  • 84
  • 93
neosonne
  • 136
  • 1
  • 4
0

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
}