(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 handleMessageMessage msg { if msg.what == 1 { if mPlayer != null { currentTime = mPlayer.getPlayCurrentTime; Intent intent = new Intent; intent.setActionMusicPlayState.MUSIC_CURRENT; intent.putExtra"currentTime", currentTime; sendBroadcastintent; handler.sendEmptyMessageDelayed1, 1000; } } } }; @Nullable @Override public IBinder onBindIntent intent { return null; } @Override public void onCreate { super.onCreate; mPlayer = new MusicPlayerthis; } @Override public void onDestroy { super.onDestroy; } @Override public int onStartCommandIntent 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.setSongNamemSonginfo.getSongname; musicInfoBean.setSingerNamemSonginfo.getSingername; if mSonginfo.getM4a != null { musicInfoBean.setUrlmSonginfo.getM4a; } else if mSonginfo.getUrl != null { musicInfoBean.setUrlmSonginfo.getUrl; } musicInfoBean.setSingerPicmSonginfo.getAlbumpic_big; } } if msg == MusicPlayState.MPS_PLAYING { new PlayTaskthis.executeUri.parsemusicInfoBean.getUrl; handler.sendEmptyMessage1; Intent intent2 = new IntentMusicPlayState.MUSIC_INFO; intent.putExtra"NewMusicInfoBean", musicInfoBean; sendBroadcastintent2; } 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.seekToMusiccurrentTime; } return super.onStartCommandintent, flags, startId; } public class PlayTask extends AsyncTask<Uri, Void, Void> { private Context mContext; public PlayTaskContext mContext { this.mContext = mContext; } @Override protected Void doInBackgroundUri... params { mPlayer.playOnlinemContext, params[0]; return null; } } @Override public void onStartIntent intent, int startId { super.onStartintent, startId; } }

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

:
: /** * 用来接收从service传回来的广播的内部类 */ public class PlayerReceiver extends BroadcastReceiver { @Override public void onReceiveContext context, Intent intent { switch intent.getAction { case MusicPlayState.MUSIC_CURRENT: currentTime = intent.getIntExtra"currentTime", -1; musicsPlayerCurrentTime.setTextCommonUtils.durationToStringcurrentTime; timeSbProgress.setProgresscurrentTime; break; case MusicPlayState.MUSIC_DURATION: duration = int intent.getLongExtra"SongDuration", -1; Log.e"duration", String.valueOfduration; timeSbProgress.setMaxduration; musicsPlayerTotalTime.setTextCommonUtils.durationToStringduration; break; case MusicPlayState.MUSIC_INFO: MusicInfoBean infoBean = MusicInfoBean intent.getSerializableExtra"NewMusicInfoBean"; mPlayerDiscView.loadAlbumCoverinfoBean.getSingerPic; musicsSongName.setTextinfoBean.getSongName; musicsPlayerSongerName.setTextinfoBean.getSingerName; break; } } }

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

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

  1. 没发

  2. 没注册监听

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

  4. 被取消了

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

建议用Log.dString, String自行调试。

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

发表评论

电子邮件地址不会被公开。 必填项已用*标注