1.在服务端和客户端各创建aidl文件,保证其包名完全一致。
2.IMyAidlInterface实现
// IMyAidlInterface.aidl
package com.replace.demo.aidl;
//需要手动引入包名
import com.replace.demo.aidl.IMyAidlInterfaceListener;
interface IMyAidlInterface {
//发送消息
void send(String str);
//获取消息
void getMessage(IMyAidlInterfaceListener listener);
}
3.IMyAidlInterfaceListener实现
// IMyAidlInterfaceListener.aidl
package com.replace.demo.aidl;
interface IMyAidlInterfaceListener {
//回调接口
void onResult(String str);
}
4.服务端实现
public class AidlService extends Service {
private IMyAidlInterfaceListener iMyAidlInterfaceListener;
@Nullable
@Override
public IBinder onBind(Intent intent) {
return new M();
}
@Override
public void onCreate() {
super.onCreate();
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
NotificationChannel notificationChannel = new NotificationChannel("HGG_10086"
, this.getClass().getName(), NotificationManager.IMPORTANCE_LOW);
NotificationManager systemService = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (systemService != null) {
systemService.createNotificationChannel(notificationChannel);
}
Notification notification = new Notification.Builder(this, "HGG_10086").build();
this.startForeground(100816, notification);
} else {
this.startForeground(100816, new Notification());
}
Log.e("HGG", "接收到:onCreate");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.e("HGG", "接收到:onStartCommand");
Toast.makeText(this, "接收到:onStartCommand", Toast.LENGTH_SHORT).show();
return super.onStartCommand(intent, flags, startId);
}
public class M extends IMyAidlInterface.Stub {
@Override
public void send(String str) throws RemoteException {
Log.e("HGG", "接收到:" + str);
if (iMyAidlInterfaceListener != null) {
iMyAidlInterfaceListener.onResult("接收到:" + str + " 返回:CDD");
}
}
@Override
public void getMessage(IMyAidlInterfaceListener listener) throws RemoteException {
iMyAidlInterfaceListener = listener;
}
}
}
5.在AndroidManifest中注册Service
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<service
android:name=".AidlService"
android:exported="true"> //允许该服务跨进程使用
6.客户端实现
public class AidlActivity extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_aidl);
}
private IMyAidlInterface iMyAidlInterface;
ServiceConnection serviceConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
Log.e("HGG", "onServiceConnected");
iMyAidlInterface = IMyAidlInterface.Stub.asInterface(service);
try {
iMyAidlInterface.getMessage(new IMyAidlInterfaceListener.Stub() {
@Override
public void onResult(String str) throws RemoteException {
Toast.makeText(AidlActivity.this, str, Toast.LENGTH_LONG).show();
}
});
} catch (RemoteException e) {
e.printStackTrace();
}
}
@Override
public void onServiceDisconnected(ComponentName name) {
Log.e("HGG", "onServiceDisconnected");
}
};
public void onClick(View view) {
try {
if (iMyAidlInterface != null) {
iMyAidlInterface.send("HGG发送的一个通信");
}
} catch (RemoteException e) {
e.printStackTrace();
}
}
public void onClick2(View view) {
Intent intent = new Intent();
//通过包名和类名的完整路劲启动服务
intent.setClassName("com.example.testaidl", "com.example.testaidl.AidlService");
bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);
}
}