Android BroadcastReceiver Database Access with Room
M

1

5

I have setup an alarm that calls each hour a broadcast receiver. This receiver tries to load data from the sqlite database.

The problem is, that the list of reminders is null. The same codes works in an activity, but not in the receiver. Is there anything i have to change to access the database in the receiver. Is this a problem with the different context in the activity and in the receiver ?

The "setAlarm" method is the one i use, to create the alarm on an activity.

Regards

public class AppReceiver extends BroadcastReceiver {

    private static final String TAG = "AppReceiver";

    @Override
    public void onReceive(Context context, Intent intent) {
        ReminderRepository mRepository; = new ReminderRepository(context);
        List<Reminder> list = mRepository.getAllReminder();
        for(Reminder r : list) {
            // TODO
        }
    }
}


public class ReminderRepository {

    private ReminderDao mReminderDao;
    private List<Reminder> mAllReminder;

    public DisposalCalenderRepository(Context context) {
        ReminderRoomDatabase db = ReminderRoomDatabase.getDatabase(context);
        mReminderDao = db.reminderDao();
        mAllReminder = mReminderDao.getAll();
    }

    public List<Reminder> getAllReminder(){
        return mAllReminder;
    }

}

@Dao
public interface ReminderDao {

    @Query("SELECT * from reminder")
    List<Reminder> getAll();

}

public void setAlarm(){
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(System.currentTimeMillis());
    calendar.add(Calendar.HOUR, 1);
    calendar.set(Calendar.MINUTE, 0);
    calendar.set(Calendar.SECOND, 0);

    AlarmManager alarmMgr = (AlarmManager)mContext.getSystemService(Context.ALARM_SERVICE);
    Intent i = new Intent(mContext, AppReceiver.class);
    PendingIntent alarmIntent = PendingIntent.getBroadcast(mContext, 0, i, 0);

    alarmMgr.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
                AlarmManager.INTERVAL_HOUR, alarmIntent);
    }
Mccune answered 30/8, 2018 at 13:44 Comment(4)
What is your error in Logcat? or are you just getting 0 items returned? Handling a broadcast from wake up has some limitations on time it can operate at least, possibly internet usage as well, but not sure unless you can supply more infoZante
No errors appear in logcat. I only get a nullpointer because the "list" variable is null. So the NPE appear on the for loop.Mccune
Hmm, well technically Room blocks access from certain threads, but that should show up as an error. So you could try doing an async{} call to get it and return value. Probably not going to fix it, but worth a shot. Also does it step into the DAO to make the call, how far can you debug before you lose sight of what's happening and need to add logging?Zante
When i call the DAO directly I get the error that a can't access Database on the mainthread.Mccune
M
7

I found two possible answers for my question.

Solution 1: Start an async task. Create an AsyncTask class and do the stuff in the doBackground method. There are no more problem accessing the database.

@Override
public void onReceive(Context context, Intent intent) {
    new notifyAsyncTask().execute(context);
}

private static class notifyAsyncTask extends AsyncTask<Context, Void, Void> {

        private String CHANNEL_ID = "1a2b3c4d";

        private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        @Override
        protected Void doInBackground(Context... params) {
            Log.i(TAG, "Notify async started");
        ReminderRepository mRepository; = new ReminderRepository(context);
        List<Reminder> list = mRepository.getAllReminder();
        for(Reminder r : list) {
                // TODO
        }
    }
}

Solution 2: Allow queries on the main thread. When the database is build, use allowMainThreadQueries() to allow queries on the main thread.

!!!! ATTENTION !!!! NOT RECOMMENDED !!!! ATTENTION !!!!

Mccune answered 31/8, 2018 at 9:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.