有两个界面A和B 界面A—游戏设置界面 界面B—系统设置界面 两个界面都可以控制歌曲的播放 既然都是相同的界面和工能,那我们建一个baseActivity来减少重复的代码吧。
public abstract class BaseActivity extends Activity { private String tag = null; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.baseactivity); tag = getClass().getSimpleName(); initView();// 初始化view initData();// 初始化数据 } public abstract void initData(); public abstract void initView(); public void play(View view) { Log.v(tag, "play"); Intent intent = new Intent(this, MusicService.class); intent.putExtra(MusicService.CMD, MusicService.MUSIC_PLAY); startService(intent); /* MusicService musicService=new MusicService(); musicService.show();//调用service自己写的里面的show方法*/ } public void pause(View view) { Log.v(tag, "pause"); Intent intent = new Intent(this, MusicService.class); intent.putExtra(MusicService.CMD, MusicService.MUSIC_PAUSE); startService(intent); } public void stopmusic(View view) { Log.v(tag, "stopmusic"); Intent intent = new Intent(this, MusicService.class); intent.putExtra(MusicService.CMD, MusicService.MUSIC_SYOP); startService(intent); } public void stopservice(View view) { Log.v(tag, "stopservice"); Intent intent = new Intent(this, MusicService.class); stopService(intent); }}
baseActvity的布局文件如下
我懒得写 就直接用个相对布局直接拖的。。。
然后建两个activity来继承baseactivity
public class GameAtivity extends BaseActivity { @Override public void initData() { // TODO Auto-generated method stub } @Override public void initView() { // TODO Auto-generated method stub TextView textView=(TextView) findViewById(R.id.text_tv); textView.setText("游戏设置界面"); textView.setBackgroundColor(Color.BLUE); } @Override public boolean onCreateOptionsMenu(Menu menu) { // TODO Auto-generated method stub menu.add("音乐设置");// 添加子菜单 return super.onCreateOptionsMenu(menu); } @Override public boolean onOptionsItemSelected(MenuItem item) { // 进入到settingActivity Intent intent = new Intent(this, SettingActivity.class); startActivity(intent); return super.onOptionsItemSelected(item); }}
这里加了一个optionmenu来跳转到SettingActivity
public class SettingActivity extends BaseActivity { @Override public void initData() { // TODO Auto-generated method stub } @Override public void initView() { // TODO Auto-generated method stub TextView textView=(TextView) findViewById(R.id.text_tv); textView.setText("系统设置界面"); textView.setBackgroundColor(Color.RED); }}
还有最重要的服务代码
//服务端public class MusicService extends Service { private MediaPlayer player; private static final String TAG = "MusicService"; public static String CMD = "cmd"; public static final int MUSIC_PLAY = 1;// 开始 public static final int MUSIC_PAUSE = 2;// 暂停 public static final int MUSIC_SYOP = 3;// 停止 // 全局服务 @Override public IBinder onBind(Intent intent) { // TODO Auto-generated method stub return null; } @Override public void onCreate() { // TODO Auto-generated method stub Log.v(TAG, "onCreat"); super.onCreate(); } @Override public int onStartCommand(Intent intent, int flags, int startId) { // TODO Auto-generated method stub Log.v(TAG, "onStartCommand"); int cmd = intent.getIntExtra(CMD, 0); // 在这里接受意图 switch (cmd) { case MUSIC_PLAY: if(player==null){ //音乐资源id 不是资产目录因为不能生成r文件 res/raw player=MediaPlayer.create(getApplicationContext(), R.raw.cunzai); //电影的播放,音乐,缓冲--读取的数据时间不同步,内存里读取数据与硬盘和sd卡不同。 而在资源文件中的不需要缓冲 应用自带 } if (player.isPlaying()) { Toast.makeText(getApplicationContext(), "已经播放,无需再点击", 0).show(); }if (!player.isPlaying()) { player.start(); //不在播放时就播放 } break; case MUSIC_PAUSE: if(player!=null&&player.isPlaying()){ player.pause(); } break; case MUSIC_SYOP: if (player!=null) { player.stop(); player.release(); player=null; } break; default: break; } return super.onStartCommand(intent, flags, startId); } @Override public void onDestroy() { // TODO Auto-generated method stub Log.v(TAG, "ondestory"); if (player!=null) { player.stop(); player.release(); player=null; } super.onDestroy(); } public void show() { // 服务属于上下文的子类 Toast.makeText(getApplicationContext(), "", 0).show(); }}
在res目录下建一个raw文件夹,放一首歌曲到里面。才能放哦。想听歌吗 跑一遍代码吧~