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

查看:239
本文介绍了android - 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;
            }
        }
    }

解决方案

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

这篇关于android - Service发送广播到Activity没有接受到数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆