`
qilixiang012
  • 浏览: 200759 次
文章分类
社区版块
存档分类
最新评论

Android Service AIDL

 
阅读更多

Service AIDL简述

Android系统中的进程之间不能共享内存,因此,需要提供一些机制在不同进程之间进行数据通信。Android应用程序组

件中的4个(Activity、Broadcast、 Service和Content Provider)都可以进行跨进程访问,Service就是通过AIDL服务来

完成不同进程之间的通信。


在AIDL服务中有两种对象:

服务程序:给调用者提供服务.将自己的服务接口暴露在外,让用户通过这些接口来调用自己的方法.

调用程序:使用服务者提供的服务.通过已知的服务程序的暴露接口,来调用服务程序的方法来完成自己的任务.


AIDL Service的编写方法

步骤:

1.曝露接口(创建AIDL文件)

2.实现接口(编写Service程序)

3.部署服务程序(在AndroidManifest.xml中设置Service的访问属性)


曝露接口(创建AIDL文件)

AIDL定义接口的源代码必须以.aidl为扩展名.

AIDL接口中用到的数据类型,除基本类型\String \List\Map\CharSequence之外,其他类型全部需要导入包

Android-sdk下的platform-tools目录下aidl.exe工具包会自动将接口文件转化为在gen目录下同名的java接口文件

生成的接口文件中包含一个Stub内部类,该类实现了IBinder接口和AIDL自定义的暴露接口.该类就是远程Service的回调

类(onBind()方法的返回值)

接口文件中包含一个名为asInterface(IBinder iBinder)的重要方法,它返回接口的实例(不是本地服务bindService() 通过

getService()).


实现接口(编写Service程序)

定义一个实现了Stub子类的对象.

在其中实现暴露接口的服务方法

给Service的onBind()方法返回这个对象

其它的生命周期方法和bindService的一样


部署服务程序

在AndroidManifest.xml文件中配置该Service的相关属性

如:
<service android:name=“.lgstestService”>
<intent-filter>
<action android:name=“cn.edu.zwu.tel.lgsFlag”>
</intent-filter>
</service>


AIDL Service的调用者的编写方法

将服务程序暴露的.aidl接口文件复制到调用者程序文件目录中,该接口也回被aidl.exe工具自动转化为gen目录下的.java文件

创建ServiceConnection对象

建立intent对象,设置其启动的服务类名\以及相关访问服务的属性

以ServiceConnection对象和intent对象为参数,调用bindService()方法绑定到远程的Service

onServiceConnected()方法中是通过IMusicPlayer.Stub.asInterface(service)的方法获得Service的代理,而不是象本

service中通过getService()方法直接获得service对象


Service AIDL与bindService的区别

绑定本地Service时可以直接获取onBind()方法的返回值;绑定远程Service时获取的是onBind()方法的返回对象的代理,因

此需要进行一些处理.

本地Service的onBind()方法会直接把IBinder对象本身传给调用者的ServiceConnection中的onServiceConnected()方法

中的第二个参数,而远程Service的onBind()方法只是将IBinder对象的代理传递给调用者的ServiceConnection中的

onServiceConnected()方法中的第二个参数.



AIDL Service的编写方法源代码实例:

创建AIDL文件:

package cn.zj.nb.wl;
interface IMusicService
{
void play();
void pause();
void stop();
}


实现接口(编写Service程序):

package cn.zj.nb.wl;

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;
import android.os.RemoteException;

public class MusicService extends Service {

MediaPlayer mPlayer;

IMusicService.Stub stub=new IMusicService.Stub() {

@Override
public void stop() throws RemoteException {

mPlayer.stop();

}

@Override

public void play() throws RemoteException {

mPlayer.start();

}

@Override

public void pause() throws RemoteException {

mPlayer.pause();

}

};

@Override

public void onCreate() {

mPlayer=MediaPlayer.create(getApplicationContext(), R.raw.wind);

}

@Override

public void onDestroy() {

// TODO Auto-generated method stub

super.onDestroy();

}

@Override

public IBinder onBind(Intent intent) {

return stub;

}

}

部署服务程序:

<service
android:name=".MusicService">
<intent-filter>
<action android:name="cn.zj.nb.wl.action.lgs" />
</intent-filter>
</service>


AIDL Service的调用者:

创建AIDL文件:

package cn.zj.nb.wl;

interface IMusicService

{
void play();
void pause();
void stop();
}


Activity:

package cn.edu.zwu.tel;

public class PlayerActivity extends Activity {

ImageButton imageButtonStop;
ImageButton imageButtonNext;
ImageButton imageButtonPlay;
ImageButton imageButtonPre;
ImageButton imageButtonRepeat;
SeekBar musicSeekBar;

boolean isPlaying = false;
boolean isBound=false;

private IMusicService musicService;

private ServiceConnection conn=new ServiceConnection()
{
@Override
public void onServiceConnected(ComponentName name, IBinder service)
{
musicService=IMusicService.Stub.asInterface(service);
}



@Override
public void onServiceDisconnected(ComponentName name)
{
musicService=null;

}
};


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.player);

imageButtonStop = (ImageButton) findViewById(R.id.imageButtonStop);
imageButtonNext = (ImageButton) findViewById(R.id.imageButtonNext);
imageButtonPlay = (ImageButton) findViewById(R.id.imageButtonPlay);
imageButtonPre = (ImageButton) findViewById(R.id.imageButtonPre);
imageButtonRepeat = (ImageButton) findViewById(R.id.imageButtonRepeat);
musicSeekBar = (SeekBar) findViewById(R.id.musicSeekBar);

Intent intent=new Intent();
intent.setAction("cn.zj.nb.wl.action.lgs");

startService(intent);
if(!isBound)
{
bindService(intent,conn,Context.BIND_AUTO_CREATE);
isBound=true;
}

imageButtonPlay.setOnClickListener(new OnClickListener()
{

@Override
public void onClick(View v) {
if(!isPlaying)
{
try {
musicService.play();
} catch (RemoteException e) {
e.printStackTrace();
}
imageButtonPlay.setBackgroundResource(R.drawable.pause_button);
isPlaying = true;


}else
{
try {
musicService.pause();
} catch (RemoteException e) {
e.printStackTrace();
}
imageButtonPlay.setBackgroundResource(R.drawable.play_button);
isPlaying = false;
}

}
});

imageButtonStop.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
try {
musicService.stop();
} catch (RemoteException e) {
e.printStackTrace();
}
if(isBound)
{
unbindService(conn);
isBound=false;
musicService=null;
}
PlayerActivity.this.finish();
}
});
}


}


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics