Итак, древовидная структура — это project/server_app и project/aidlinterface, а для клиента — project/client_app и project/aidlinterface. Таким образом, приложение и библиотека находятся в одной иерархии. Благодаря этому мне удалось создать пакеты сервера и клиента, которые бы были одинаковыми «com.aidlInterface». Первый вопрос: подходит ли этот подход для этой проблемы/жизнеспособен ли этот подход или есть лучший способ?
Второй вопрос: затем я создал службу в серверном приложении и гарантировал, что служба инициализируется путем входа в MainActivity.kt в функции onCreate:
Код: Выделить всё
startService(Intent(this, ServiceName::class.java))
code>, ничего не появляется, что говорит мне о том, что пакет не прикреплен. но я добавил пакет:
Код: Выделить всё
dependencies {
implementation(project(":aidlInterface"))
...
}
Код: Выделить всё
// IAIDLColorInterface.aidl
package com.aidlinterface;
// Declare any non-default types here with import statements
interface IAIDLColorInterface {
/**
* Demonstrates some basic types that you can use as parameters
* and return values in AIDL.
*/
int getColor();
}
Вот сервис в серверном приложении:
Код: Выделить всё
package com.aidlinterface;
import android.app.Service;
import android.content.Intent;
import android.graphics.Color;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
import java.util.Random;
public class AIDLColorService extends Service {
private static final String TAG ="AIDLColorService" ;
public AIDLColorService() {
}
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
return binder;
}
private final IAIDLColorInterface.Stub binder = new IAIDLColorInterface.Stub() {
@Override
public int getColor() throws RemoteException {
Random rnd = new Random();
int color = Color.argb(255, rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256));
Log.d(TAG, "getColor: "+ color);
return color;
}
};
}
Код: Выделить всё
Код: Выделить всё
import com.aidlinterface.IAIDLColorInterface
class ColorPlugin(
mapView:MapView ){
private var AIDLColorService: IAIDLColorInterface? = null
private val serviceConnection = object: ServiceConnection {
override fun onServiceConnected(name: ComponentName?, service:IBinder?){
AIDLColorService = IAIDLColorInterface.Stub.asInterface(service)
}
override fun onServiceDisconnected(name: ComponentName?){
AIDLColorService = null
}
init{
val serviceIntent = Intent("AIDLColorService")
serviceIntent.setPackage("com.aidlInterface")
pluginContext.bindService(serviceIntent, serviceConnection, Context.BIND_AUTO_CREATE)
override fun disposeImpl(){
pluginContext.unbindService(serviceConnection)
}
override fun onReceive(context:Context, intent: Intent){
// code for plugin
// CODE THAT CALLS SERVICE
b.setOnClickListener {
try {
val color = iADILColorService.color
it.setBackgroundColor(color)
} catch (e: RemoteException) {
// Handle exception
}
}
}
Подробнее здесь: https://stackoverflow.com/questions/789 ... server-app
Мобильная версия