2 u010029536 u010029536 于 2016.04.26 23:47 提问

我动态注册的广播接收器接收不到广播,一下是我的代码 20C

/**
* 此方法通过传递列表点击位置来获取mp3Info对象
*
* @param listPosition
*/
public void playMusic(int listPosition) {
if (favmusicInfos != null) {
MusicInfo musicInfo = favmusicInfos.get(listPosition);

Intent intent = new Intent();
System.out.println("---------------------"+musicInfo);
intent.setAction("PLAY_MUSIC_FROME_FAVORITE");
intent.putExtra("musicInfo", musicInfo);
intent.putExtra("listPosition", listPosition);
sendBroadcast(intent);
}

 protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub


        super.onCreate(savedInstanceState);
        setContentView(R.layout.local_music_pop);

        mMusicList = (ListView)findViewById(R.id.list_view);
        //musicInfos = customeApp.getPlayingMusicInfos();
        playingAdapter = new PlayingListAdapter(this,musicInfos);
        if(musicInfos != null){
            mMusicList.setAdapter(playingAdapter);
        }
        playerReceiver = new PlayerReceiver();
        //IntentFilter filter = new IntentFilter();
        //registerReceiver(playerReceiver, filter);
        registerReceiver();
        //unregisterReceiver(playerReceiver);
        System.out.println("<<<---------------->>>");
    }


    private void registerReceiver() {
        //定义和注册广播接收器
        //playerReceiver = new PlayerReceiver();
        IntentFilter filter = new IntentFilter();
        registerReceiver(playerReceiver, filter);
        System.out.println("(((---------------->>>");
    }
    public class PlayerReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            // TODO Auto-generated method stub
            //intent = getIntent();
            System.out.println("(((----------------)))");
            String action = intent.getAction();
            System.out.println(action);
            if(action.equals("PLAY_MUSIC_FROM_LOCAL")){
                if(musicInfos == null){
                    musicInfos = customeApp.getlocalMusicInfos();
                    System.out.println("ping---------------)"+musicInfos);
                    mMusicList.setAdapter(playingAdapter);
                    int i = intent.getIntExtra("listPosition", -1);
                    musicInfo = musicInfos.get(i);
                    intent.putExtra("musicInfo",musicInfo );
                    intent.setClass(PlayingListActivity.this, Service1.class);
                    startService(intent);
                }
            }else if(action.equals("PLAY_MUSIC_FROME_FAVORITE")){
                if(musicInfos == null){
                    musicInfos = customeApp.getFavoriteMusicInfos();
                    System.out.println("ping---------------)"+musicInfos);
                    mMusicList.setAdapter(playingAdapter);
                    int i = intent.getIntExtra("listPosition", -1);

                    musicInfo = musicInfos.get(i);
                    intent.putExtra("musicInfo",musicInfo );
                    intent.setClass(PlayingListActivity.this, Service1.class);
                    startService(intent);
                }
            }
        }

    }

3个回答

fenhailan
fenhailan   2016.04.27 10:14

在你代码的26行是初始化了filter,但是这之后你没有为filter添加action,应该在之后filter.addAction(…)

sl_18500
sl_18500   2016.04.27 15:11

registerReceiver方法中需要添加filter.addAction("PLAY_MUSIC_FROM_LOCAL") filter.addAction("PLAY_MUSIC_FROME_FAVORITE")

sl_18500
sl_18500 那你发广播是怎么发的
一年多之前 回复
u010029536
u010029536 可是我加上之后还是收不到广播
一年多之前 回复
u010076059
u010076059   2016.05.04 22:21

动态注册 依赖于 程序是否退出,程序 退出了 肯定收不到

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!