Problem solved - you can pass the Service
's Context
via the constructor and keep it in IRecordServiceImpl
as private field
Using Context inside aidl service implementation
-
01-07-2022 - |
题
I'm trying to access sqlite DB (that is filled on diffrent part of the package) on AIDL stub implementation but - there is no context there. how can I get the context? there are 2 projects (applications) - A,B. Project A contains keeps records on sqlite DB. and contains aidl service. Project B needs to ask project A (diffrent package, there can be many projects like B) if a record exists. the only way for project A to answer project B from the stub is to check the DB is to have a Context (the "?????" in the code below) - how can I get the project A's context from the stub?
The AIDL's implementation:
public class IRecordServiceImpl extends IRecordService.Stub{
@Override
public boolean RecordExists(String recordKey)
throws RemoteException {
boolean returnValue = false;
RecordDataSource rds = new RecordDataSource(??????);
rds.Open();
returnValue = rds.isRecordExists(recordKey);
rds.Close();
return returnValue;
}
}
The Service code:
public class IRecordService extends Service {
private IRecordServiceImpl service;
@Override
public void onCreate() {
super.onCreate();
this.service = new IRecordServiceImpl();
}
@Override
public IBinder onBind(Intent intent) {
return this.service;
}
@Override
public boolean onUnbind(Intent intent) {
return super.onUnbind(intent);
}
@Override
public void onDestroy() {
super.onDestroy();
}
}
Thanks!
解决方案
不隶属于 StackOverflow