Remote Service拓展

通常每个应用程序都在它自己的进程内运行,但有时需要在进程之间传递对象(IPC通信),你可以通过应用程序UI的方式写个运行在一个不同的进程中的service。在android平台中,一个进程通常不能访问其它进程中的内存区域。所以,他们需要把对象拆分成操作系统能理解的简单形式,以便伪装成对象跨越边界访问。编写这种伪装代码相当的枯燥乏味,好在android为我们提供了AIDL工具可以来做这件事。
 
AIDL(android接口描述语言)是一个IDL语言,它可以生成一段代码,可以使在一个android设备上运行的两个进程使用内部通信进程进行交互。如果你需要在一个进程中(例如在一个Activity中)访问另一个进程中(例如一个Service)某个对象的方法,你就可以使用AIDL来生成这样的代码来伪装传递各种参数。
 
要使用AIDL,Service需要以aidl文件的方式提供服务接口,AIDL工具将生成一个相应的java接口,并且在生成的服务接口中包含一个功能调用的stub服务桩类。Service的实现类需要去继承这个stub服务桩类。Service的onBind方法会返回实现类的对象,之后你就可以使用它了,参见下例:

IMusicControlService.aidl

  1. package com.homer.remote;  
  2.   
  3. interface IMusicControlService{  
  4.         void play();   
  5.         void stop();   
  6.         void pause();  
  7. }  

使用eclipse的Android插件,会根据这个aidl文件生成一个Java接口类,生成的接口类中会有一个内部类Stub类,Service来继承该Stub类:
Service
[java]  view plain copy print ?
  1. public class RemoteMusicService extends Service {  
  2.   
  3.     private MediaPlayer mediaPlayer;  
  4.   
  5.     @Override  
  6.     public IBinder onBind(Intent intent) {  
  7.         return binder;  
  8.     }  
  9.   
  10.     private final IMusicControlService.Stub binder = new IMusicControlService.Stub() {  
  11.   
  12.         @Override  
  13.         public void play() throws RemoteException {  
  14.             if (mediaPlayer == null) {  
  15.                 mediaPlayer = MediaPlayer.create(RemoteMusicService.this, R.raw.tmp);  
  16.                 mediaPlayer.setLooping(false);  
  17.             }  
  18.             if (!mediaPlayer.isPlaying()) {  
  19.                 mediaPlayer.start();  
  20.             }  
  21.         }  
  22.   
  23.         @Override  
  24.         public void pause() throws RemoteException {  
  25.             if (mediaPlayer != null && mediaPlayer.isPlaying()) {  
  26.                 mediaPlayer.pause();  
  27.             }             
  28.         }  
  29.   
  30.         @Override  
  31.         public void stop() throws RemoteException {  
  32.             if (mediaPlayer != null) {  
  33.                 mediaPlayer.stop();  
  34.                 try {  
  35.                     mediaPlayer.prepare();      // 在调用stop后如果需要再次通过start进行播放,需要之前调用prepare函数  
  36.                 } catch (IOException ex) {  
  37.                     ex.printStackTrace();  
  38.                 }  
  39.             }  
  40.         }  
  41.     };  
  42.       
  43.     @Override  
  44.     public void onDestroy() {  
  45.         super.onDestroy();  
  46.           
  47.         if(mediaPlayer != null){  
  48.             mediaPlayer.stop();  
  49.             mediaPlayer.release();  
  50.         }  
  51.     }  
  52. }  

客户端(Activity)应用连接到这个Service时,onServiceConnected方法将被调用,客户端就可以获得IBinder对象。参看下面的客户端onServiceConnected方法:

Activity

[java]  view plain copy print ?
  1. public class PlayRemoteMusic extends Activity implements OnClickListener {  
  2.   
  3.     private Button playBtn;  
  4.     private Button stopBtn;  
  5.     private Button pauseBtn;  
  6.     private Button exitBtn;  
  7.   
  8.     private IMusicControlService musicService;  
  9.   
  10.     @Override  
  11.     public void onCreate(Bundle savedInstanceState) {  
  12.         super.onCreate(savedInstanceState);  
  13.         setContentView(R.layout.remote_music_service);  
  14.   
  15.         playBtn = (Button) findViewById(R.id.play);  
  16.         stopBtn = (Button) findViewById(R.id.stop);  
  17.         pauseBtn = (Button) findViewById(R.id.pause);  
  18.         exitBtn = (Button) findViewById(R.id.exit);  
  19.   
  20.         playBtn.setOnClickListener(this);  
  21.         stopBtn.setOnClickListener(this);  
  22.         pauseBtn.setOnClickListener(this);  
  23.         exitBtn.setOnClickListener(this);  
  24.   
  25.         connection();  
  26.     }  
  27.   
  28.     private void connection() {  
  29.         Intent intent = new Intent("com.homer.remote.remoteMusicReceiver");  
  30.         bindService(intent, sc, Context.BIND_AUTO_CREATE);              // bindService  
  31.     }  
  32.   
  33.     @Override  
  34.     public void onClick(View v) {  
  35.   
  36.         try {  
  37.             switch (v.getId()) {  
  38.             case R.id.play:  
  39.                 musicService.play();  
  40.                 break;  
  41.             case R.id.stop:  
  42.                 if (musicService != null) {  
  43.                     musicService.stop();  
  44.                 }  
  45.                 break;  
  46.             case R.id.pause:  
  47.                 if (musicService != null) {  
  48.                     musicService.pause();  
  49.                 }  
  50.                 break;  
  51.             case R.id.exit:  
  52.                 this.finish();  
  53.                 break;  
  54.             }  
  55.         } catch (RemoteException e) {  
  56.             e.printStackTrace();  
  57.         }  
  58.     }  
  59.   
  60.     private ServiceConnection sc = new ServiceConnection() {  
  61.         @Override  
  62.         public void onServiceConnected(ComponentName name, IBinder service) {       //connect Service  
  63.             musicService = IMusicControlService.Stub.asInterface(service);  
  64.         }  
  65.   
  66.         @Override  
  67.         public void onServiceDisconnected(ComponentName name) {                 //disconnect Service  
  68.             musicService = null;  
  69.         }  
  70.   
  71.     };  
  72.       
  73.     @Override  
  74.     public void onDestroy(){  
  75.         super.onDestroy();  
  76.           
  77.         if(sc != null){  
  78.             unbindService(sc);              // unBindService  
  79.         }  
  80.     }  
  81. }  

Remote Service流程总结:

1、 Activity(客户端)中,Intent intent = new Intent("com.homer.remote.remoteMusicReceiver");构建intent,然后bindService(intent, sc, Context.BIND_AUTO_CREATE);绑定服务

2、 Activity(客户端)中,通过ServiceConnection()重载onServiceConnected()建立连接,获取Service.Stub实例;onServiceDisconnected()释放连接(与bindService类似)

3、 Service中,通过重载onBind(Intent intent) 返回Service.Stub实例,但Service.Stub类是由aidl文件生成的接口类中的一个内部类Stub类,Service来继承该Stub类

4、 Activity中,通过操作Service实例(musicService),执行音乐播放操作(play、pause、stop等)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值