Service Broadcast简单音乐播放功能

在Activity上有两个ImageButton,分别控制播放/暂停、停止。

image

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        start = (ImageButton) findViewById(R.id.play);
        stop = (ImageButton) findViewById(R.id.stop);
        start.setOnClickListener(this);
        stop.setOnClickListener(this);
        activityReceiver = new ActivityReceiver();//广播接收器,主要负责播放/暂停按钮图标的变换
        IntentFilter filter = new IntentFilter();
        filter.addAction("com.sunny.update");
       registerReceiver(activityReceiver, filter);
        Intent intent = new Intent(this, MyService.class);//启动服务,音乐的播放用Service控制,不会因Activity退出而停止
        startService(intent);
    }

其中ActivityReceiver 如下,主要负责播放/暂停按钮图标的变换,过滤接收Action为com.sunny.update的Intent:

public class ActivityReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            // TODO Auto-generated method stub
            int update=intent.getIntExtra("update", -1);
            switch(update){
            case 1:
                status=1;
                break;
            case 2:
                start.setImageResource(R.drawable.pause);
                status=2;
                break;
            case 3:
                start.setImageResource(R.drawable.play);
                status=3;
                break;
            }
        }
    }

为两个按钮注册时间,发送Action为com.sunny.control的广播控制音乐播放,由Service中的广播接收器接收:

public void onClick(View v) {
        // TODO Auto-generated method stub
        Intent intent = new Intent("com.sunny.control");
        switch (v.getId()) {
        case R.id.play:
           intent.putExtra("ACTION", 1);
            sendBroadcast(intent);
            break;
        case R.id.stop:
            intent.putExtra("ACTION", 2);
            sendBroadcast(intent);
            break;
        }
    }

在MyService中,启动时的onCreate()方法:

public void onCreate() {
        // TODO Auto-generated method stub
        status=1;//当前状态,1 没有声音 2正在播放 3 暂停
        serviceReceiver=new ServiceReceiver();//广播接收器,控制音乐播放
        IntentFilter filter=new IntentFilter();
        filter.addAction("com.sunny.control");
        registerReceiver(serviceReceiver, filter);
        super.onCreate();
    }

其中广播接收器ServiceReceiver 如下:

class ServiceReceiver extends BroadcastReceiver{

        @Override
        public void onReceive(Context context, Intent intent) {
            // TODO Auto-generated method stub
           int action=intent.getIntExtra("ACTION", -1);//过滤Intent Action为com.sunny.control的广播,根据ACTION参数控制
            switch(action){
            case 1:
                if(status==1){//在正在播放状态下
                    mp=MediaPlayer.create(context, R.raw.intro);
                    status=2;
                    Intent sendIntent=new Intent("com.sunny.update");
                    sendIntent.putExtra("update", 2);
                    sendBroadcast(sendIntent);
                    mp.start();
                }
                else if(status==2){//在暂停状态下
                    mp.pause();
                    status=3;
                    Intent sendIntent=new Intent("com.sunny.update");
                    sendIntent.putExtra("update",3);
                    sendBroadcast(sendIntent);
                }else if(status==3){//在暂停后重新播放状态下
                    mp.start();
                    status=2;
                    Intent sendIntent=new Intent("com.sunny.update");
                    sendIntent.putExtra("update", 2);
                    sendBroadcast(sendIntent);
               
                }
                break;
            case 2:
                if(status==2||status==3){
                    mp.stop();
                    status=1;
                    Intent sendIntent=new Intent("com.sunny.update");
                    sendIntent.putExtra("update",1);
                    sendBroadcast(sendIntent);
                }
            }
        }
       
    }

imageimage

你可能感兴趣的:(Broadcast)