首先我们先写Service端,新建立一个EmptyActivity的工程,点击项目栏右键->新建->AIDL->AIDL File,填入名字后确定,我填的名字是IAudio.aidl。
打开IAudio.aidl,填入如下代码:
// IAudio.aidl
package com.example.audioservice;
import com.example.audioservice.IAudioCallback;
// Declare any non-default types here with import statements
interface IAudio {
/**
* Demonstrates some basic types that you can use as parameters
* and return values in AIDL.
*/
void setVolume(int step);
int getVolume();
void registerCallback(IAudioCallback callback);
}
由于我们接手客户端的一个IAudioCallback接口,因此我们还需要创建一个名为IAudioCallback的aidl文件。
// IAudioCallback.aidl
package com.example.audioservice;
// Declare any non-default types here with import statements
interface IAudioCallback {
/**
* Demonstrates some basic types that you can use as parameters
* and return values in AIDL.
*/
void onEvent(String message);
}
完后进行build,生成自动生成IAudio.java文件和IAudioCallback.java文件,之后在代码中使用IAudio和IAudioCallback才能编译过。
创建Service类,我填写的名字是AudioService,键入如下代码:
package com.example.audioservice;
import android.app.Service;
imp