android service与activity双向通信

 


     前些日子在做音乐播放器的例子,在做的过程中要涉及到service 与activity之间的交互,于是乎就在网上搜罗例子,例子确实很多,但是涉及到他俩之间互相交互的例子就少了。于是乎决定把自己的例子写下来,与大家分享,有不足之处还请各位多多指教(这是我在csdn上的第一篇博文,谢谢各位支持)

     service与activity之间通信肯定需要用到AIDL技术,关于它就不具体的说了,网上一大推,直接上例子,例子比较直观。

    (1) service端 AIDL文件:文件名  IMusicService.aidl

           package com.carsmart.music.aidl; 

           //引用客户端的aidl(即activity端)
           import com.carsmart.music.aidl.IPlayStatusChangeListener; 
 
           interface IMusicService { 
                   void addLisener(in IPlayStatusChangeListener psc);
                   void play(String musPlayListName,int musicIndex);
                   void playByDegree(int degree);
                   void pause();
                   void stop();
                   void next();
                   void previous();
                   void setPlayPattern(int playPattern);
                   int getProgress();
                   int getDuration();
                   String getPlayListName();
                   int getMusicIndex();
            } 


 

     (2)是要在Service 中的继承IMusicService.Stub类(该类是写好aidl文件后,若没错,eclipse会自动在工程的gen文件夹下生成一个IMusicService.java类)

      

        //返回给客户端
	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub
		playerProxy = new MusicPlayerProxy();
		return playerProxy;
	}
	
	//实现IMusicService.Stub子类
	private class MusicPlayerProxy extends IMusicService.Stub{

		@Override
		public void addLisener(IPlayStatusChangeListener psc)
				throws RemoteException {
			// TODO Auto-generated method stub
			musicPlayer.addLisener(psc);
		}

		@Override
		public void play(String musPlayListName, int musicIndex) throws RemoteException {
			// TODO Auto-generated method stub
			musicPlayer.play(musPlayListName,musicIndex);
		}

		@Override
		public void playByDegree(int degree) throws RemoteException {
			// TODO Auto-generated method stub
			musicPlayer.playByDegree(degree);
		}

		@Override
		public void pause() throws RemoteException {
			// TODO Auto-generated method stub
			musicPlayer.pause();
		}

		@Override
		public void stop() throws RemoteException {
			// TODO Auto-generated method stub
			musicPlayer.stop();
		}

		@Override
		public void next() throws RemoteException {
			// TODO Auto-generated method stub
			musicPlayer.next();
		}

		@Override
		public void previous() throws RemoteException {
			// TODO Auto-generated method stub
			musicPlayer.previous();
		}

		@Override
		public void setPlayPattern(int playPattern) throws RemoteException {
			// TODO Auto-generated method stub
			musicPlayer.setPlayPattern(playPattern);
		}

		@Override
		public int getProgress() throws RemoteException {
			// TODO Auto-generated method stub
			return musicPlayer.getProgress();
		}

		@Override
		public int getDuration() throws RemoteException {
			// TODO Auto-generated method stub
			return musicPlayer.getDuration();
		}

		@Override
		public String getPlayListName() throws RemoteException {
			// TODO Auto-generated method stub
			return musicPlayer.getPlayListName();
		}

		@Override
		public int getMusicIndex() throws RemoteException {
			// TODO Auto-generated method stub
			return musicPlayer.getMusicIndex();
		}
		
	}

        (3)activity端的AIDL文件:文件名  IPlayStatusChangeListener.aidl    在我例子里的作用就是监听播放音乐的状态及歌曲相关信息

                   

        package com.carsmart.music.aidl; 

        //只能传递基本类型数据及string类型,若想传递复杂类型或自定义类型得让被传递对象实现Serializable或Parcelable接口即可
         interface IPlayStatusChangeListener { 
                 void onPlayStatChange(int status);
                 void onMusicInfoChange(String title,String artist);
                 void onPlayMusicIndexChanged(String playListName,int musicIndex);
          } 


          (4)在activity端实现IPlayStatusChangeListener.Stub

                    

       public class PlayStatusChangeAdapter  extends IPlayStatusChangeListener.Stub{
	  //播放状态发生改变
	  @Override
	  public void onPlayStatChange(int status) throws RemoteException {
		// TODO Auto-generated method stub
	  }

	  //当播放歌曲发生改变时,把当前播放歌曲的名作者发过来,用于播放界面
	  @Override
	  public void onMusicInfoChange(String title,String artist) throws RemoteException {
		// TODO Auto-generated method stub
	  }

	  //当播放歌曲发生改变时,把播放列表名,索引广播,用于涮新播放列表
	  @Override
	  public void onPlayMusicIndexChanged(String playListName, int musicIndex)
			throws RemoteException {
		// TODO Auto-generated method stub
		
	  }
       }


             (5)在activity端 生成一个ServiceConnection子类,用于bindService连接服务端


 

     private class SerConn implements ServiceConnection{

		@Override
		public void onServiceConnected(ComponentName name, IBinder service) {
			// TODO Auto-generated method stub
			ims = IMusicService.Stub.asInterface(service);
	        
	      		try {
				
				//若在构造方法或onCreate方法里设置有可能报空指针错误(因ims有可能没连接上)  设置监听器
				if(plaStaChaListener != null){
				   ims.addLisener(plaStaChaListener);
				}
			} catch (RemoteException e) {
				e.printStackTrace();
			}
		}

		@Override
		public void onServiceDisconnected(ComponentName name) {
			// TODO Auto-generated method stub
			ims = null;
		}
		
	}


               (6)最后一步也是最轻松的一步即和服务器连接

        
        
        SerConn conn = new SerConn();
        Intent service = new Intent("com.carsmart.music.aidl.IMusicService");
        this.bindService(service, conn, this.activity.BIND_AUTO_CREATE);

   大功告成,希望各位多多指教

你可能感兴趣的:(android,service,android,string,interface,class,eclipse)