织梦CMS - 轻松建站从此开始!

沙龙国际_salon365.com_沙龙国际www.salon365.com

当前位置: 主页 > 沙龙网上娱乐 >

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

时间:2017-08-13 03:27来源:未知 作者:admin 点击:
Service: public class MusicPlayService extends Service { private static MusicPlayer mPlayer = null; private int msg; private int currentTime;//当前播放进度 private long duration;//播放长度 private MusicContentListBean mSonginfo; p

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
      
      (责任编辑:admin)
织梦二维码生成器
顶一下
(0)
0%
踩一下
(0)
0%
------分隔线----------------------------
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
用户名: 验证码:点击我更换图片
栏目列表
推荐内容