category.DEFAULT 意图过滤器如何工作?

How does the category.DEFAULT intent filter work?

我正在制作一个音乐播放器应用程序,我希望它可以选择成为默认音乐应用程序。我一直在查看此处的文档,但没能理解它。

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

我收集到我需要使用 android.intent.category.DEFAULT 但在那之后...我卡住了。

我的问题是:如果有人 select 编辑了一个 mp3 文件,而我的应用程序是默认应用程序并打开了...我在哪里/如何 select 哪个 Activity/Service和打开文件的方法?我在哪里获取用户 select 编辑的文件的文件路径?

如果用户单击 .mp3 文件,您希望启动您的应用程序以播放该 .mp3 文件

您必须在您的 manifest 文件中放置一个 intent 过滤器,它将具有此 intent

android.intent.category.APP_MUSIC

同样请阅读Documentation

我想通了:-)

安卓清单:

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

主要活动:

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