0

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!

yossico
  • 3,421
  • 5
  • 41
  • 76

1 Answers1

0

Problem solved - you can pass the Service's Context via the constructor and keep it in IRecordServiceImpl as private field

yossico
  • 3,421
  • 5
  • 41
  • 76
  • Could you show me the code? – linjiejun Dec 20 '21 at 03:28
  • Actually, I don't have this code anymore. just add a context field to the IRecordServiceImpl constructor and pass the Context to it. – yossico Dec 20 '21 at 06:25
  • Anyway.Thk u... – linjiejun Dec 20 '21 at 07:28
  • It would be like that (sorry for doing it in comment, but there are too many pending edits, so I cannot edit the post) class RecordService : Service() { private val serviceImplementation = IRecordServiceImpl(this.applicationContext) override fun onBind(intent: Intent): IBinder { return serviceImplementation } } – Kostek Mar 29 '23 at 10:08