Search code examples
androidandroid-intentmp3defaultintentfilter

How does the category.DEFAULT intent filter work?


I'm making a music player app and I'd like it to have the option of becoming the default music app. I've been reviewing the documentation here but haven't had much luck understanding it.

http://developer.android.com/guide/components/intents-filters.html

I've gathered I need to use android.intent.category.DEFAULT but after that... I'm stuck.

My question is this: If someone selected an mp3 file, and my app was the default app and opened... Where / how do I select which Activity/Service and method to open the file with? And where wou7ld I get the file path for the file that the user selected?


Solution

  • I figured it out :-)

    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...       
        }