У меня есть фоновая служба, и я хотел сохранить и получить доступ к некоторым данным из комнаты db. Как это можно сделать? Вот полный код: - http://www.digitstory.com/android-detect-new-contact-addition/
public class ContactWatchService extends Service {
private Looper mServiceLooper;
private ServiceHandler mServiceHandler;
private final class ServiceHandler extends Handler {
public ServiceHandler(Looper looper) {
super(looper);
}
@Override
public void handleMessage(Message msg) {
try {
//Register contact observer
startContactObserver();
} catch (Exception e) {
e.printStackTrace();
}
}
}
private void startContactObserver(){
//TODO ------ ERROR CASE (when app is in background process can not access Room DB
List<String> names = MainActivity.myAppDB.myAttrDao().getAllNames();
}
@Override
public void onCreate() {
// Start up the thread running the service. Note that we create a
// separate thread because the service normally runs in the process's
// main thread, which we don't want to block. We also make it
// background priority so CPU-intensive work will not disrupt our UI.
HandlerThread thread = new HandlerThread("ServiceStartArguments",
Process.THREAD_PRIORITY_BACKGROUND);
thread.start();
// Get the HandlerThread's Looper and use it for our Handler
mServiceLooper = thread.getLooper();
mServiceHandler = new ServiceHandler(mServiceLooper);
}
Проблема в том, что приложение работает в фоновом режиме и не может получить доступ к абстрактному Дао, например
MainActivity.myAppDB.myAttrDao()