一个关于server使用 aidl的例子

package com.android.google;
interface IControlMusic
{
void playMusic();
void stopMusic();
void init();
}
以上是IControlMusic的aidl文件
下面的是ControlMusicService service文件
package com.android.google;
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;
import android.os.RemoteException;
public class ControlMusicService extends Service {
private  MediaPlayer player;
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
if(player.isPlaying())
{
player.stop();
player.release();
player=null;
}
}
private final IControlMusic.Stub binder=new IControlMusic.Stub()
{
@Override
public void playMusic() throws RemoteException {
// TODO Auto-generated method stub
player=MediaPlayer.create(ControlMusicService.this, R.raw.rain);
player.start();
}
@Override
public void stopMusic() throws RemoteException {
// TODO Auto-generated method stub
if(player.isPlaying())
{
player.stop();
}
}
@Override
public void init() throws RemoteException {
// TODO Auto-generated method stub
player=MediaPlayer.create(ControlMusicService.this, R.raw.rain);
}
};
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return binder;
}
}
以下是activity文件
package com.android.google;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class StartServiceActivity extends Activity {
private Button btnPlay, btnStop,btnWraper,btnSearch;
private IControlMusic myIControlMusic;
private Intent intent;
private ServiceConnection serviceConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
// TODO Auto-generated method stub
myIControlMusic = IControlMusic.Stub.asInterface(service);
}
@Override
public void onServiceDisconnected(ComponentName name) {
// TODO Auto-generated method stub
myIControlMusic = null;
// StartServiceActivity.this.unbindService(serviceConnection);
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
this.setContentView(R.layout.startserviceactivity);
binderServer();
btnPlay = (Button) findViewById(R.id.btn_play);
btnStop = (Button) findViewById(R.id.btn_stop);
btnWraper=(Button)findViewById(R.id.btn_wrapper);
btnSearch=(Button)findViewById(R.id.btn_search);
btnSearch.setOnClickListener(clickListener);
btnPlay.setOnClickListener(clickListener);
btnStop.setOnClickListener(clickListener);
btnWraper.setOnClickListener(clickListener);
}
private OnClickListener clickListener = new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.btn_play:
// startService(new Intent("com.dream.androidstu.MUSIC"));
try {
myIControlMusic.playMusic();
} catch (RemoteException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
// startService(intent);
break;
case R.id.btn_stop:
// stopService(new Intent("com.dream.androidstu.MUSIC"));
try {
if(myIControlMusic!=null){
myIControlMusic.stopMusic();
}
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
case R.id.btn_wrapper:
Intent intent=new Intent(Intent.ACTION_SET_WALLPAPER);//设置其为action事件
startActivity(Intent.createChooser(intent, "wall"));
break;
case R.id.btn_search:
onSearchRequested();
break;
}
}
};
private void binderServer() {
intent = new Intent();
intent.setClass(this, ControlMusicService.class);
bindService(intent, serviceConnection, BIND_AUTO_CREATE);
}
}
xml文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
android:id="@+id/tv_main"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"
    android:textSize="18px"/>
    
<Button 
android:text="播放音乐" 
android:id="@+id/btn_play" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" />
<Button 
android:text="停止播放" 
android:id="@+id/btn_stop" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" />
<Button 
android:text="wrapper" 
android:id="@+id/btn_wrapper" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" />
<Button 
android:text="search" 
android:id="@+id/btn_search" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" />
</LinearLayout>
总结:activity 是通过 aidl的方法和service进行通信的,所以在activity中要获得service的一个connection,该connection获得一个aidl端的实例,而service端者实现aidl中的方法,这样actiivty就实现和service端的通信了!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值