AS广播接收者BroadcastReceiver组件(进阶)
要求:
实现上一首、播放/暂停、停止、下一首的功能,同时显示播放歌曲的简单信息。
实现界面展示:
四个按钮的功能顺序依次为:
上一首 播放/暂停 停止 下一首
布局文件:
layout
2个TextView,4个ImageButton
事件监听控制:
MainActivity.java文件
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
TextView title,author;
ImageButton play,stop,last,next;
ActivityReceiver activityReceiver;
public static final String CTL_ACTION="org.crazyit.action.CTL_ACTION";
public static final String UPDATE_ACTION="org.crazyit.action.UPDATE_ACTION";
int status=0x11;
String[] titleStrs=new String[]{"无人之岛","朋友关系","知足"};
String[] authorStrs=new String[]{"任然","Jessie","五月天"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
play=(ImageButton)this.findViewById(R.id.play);
stop=(ImageButton)this.findViewById(R.id.stop);
last=(ImageButton)this.findViewById(R.id.last);
next=(ImageButton)this.findViewById(R.id.next);
title=(TextView)findViewById(R.id.title);
author=(TextView)findViewById(R.id.author);
play.setOnClickListener(this);
stop.setOnClickListener(this);
last.setOnClickListener(this);
next.setOnClickListener(this);
activityReceiver=new ActivityReceiver();
IntentFilter filter=new IntentFilter();
filter.addAction(UPDATE_ACTION);
registerReceiver(activityReceiver,filter);
Intent intent=new Intent(this,MusicService.class);
startService(intent);
}
@Override
public void onClick(View source) {
Intent intent=new Intent("org.crazyit.action.CTL_ACTION");
switch (source.getId())
{
case R.id.play:
intent.putExtra("control",1);
break;
case R.id.stop:
intent.putExtra("control",2);
break;
case R.id.last:
intent.putExtra("control",3);
break;
case R.id.next:
intent.putExtra("control",4);
break;
}
sendBroadcast(intent);
}
private class ActivityReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
int update=intent.getIntExtra("update",-1);
int current=intent.getIntExtra("current",-1);
if(current>=0)
{
title.setText(titleStrs[current]);
author.setText(authorStrs[current]);
}
switch (update)
{
case 0x11:
play.setImageResource(R.drawable.play);
status=0x11;
break;
case 0x12:
play.setImageResource(R.drawable.pause);
status=0x12;
break;
case 0x13:
play.setImageResource(R.drawable.play);
status=0x13;
break;
}
}
}
}
1、监听4个按钮的点击事件,发生点击事件就发送广播;
2、定义一个BroadcastReceiver监听从Service中传回的广播;
3、根据传回广播中携带的信息设置控制系统的状态,更改界面中播放键的图案显示和文本显示的歌曲信息。
MusicService.java文件
public class MusicService extends Service {
public MusicService() {
}
MyReceiver serviceReceiver;
AssetManager am;
String[] musics=new String[]{"1.mp3","2.mp3","3.mp3"};
MediaPlayer mPlayer;
int status=0x11;
int current=0;
@Override
public IBinder onBind(Intent intent) { return null;}
@Override
public void onCreate() {
super.onCreate();
am=getAssets();
serviceReceiver=new MyReceiver();
IntentFilter filter=new IntentFilter();
filter.addAction(MainActivity.CTL_ACTION);
registerReceiver(serviceReceiver,filter);
mPlayer=new MediaPlayer();
mPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
current++;
if (current>=3)
{
current=0;
}
Intent sendIntent = new Intent(MainActivity.UPDATE_ACTION);
sendIntent.putExtra("current",current);
sendBroadcast(sendIntent);
prepareAndPlay(musics[current]);
}
});
}
public class MyReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent) {
int control =intent.getIntExtra("control",-1);
switch (control)
{
case 1:
if (status==0x11)
{
prepareAndPlay(musics[current]);
status=0x12;
}
else if (status==0x12)
{
mPlayer.pause();
status=0x13;
}
else if (status==0x13)
{
mPlayer.start();
status=0x12;
}
break;
case 2:
if (status==0x12||status==0x13) {
mPlayer.stop();
status = 0x11;
}
break;
case 3:
if (status==0x11||status==0x13)
{
if(current==0) {
current=2;
prepareAndPlay(musics[current]);
}
else {
current=current-1;
prepareAndPlay(musics[current]);
}
status=0x12;
}
else if (status==0x12)
{
if(current==0) {
current=2;
prepareAndPlay(musics[current]);
}
else {
current=current-1;
prepareAndPlay(musics[current]);
}
}
break;
case 4:
if (status==0x11||status==0x13)
{
if(current==2) {
current=0;
prepareAndPlay(musics[current]);
}
else {
current=current+1;
prepareAndPlay(musics[current]);
}
status=0x12;
}
else if (status==0x12)
{
if(current==2) {
current=0;
prepareAndPlay(musics[current]);
}
else {
current=current+1;
prepareAndPlay(musics[current]);
}
}
break;
}
Intent sendIntent=new Intent(MainActivity.UPDATE_ACTION);
sendIntent.putExtra("update",status);
sendIntent.putExtra("current",current);
sendBroadcast(sendIntent);
}
}
private void prepareAndPlay(String music)
{
try
{
AssetFileDescriptor afd=am.openFd(music);
mPlayer.reset();
mPlayer.setDataSource(afd.getFileDescriptor(),afd.getStartOffset(),afd.getLength());
mPlayer.prepare();
mPlayer.start();
}catch (IOException e) {
e.printStackTrace();
}
}
}
1、监听每首歌是否播完,播完改变current的值,接着播放下一首,同时发送广播告诉Activity歌曲已更换,页面内容得更换;
2、接收Activity发出的广播,根据广播携带的信息改变标识状态的变量的值,同时控制音乐是否播放/暂停,控制当前音乐播放哪首歌;
3、把更改过的状态值和current值用广播传给Activity,让Activity做界面变动。
最后附上源码(码云仓库):
链接:https://gitee.com/GAIALscariot/Musicbox