第一步:写一个Service类继承Sercice
package cn.itcast.aidl;
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;
import android.os.RemoteException;
import cn.itcast.aidl.aidl.IDownloadService;
//定义一个类继承服务
public class ServiceBinder extends Service {
private MediaPlayer mMediaPlayer;
private TextFile textFile = new TextFile();
//定义一个内部类继承Stub
public class TextFile extends IDownloadService.Stub {
@Override
//创建一个播放器
public void playMusic(String path) {
if (mMediaPlayer == null) {
//创建播放器音乐资源或者路径
mMediaPlayer = MediaPlayer.create(ServiceBinder.this, R.raw.a);
} else {
//重置播放器
mMediaPlayer.reset();
mMediaPlayer = MediaPlayer.create(ServiceBinder.this, R.raw.a);
}
mMediaPlayer.start();
}
//暂停播放器
@Override
public void pauseMusic() {
if (mMediaPlayer == null) {
mMediaPlayer.pause();
}
}
}
@Override
public IBinder onBind(Intent intent) {
return textFile;
}
}
以上把service写好了
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
另外服务端里的aidl包名和文件内容也要在客户端的App里定义要完全一样
例:
服务端
package cn.itcast.aidl.aidl;
interface IDownloadService {
void playMusic(String path);
void pauseMusic();
}
这是在gen目录下保存时自动生成的一个aidl
客户端
package cn.itcast.aidl.aidl;
interface IDownloadService {
void playMusic(String path);
void pauseMusic();
}
都是在gen目录下生成aidl
当然也可以直接复制
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
第二步:继续写一个客户端Activty继承Activty
package com.scxh.lizhong;
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.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import cn.itcast.aidl.aidl.IDownloadService;
public class MainActivity extends Activity {
private Button mnButton;
private IDownloadService mDownloadService;
private boolean isplayering=false;
//新建一个服务连接
private ServiceConnection mServiceConnection = new ServiceConnection() {
//实现服务连接的两个方法
@Override
//第一个方法接收IBinder,用IBinder和服务进行连接
public void onServiceConnected(ComponentName name, IBinder service) {
mDownloadService = IDownloadService.Stub.asInterface(service);
}
@Override
public void onServiceDisconnected(ComponentName name) {
mDownloadService = null;
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mnButton = (Button) findViewById(R.id.buttonservice);
//通过隐式调用
Intent intent = new Intent();
//和service里的androidmenifist注册的com.example.com.scxh.serviceBinder一致
intent.setAction("com.example.com.scxh.serviceBinder");
// startService(intent);
bindService(intent, mServiceConnection, BIND_AUTO_CREATE);
mnButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if(isplayering){
isplayering=false;
try {
mDownloadService.pauseMusic();
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
else{
isplayering=true;
try {
mDownloadService.playMusic("");
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
});
}
}
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
把xml写上
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="点击播放"
android:id="@+id/buttonservice"/>"
</RelativeLayout>