BroadCastReceiver无法获取有关当前歌曲播放的信息

时间:2019-04-21 16:10:10

标签: java android broadcastreceiver

因此,我正在尝试在用户的电话上获取当前正在播放的艺术家和歌曲名称,但是当我打开音乐时,接收器不会做出任何反应。当我尝试从“当前”键下的sharedPreferenses获取值时,总是有默认值。使用标准音乐播放器。

这是我的接收器

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;

public class BroadCastReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        String cmd = intent.getStringExtra("command");
        String artist = intent.getStringExtra("artist");
        String album = intent.getStringExtra("album");
        String track = intent.getStringExtra("track");
        SharedPreferences ss = context.getSharedPreferences("current", Context.MODE_PRIVATE);
        String hs = track+"\n"+artist;
        SharedPreferences.Editor edit = ss.edit();
        edit.clear();
        edit.putString("current",hs);
        edit.commit();
    }
}

我的清单

<receiver
            android:name=".BroadCastReceiver"
            android:enabled="true"
            android:exported="true">
            <intent-filter>
                <action android:name="com.android.music.metachanged"/>
                <action android:name="com.android.music.playstatechanged"/>
                <action android:name="com.android.music.playbackcomplete"/>
            </intent-filter>
        </receiver>

0 个答案:

没有答案