11

我想在我尝试打开音频文件(即从文件浏览器或 gmail 附件)时弹出的播放器列表中看到我的应用程序(“使用...继续操作”)。以下是我为 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>

感谢您的帮助 !

4

4 回答 4

14

您总是需要 a <category>on an ,因为used with<activity> <intent-filter>上总是至少有一个类别。IntentstartActivity()

以下是AOSP 音乐应用程序的用途

        <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>
于 2013-04-02T15:58:25.130 回答
4

这对我有用:

安卓清单:

 <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...       
    }
于 2015-11-18T15:20:01.060 回答
0

这对我有用

<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>
于 2017-08-25T14:45:19.870 回答
0

CommonsWare 关于 Manifest 的回答很好,而且很有效。

“但是当我选择歌曲并从列表中选择我的应用程序时,它成功打开了我的应用程序但没有播放所选歌曲。该怎么办??”

如果您的应用不在后台,您可以在 OnCreate 活动中执行

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

如果您的应用程序在后台,您必须拦截的 Intent 将通过您必须覆盖的 onNewIntent() 方法。

这是一个完整的代码示例:

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
}
于 2017-08-25T14:56:10.070 回答