将我的音乐播放器设置为默认音乐播放器

Set my music player as default music player

我正在尝试将我的应用设置为默认音乐播放器,但我发现了两个问题。

第一个是关于manifst声明的:根据Google音乐应用程序Manifest(manifest)代码放在我选择的activity 是:

    <activity
        android:name=".Dashboard"
        android:theme="@style/NoActionBar"
        android:screenOrientation="portrait"
        android:launchMode="singleTask">
        <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:host="media"/>
            <data android:mimeType="audio/*"/>
            <data android:mimeType="application/ogg"/>
            <data android:mimeType="application/x-ogg"/>
            <data android:mimeType="application/itunes"/>
        </intent-filter>
    </activity>

但是如果我尝试打开音乐文件,我的应用程序不在兼容应用程序列表中(为什么?)。所以我尝试以这种方式指定文件扩展名:

   <activity
        android:name=".Dashboard"
        android:theme="@style/NoActionBar"
        android:screenOrientation="portrait"
        android:launchMode="singleTask">
        <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="*/*" />
            <data android:pathPattern=".*\.mp3" />
            <data android:host="*" />
        </intent-filter>
    </activity>

这段代码对我有用,但我认为这不是最好的方法。 还有另一个 activity 有一些重要的内容过滤器,我不知道它是否是问题所在。它是第一个 activity(因此启动一个,登录 activity)。他的 maifest 宣言是

    <activity
        android:name=".LoginActivity"
        android:label="@string/app_name"
        android:theme="@style/NoActionBar"
        android:screenOrientation="portrait">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <action android:name="android.intent.action.MUSIC_PLAYER" />
            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.LAUNCHER" />
            <category android:name="android.intent.category.APP_MUSIC" />
            <action android:name="android.intent.action.VIEW" />
        </intent-filter>
    </activity>

此刻我可以将我的应用程序设置为启动 .mp3 文件时的默认应用程序,但是这里第二个问题: 当我的应用程序在后台运行时,我无法从 Intent 拦截 Uri。我试图将 getIntent().getData() 放在 OnCreate, OnResume and OnStart 上,但是当音乐文件被点击并且我的应用程序在后台运行时,数据始终是 null。当我的应用程序不在后台时它是非空的。 Where/What有什么办法解决吗?

我现在可以回答我自己的问题了。感谢 CommonsWare 的评论和他对另一个问题的旧回答:

关于清单,这几行对我很有用,只需将我的实际意图过滤器更改为:

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

关于后台不拦截数据,我是这样解决的:

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
    }
    return false;           //has not intent to intercept
}

仍然感谢@CommonsWare。