Service发送广播到Activity没有接受到数据?

Service:

public class MusicPlayService extends Service {

    private static MusicPlayer mPlayer = null;

    private int msg;

    private int currentTime;//当前播放进度
    private long duration;//播放长度

    private MusicContentListBean mSonginfo;

    private MusicInfoBean musicInfoBean;
    /**
     * handler用来接收消息,来发送广播更新播放时间
     */
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            if (msg.what == 1) {
                if (mPlayer != null) {
                    currentTime = mPlayer.getPlayCurrentTime();
                    Intent intent = new Intent();
                    intent.setAction(MusicPlayState.MUSIC_CURRENT);
                    intent.putExtra("currentTime", currentTime);
                    sendBroadcast(intent);
                    handler.sendEmptyMessageDelayed(1, 1000);
                }
            }
        }
    };

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

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

        mPlayer = new MusicPlayer(this);

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        if (intent != null) {

            musicInfoBean = new MusicInfoBean();
            msg = intent.getIntExtra("MSG", 0);//播放信息

            mSonginfo = (MusicContentListBean) intent.getSerializableExtra("MusicInfoBean");

            if (mSonginfo != null) {

                musicInfoBean.setSongName(mSonginfo.getSongname());
                musicInfoBean.setSingerName(mSonginfo.getSingername());

                if (mSonginfo.getM4a() != null) {
                    musicInfoBean.setUrl(mSonginfo.getM4a());
                } else if (mSonginfo.getUrl() != null) {
                    musicInfoBean.setUrl(mSonginfo.getUrl());
                }

                musicInfoBean.setSingerPic(mSonginfo.getAlbumpic_big());
            }
        }
        if (msg == MusicPlayState.MPS_PLAYING) {
            
            new PlayTask(this).execute(Uri.parse(musicInfoBean.getUrl()));
            handler.sendEmptyMessage(1);
            
            Intent intent2 = new Intent(MusicPlayState.MUSIC_INFO);
            intent.putExtra("NewMusicInfoBean", musicInfoBean);
            sendBroadcast(intent2);
            
        } else if (msg == MusicPlayState.MPS_PAUSE) {
            mPlayer.pause();
        } else if (msg == MusicPlayState.MPS_CONTINUE) {
            mPlayer.resume();
        } else if (msg == MusicPlayState.UPDATE_PROGRESS) {
            currentTime = intent.getIntExtra("progress", -1);
            mPlayer.seekToMusic(currentTime);
        }

        return super.onStartCommand(intent, flags, startId);
    }


    public class PlayTask extends AsyncTask<Uri, Void, Void> {

        private Context mContext;

        public PlayTask(Context mContext) {
            this.mContext = mContext;
        }

        @Override
        protected Void doInBackground(Uri... params) {
            mPlayer.playOnline(mContext, params[0]);
            return null;
        }
    }

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

}

Activity:广播已经注册了,也已经添加过滤了....

:
:
  /**
     * 用来接收从service传回来的广播的内部类
     */
    public class PlayerReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            switch (intent.getAction()) {
                case MusicPlayState.MUSIC_CURRENT:
                    currentTime = intent.getIntExtra("currentTime", -1);
                    musicsPlayerCurrentTime.setText(CommonUtils.durationToString(currentTime));
                    timeSbProgress.setProgress(currentTime);
                    break;
                case MusicPlayState.MUSIC_DURATION:
                    duration = (int) intent.getLongExtra("SongDuration", -1);
                    Log.e("duration", String.valueOf(duration));
                    timeSbProgress.setMax(duration);
                    musicsPlayerTotalTime.setText(CommonUtils.durationToString(duration));
                    break;
                case MusicPlayState.MUSIC_INFO:
                    MusicInfoBean infoBean = (MusicInfoBean) intent.getSerializableExtra("NewMusicInfoBean");
                    mPlayerDiscView.loadAlbumCover(infoBean.getSingerPic());
                    musicsSongName.setText(infoBean.getSongName());
                    musicsPlayerSongerName.setText(infoBean.getSingerName());
                    break;
            }
        }
    }
阅读 5.4k
3 个回答

确定下是否有并行地接收3个Action的过滤器?

广播接收不到,通常有三个原因:

  1. 没发

  2. 没注册监听

  3. 没权限(或一些其它配置)

  4. 被取消了

自定义广播通常是前两个问题。
题主的代码中一行log都没有,并且没有展示注册监听的代码,所以都有可能。

建议用Log.d(String, String)自行调试。

是否已经startService并执行到了MusicPlayState.MPS_PLAYING?

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题