1

I have an Android project in which I use a service in order, I'm currently using this service to watch the MediaPlayer and events go through the BR.

package com.uk.jacob.groovebuddy;

import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.IBinder;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;

public class MediaSyncService extends Service {

@Override
public void onCreate() {
    super.onCreate();

    IntentFilter iF = new IntentFilter();

    iF.addAction("com.android.music.metachanged");

    iF.addAction("com.htc.music.metachanged");

    iF.addAction("fm.last.android.metachanged");
    iF.addAction("com.sec.android.app.music.metachanged");
    iF.addAction("com.nullsoft.winamp.metachanged");
    iF.addAction("com.amazon.mp3.metachanged");
    iF.addAction("com.miui.player.metachanged");
    iF.addAction("com.real.IMP.metachanged");
    iF.addAction("com.sonyericsson.music.metachanged");
    iF.addAction("com.rdio.android.metachanged");
    iF.addAction("com.samsung.sec.android.MusicPlayer.metachanged");
    iF.addAction("com.andrew.apollo.metachanged");

    registerReceiver(mReceiver, iF);
}

private BroadcastReceiver mReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        if(intent.getAction() != null) {
            String track = intent.getStringExtra("track");
            String album = intent.getStringExtra("album");
            String artist = intent.getStringExtra("artist");

            Log.d("Test", "Your listening to " + track + " on the album " + album + " by the artist " + artist);
        }
    }
};

@Override
public void onStart(Intent intent, int startId) {
    super.onStart(intent, startId);
}

@Override
public void onDestroy() {
    super.onDestroy();
    Toast.makeText(this, "Service destroyed...", Toast.LENGTH_LONG).show();
}

@Override
public IBinder onBind(Intent intent) {
    return null;
}

}

The broadcast receiver is being triggered twice and as such I see the output

Your listening to After Midnight on the album Neighborhoods (Deluxe Explicit Version) by the artist blink-182
Your listening to After Midnight on the album Neighborhoods (Deluxe Explicit Version) by the artist blink-182

As you can see I am registering the BR only once.

Jacob Clark
  • 3,317
  • 5
  • 32
  • 61
  • May be do you have two different actions? Try print to log intent.getAction() – Puzirki Jul 27 '15 at 18:54
  • @Roman com.android.music.metachanged is the response from getAction() – Jacob Clark Jul 27 '15 at 18:58
  • @JacobClark Perhaps to simplify the question you may have written the code all at once, but its better if you split it in between each file. Also, you might want to post the manifest, so that we can be assured that you dont have two rules for the same event. As mimmo wrotte, LOG the full operation, the registering/unregistering, the received broadcast etc. then post the results in your question – Bonatti Jul 28 '15 at 12:13

0 Answers0