BroadcastReceiver触发两次

时间:2015-07-27 18:52:09

标签: java android

我有一个Android项目,我按顺序使用服务,我目前正在使用此服务观看MediaPlayer和事件通过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;
}

}

广播接收器被触发两次,因此我看到输出

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

正如您所看到的,我只注册了一次BR。

0 个答案:

没有答案