主界面非常简单,就是3个按钮去实现。监听了返回键,弹出对话框,如果需要隐藏播放器,就将期移动到后台去。
在服务中就是把其设置为前台进程。
这里需要注意的是,在对当前Activity做处理的时候,需要将其启动模式做更改,不能使用默认的启动方式,使用
默认的启动方式的话,会造成我这里的通知跳转进播放器的时候,有两个一模一样的界面
.....................................................................................
android:launchMode="singleTask"
.....................................................................................
package cn.itcast.musicdemo1; import cn.itcast.musicdemo1.dao.MusicI; import cn.itcast.musicdemo1.service.BackgroundMusicService; import android.os.Bundle; import android.os.IBinder; import android.view.KeyEvent; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.app.Activity; import android.app.AlertDialog; import android.content.ComponentName; import android.content.DialogInterface; import android.content.Intent; import android.content.ServiceConnection; public class MainActivity extends Activity implements OnClickListener { private Button start; private Button pause_resume; private Button stop; /** 服务中的接口 */ private MusicI mMusicI; private MyServiceConnection conn; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); start = (Button) findViewById(R.id.start); pause_resume = (Button) findViewById(R.id.pause_resume); stop = (Button) findViewById(R.id.stop); start.setOnClickListener(this); pause_resume.setOnClickListener(this); stop.setOnClickListener(this); /** 绑定服务 */ Intent service = new Intent(MainActivity.this, BackgroundMusicService.class); conn = new MyServiceConnection(); bindService(service, conn, BIND_AUTO_CREATE); } class MyServiceConnection implements ServiceConnection { @Override public void onServiceConnected(ComponentName name, IBinder service) { mMusicI = (MusicI) service; } @Override public void onServiceDisconnected(ComponentName name) { } } @Override public void onClick(View v) { switch (v.getId()) { case R.id.start: mMusicI.start(); break; case R.id.pause_resume: mMusicI.pause(); break; case R.id.stop: mMusicI.stop(); break; } } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { switch (keyCode) { // 监听返回键的操作 case KeyEvent.KEYCODE_BACK: new AlertDialog.Builder(MainActivity.this) .setTitle("设置") .setMessage("确定需要退出吗?") .setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { finish(); } }) .setNeutralButton("隐藏", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { /** 隐藏到后台任务站*/ moveTaskToBack(true); } }) .setNegativeButton("取消", null) .show(); break; default: break; } return super.onKeyDown(keyCode, event); } @Override protected void onDestroy() { super.onDestroy(); /** 断开绑定服务和服务的连接*/ if (conn != null) { unbindService(conn); conn = null; } } }
服务端的代码如下:
package cn.itcast.musicdemo1.service; import cn.itcast.musicdemo1.MainActivity; import cn.itcast.musicdemo1.dao.MusicI; import android.app.Notification; import android.app.PendingIntent; import android.app.Service; import android.content.Intent; import android.media.MediaPlayer; import android.os.Binder; import android.os.Environment; import android.os.IBinder; public class BackgroundMusicService extends Service { MyBinder mMyBinder = new MyBinder(); MediaPlayer player; @Override public IBinder onBind(Intent intent) { return mMyBinder; } @Override public void onCreate() { super.onCreate(); /** * id 通知的唯一识别 notification 通知 */ Notification notification = new Notification( android.R.drawable.ic_media_play, "素素播放器", //翻滚信息 System.currentTimeMillis()); Intent intent = new Intent(this, MainActivity.class); /** PendingIntent延时意图*/ PendingIntent contentIntent = PendingIntent.getActivity(this, 100, intent, 0); notification.setLatestEventInfo(this, "哈哈", "播放器运行在后台", contentIntent); /** 将当前进程提升为前台进程 */ startForeground(100, notification); player = new MediaPlayer(); player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() { @Override public void onCompletion(MediaPlayer mp) { } }); } @Override public void onDestroy() { super.onDestroy(); // 释放硬件资源 player.release(); } private class MyBinder extends Binder implements MusicI { @Override public void start() { try { player.reset(); player.setDataSource(Environment.getExternalStorageDirectory() + "/mote.mp3"); player.prepare(); player.start(); } catch (Exception e) { e.printStackTrace(); } } @Override public void pause() { player.pause(); } @Override public void stop() { player.stop(); } } }