第二种:使用Receiver方式启动服务
这里的实现原理是首先创建一个PlayReceiverMusic,通过该Activity启动BroadCastReceiver,在MusicReceiver类中接收该广播。然后重写onReceive(),然后重写创建intent对象。启动MusicService。因此,实际上仍然是使用MusicService的代码来进行播放音乐。增加的步骤是在startService()方法调用前调用sendBroadcast()方法发送广播并启动Receiver。实现效果如下:
点击Receiver Service之后进入第二个页面:
进入第二个页面有五个Button,分别实现不同的音乐播放功能。
接下来分析一下代码:
package com.lqh.servicetest;
import android.R.integer;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class PlayReceiverMusic extends Activity implements OnClickListener{
private static final String TAG="PlayMusic";
private Button playBtn,stopBtn,pauseBtn,exitBtn,closeBtn;
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.music_service);
setTitle("Music Service");
findView();
bindButton();
}
private void findView(){
playBtn=(Button) findViewById(R.id.play);
stopBtn=(Button) findViewById(R.id.stop);
pauseBtn=(Button) findViewById(R.id.pause);
exitBtn=(Button) findViewById(R.id.exit);
closeBtn=(Button) findViewById(R.id.close);
}
private void bindButton(){
playBtn.setOnClickListener(this);
stopBtn.setOnClickListener(this);
pauseBtn.setOnClickListener(this);
exitBtn.setOnClickListener(this);
closeBtn.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
int op=-1;
Intent intent=new Intent("com.android.ServiceDemo.musicReceiver");
switch (v.getId()) {
case R.id.play:
Log.d(TAG,"onClick:playing music");
op=1;
break;
case R.id.pause:
Log.d(TAG,"onClick:pausing music");
op=2;
break;
case R.id.stop:
Log.d(TAG,"onClick:stoping music");
op=3;
break;
case R.id.close:
Log.d(TAG,"onClick:close");
this.finish();
break;
case R.id.exit:
Log.d(TAG,"onClick:exit");
stopService(intent);
this.finish();
return;
}
Bundle bundle=new Bundle();
bundle.putInt("op", op);
intent.putExtras(bundle);
sendBroadcast(intent);
// startService(intent);
}
}
注意:这里我们用的sendBroadcast()启动intent对象。
然后进入MusicReceiver:
package com.lqh.servicetest;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
public class MusicReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Intent it=new Intent("com.lqh.servicetest.MusicService");
Bundle bundle=intent.getExtras();
it.putExtras(bundle);
if(bundle!=null){
int op=bundle.getInt("op");
if(op==4){
context.stopService(it);
}else{
context.startService(it);
}
}
}
}
然后通过MusicReceiver来启动音乐播放服务:
package com.lqh.servicetest;
import android.R.integer;
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;
public class MusicService extends Service {
private MediaPlayer mediaPlayer;
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
public void onCreate(){
Toast.makeText(this, "启动音乐播放器", Toast.LENGTH_SHORT).show();
if(mediaPlayer==null){
mediaPlayer=MediaPlayer.create(this, R.raw.tmp);
mediaPlayer.setLooping(false);
}
}
public void onDestroy(){
Toast.makeText(this, "停止音乐播放器", Toast.LENGTH_SHORT).show();
if(mediaPlayer!=null){
mediaPlayer.stop();
mediaPlayer.release();
}
}
public void onStart(Intent intent,int startId){
if(intent!=null){
Bundle bundle=intent.getExtras();
if(bundle!=null){
int op=bundle.getInt("op");
switch (op) {
case 1:
play();
break;
case 2:
pause();
break;
case 3:
stop();
break;
}
}
}
}
public void play(){
if(!mediaPlayer.isPlaying()){
mediaPlayer.start();
}
}
public void pause(){
if(mediaPlayer!=null&&mediaPlayer.isPlaying()){
mediaPlayer.pause();
}
}
public void stop(){
if(mediaPlayer!=null){
mediaPlayer.pause();
mediaPlayer.seekTo(0);
}
}
}
最后,在AndroidManifest中注册一下Receiver和Service即可实现。
<service
android:enabled="true"
android:name=".MusicService"
>
<intent-filter>
<action android:name="com.lqh.servicetest.MusicService"/>
</intent-filter>
</service>
<receiver android:name=".MusicReceiver">
<intent-filter>
<action android:name="com.android.ServiceDemo.musicReceiver"/>
</intent-filter>
</receiver>
大部分和第一种通过StartService()方式启动相同。所以代码也较为简单明了。