Android初试--Android中的Service简介(2)

来源:互联网 发布:系统还原数据恢复 编辑:程序博客网 时间:2024/05/29 10:02
服务的第二种启动方式:context.bindService()

context.bindService()的启动流程:
context.bindService()-----》onCreate()-----》onBind()-----Service running
onBind()将返回给Activity一个IBind接口对象,在Activity中我们通过ServiceConnection接口中的
onServiceConnected(ComponentName name, IBinder service) 的第二个参数,在Activity里面得到IBind接口对象。
然后我们可以使用,在Activity里面得到IBind接口对象,得到Service类的对象。
Context.unbindService()-----》onUnbind() ---》 onDestroy()---Service stop

实例分析图


package com.click369.bindservicedemo;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity{
   private  ServiceConnection  serviceConnection=null;
   private  MyService  myService=null;
   public  class  MyServiceConnection  implements ServiceConnection{
@Override
public void onServiceConnected(ComponentName arg0, IBinder ibinder) {
myService=((MyService.MyIBinder)ibinder).getService();
}
@Override
public void onServiceDisconnected(ComponentName arg0) {
myService=null;
}  
      }
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//启动服务[绑定服务]
Intent intentservice=new Intent(this,MyService.class);
serviceConnection=new MyServiceConnection();
this.bindService(intentservice, serviceConnection, Context.BIND_AUTO_CREATE);
}
public  void  playAndPause(View view){
Button  button=(Button)view;
String button_value=button.getText().toString();
if(button_value.equals("播放音乐")){
button.setText("暂停播放");
myService.play();

}else  if(button_value.equals("暂停播放")){
button.setText("播放音乐");
myService.pause();
}
}
   public  void  stopMyService(View view){
this.unbindService(serviceConnection);
}


   public  void  exitApp(View view){
this.finish();
    }
   @Override
   protected void onDestroy() {
super.onDestroy();
this.unbindService(serviceConnection);
              }
     }


package com.click369.bindservicedemo;
import java.io.File;
import java.io.IOException;
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 MyService extends Service {
    private  MediaPlayer  mediaPlayer=null;
private  MyIBinder  myIBinder=null;

public  class  MyIBinder  extends Binder{
public  MyService  getService(){
return MyService.this;
}
}
@Override
public void onCreate() {
super.onCreate();
myIBinder=new MyIBinder();
if(mediaPlayer==null){
mediaPlayer=new MediaPlayer();
File  path=new File(Environment.getExternalStorageDirectory(),"xinyu.mp3");
try {
mediaPlayer.setDataSource(path.getPath());
mediaPlayer.prepare();
mediaPlayer.setLooping(false);
} catch (Exception e) {
e.printStackTrace();

}
}
@Override
public IBinder onBind(Intent intent) {
return this.myIBinder;
}
@Override
public boolean onUnbind(Intent intent) {
return super.onUnbind(intent);
}
@Override
public void onDestroy() {
super.onDestroy();
if(mediaPlayer!=null || mediaPlayer.isPlaying()){
mediaPlayer.stop();
mediaPlayer.release();
}
}
public  void  play(){
if(mediaPlayer!=null && !mediaPlayer.isPlaying()){
//播放
mediaPlayer.start();
}
}
public void pause() {
if(mediaPlayer!=null && mediaPlayer.isPlaying()){
mediaPlayer.pause();
}
}
    }



0 0