首先我们先写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;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
public class AudioService extends Service{
private int volume;
private IAudioCallback iAudioCallback;
public AudioService() {
volume = 0;
}
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
//throw new UnsupportedOperationException("Not yet implemented");
Log.i("AudioService", "onBind " + volume);
return new AudioBinder();
}
private final class AudioBinder extends IAudio.Stub{
@Override
public int getVolume() throws RemoteException {
Log.i("AudioService", "getVolume " + volume);
iAudioCallback.onEvent("取得成功");
return volume;
}
@Override
public void setVolume(int step) throws RemoteException {
Log.i("AudioService", "setVolume " + step);
volume = step;
iAudioCallback.onEvent("设置成功");
}
@Override
public void registerCallback(IAudioCallback callback) throws RemoteException {
Log.i("AudioService", "registerListener " + callback);
iAudioCallback = callback;
}
}
}
代码比较简单,就做了三件事:
1、继承Service类,定义一个AudioBinder类用来继承IAudio.Stub类,实现了IAudio接口。
2、在onBind中返回这个AudioBinder类的对象。
3、在registerCallback方法中将传递过来的IAudioListener对象赋值给本地IAudioCallback变量,在需要回调是调用这个变量的onEvent方法就行。
最后要在AndroidManifest.xml中修改service的配置,否则Client会找不到Service端。
<service android:name=".AudioService"
android:exported="true">
<intent-filter>
<action android:name="AudioService" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</service>
到这里Service端就写好了,下面我们来写Client端。
同样是创建EmptyActivity的工程,然后将在Service端的aidl文件夹上点击右键->复制,在Client端的main文件夹点击右键->粘贴,注意要复制整个aidl目录。
然后编译,同样会自动生成IAudio.java文件。
然后在MainActivity中键入如下代码:
package com.example.audioclient;
import androidx.appcompat.app.AppCompatActivity;
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.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.example.audioservice.IAudio;
import com.example.audioservice.IAudioCallback;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private EditText editText;
private Button volumeSetButton;
private Button volumeGetButton;
private TextView textView;
private IAudio iAudio;
private AudioConnection audioConnection = new AudioConnection();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
volumeGetButton = findViewById(R.id.button_volume_get);
volumeSetButton = findViewById(R.id.button_volume_set);
editText = findViewById(R.id.edit_text);
textView = findViewById(R.id.textView);
volumeSetButton.setOnClickListener(this);
volumeGetButton.setOnClickListener(this);
//绑定远程Service
Intent service = new Intent("AudioService");
service.setPackage("com.example.audioservice");
bindService(service, audioConnection,BIND_AUTO_CREATE);
}
@Override
public void onClick(View v) {
if(v.getId() == R.id.button_volume_get){
try {
textView.setText("当前音量:"+ iAudio.getVolume());
} catch (RemoteException e) {
e.printStackTrace();
}
}else if(v.getId() == R.id.button_volume_set){
String number = editText.getText().toString();
int step = Integer.valueOf(number);
try {
iAudio.setVolume(step);
} catch (RemoteException e) {
e.printStackTrace();
}
}
}
private final class AudioConnection implements ServiceConnection{
@Override
public void onServiceConnected(ComponentName name, IBinder service) { //要注意绑定远程Service成功后会调用onServiceConnected方法,需要使用IAudio.Stub.asInterface(service)将iAudio赋值。
Log.i("AudioClient", "onServiceConnected");
iAudio = IAudio.Stub.asInterface(service);
try {
iAudio.registerCallback(new IAudioCallback.Stub() { //IAudioCallback.Stub是AIDL自动生成的实现类,可以通过该类向服务端注册处理回调。
@Override
public void onEvent(String message) throws RemoteException {
Toast.makeText(MainActivity.this,message,
Toast.LENGTH_SHORT).show();
}
});
} catch (RemoteException e) {
e.printStackTrace();
}
}
@Override
public void onServiceDisconnected(ComponentName name) {
iAudio = null;
}
}
}
代码也比较简单,只做了四件事:
1、新建AudioConnetion实现ServiceConnection接口。
2、以ServiceConnection对象作为参数,调用bindService绑定远程Service 。
3、在onServiceConnected中调用iAudio的registerCallback方法传入IAudioCallback.Stub对象,这里要注意传入的是IAudioCallback.Stub对象而不是IAudioCallback对象。
4、在onClick方法中调用AudioService的方法设置当前音量和取得当前音量,调用方法时就像是调用本地方法是的,但实际中间经过了Binder通信 。
运行后在editText中填写想要设置的音量,点击设置音量,就会将音量设置到Service端,再点击获取当前音量,就会从Service端取得音量,然后显示在textView 中。
设置成功和取得成功后Service端会调用Client的回调,Client收到回调后会弹出取得成功和设置成功的Toast。
Domo代码: