1

I would like to receive broadcasts from music players while the app is in the background.

I found the following code here:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    IntentFilter iF = new IntentFilter();
    iF.addAction("com.android.music.metachanged");
    iF.addAction("com.android.music.playstatechanged");
    iF.addAction("com.android.music.playbackcomplete");
    iF.addAction("com.android.music.queuechanged");

    registerReceiver(mReceiver, iF);
}

private BroadcastReceiver mReceiver = new BroadcastReceiver() {

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        String cmd = intent.getStringExtra("command");
        Log.v("tag ", action + " / " + cmd);
        String artist = intent.getStringExtra("artist");
        String album = intent.getStringExtra("album");
        String track = intent.getStringExtra("track");
        Log.v("tag", artist + ":" + album + ":" + track);
        Toast.makeText(CurrentMusicTrackInfoActivity.this, track, Toast.LENGTH_SHORT).show();
    }
};

And it works, but only while the activity is running. So I tried to make a static broadcastreceiver:

<receiver
    android:name=".receiver.MusicBroadcastReceiver"
    android:exported="false">
    <intent-filter>
        <action android:name="com.android.music.metachanged" />
        <action android:name="com.android.music.playstatechanged" />
        <action android:name="com.android.music.playbackcomplete" />
        <action android:name="com.android.music.queuechanged" />
    </intent-filter>
</receiver>

The class:

public class MusicBroadcastReceiver extends BroadcastReceiver {
    public MusicBroadcastReceiver() {
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        String cmd = intent.getStringExtra("command");
        Log.v("tag ", action + " / " + cmd);
        String artist = intent.getStringExtra("artist");
        String album = intent.getStringExtra("album");
        String track = intent.getStringExtra("track");
        Log.v("tag", artist + ":" + album + ":" + track);
        Toast.makeText(context, track, Toast.LENGTH_SHORT).show();
    }
}

And it doesn't work. I started searching and I found this question, which claims that the code above should work. But it doesn't, onReceive is not called when I start playing music or skip a track. Imust be doing something very wrong, but I can't figure out what.

Any help would be appreciated. Thanks.

Community
  • 1
  • 1
Longi
  • 3,913
  • 2
  • 28
  • 38
  • Quick question, with your current code, does the onRecieve work if you open the application, go to the background and change music ? – Jayesh Elamgodil Apr 14 '15 at 19:20
  • Could you try using the complete path name for the android:name attribute – Jayesh Elamgodil Apr 14 '15 at 19:23
  • Which one? I haven't tried the dynamic one, because i assumed it would be destroyed the moment the activity is destroyed. A static one doesn't work. I tried using the full path, but it didn't make a difference. – Longi Apr 14 '15 at 19:26

2 Answers2

1

Try to set android:exported="true"

http://developer.android.com/guide/topics/manifest/receiver-element.html

android:exported
Whether or not the broadcast receiver can receive messages from sources outside its application — "true" if it can, and "false" if not. If "false", the only messages the broadcast receiver can receive are those sent by components of the same application or applications with the same user ID.

Mattia Maestrini
  • 32,270
  • 15
  • 87
  • 94
0

There is info about the usage of "com.android.music.playstatechanged" As mentioned this method is used only for the standard player.

And this type of notification isn't described in API. Possible it was removed as obsoleted.

UPD: In case we use a receiver with filter "com.android.music.playstatechanged" it receives events but only for default music player

I used another approach: just check if music is played on the timer too

AudioManager am = (AudioManager)this.getSystemService(Context.AUDIO_SERVICE);
am.isMusicActive()
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Apr 25 '22 at 13:29
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/late-answers/31618308) – Hypenate Apr 30 '22 at 13:30