利用BroadcastReceiver模拟音乐播放器,实现播放、暂停、音乐切换、显示当前歌曲信息等基本功能。
MainActivity
public class MainActivity extends Activity implements OnClickListener
{
TextView title, author;
ImageButton play, stop;
ImageButton pre, next;
ImageView cover;
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[] {
"伍佰", "周蕙", "四个女生" };
Integer[] covers = new Integer[] {
R.drawable.beautiful, R.drawable.promise, R.drawable.wish};
@Override
public 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);
title = (TextView) findViewById(R.id.title);
author = (TextView) findViewById(R.id.author);
cover = findViewById(R.id.cover);
pre = this.findViewById(R.id.pre);
next = this.findViewById(R.id.next);
play.setOnClickListener(this);
stop.setOnClickListener(this);
pre.setOnClickListener(this);
next.setOnClickListener(this);
activityReceiver = new ActivityReceiver();
IntentFilter filter = new IntentFilter();
filter.addAction(UPDATE_ACTION);
registerReceiver(activityReceiver, filter);
Intent intent = new Intent(this