Answer the question
In order to leave comments, you need to log in
SQLite query does not get updated data from database in Service. Why?
I launch the application, it immediately launches this service in the main activity, which checks for new plans. The first time the check is successful, it finds a new one. Then I add a plan through another activity, it returns to the main activity where it finds a new plan in the list, but the service does not see it (unloads it as before, without a new entry), although here it runs a check from beeper in the loop. I've already tried everything and nothing. Save :(
public class PlansCheckingService extends Service {
public static boolean isNewPlan = false;
private Looper serviceLooper;
private ServiceHandler serviceHandler;
private final class ServiceHandler extends Handler {
private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
private final String selection = ScheduleContract.PlansEntry.COLUMN_NAME_END_DATE + " < "
+ Long.toString(System.currentTimeMillis()) + " AND "
+ ScheduleContract.PlansEntry.COLUMN_NAME_COMPLITED + " = "
+ ScheduleContract.PlansEntry.COMPLITED_STAGES_NOT_CHECKED;
private final String sortOrder = ScheduleContract.PlansEntry._ID + " DESC";
private int lastPlanId = 0;
private int newPlanId = 0;
public ServiceHandler(Looper looper) {
super(looper);
}
@Override
public synchronized void handleMessage(Message message) {
final Runnable beeper = new Runnable() {
@Override
public void run() {
ScheduleDbHelper helper = new ScheduleDbHelper(getApplicationContext());
SQLiteDatabase db = helper.getReadableDatabase();
Cursor lastFoundPlan;
Cursor newPlans;
// db.beginTransaction();
lastFoundPlan = db.query(
ScheduleContract.PlansEntry.TABLE_NAME,
null,
selection,
null,
null,
null,
sortOrder,
"1"
);
if (lastFoundPlan.getCount() > 0) {
lastFoundPlan.moveToFirst();
Log.v("App", "cursor more");
newPlanId = lastFoundPlan.getInt(
lastFoundPlan.getColumnIndex(ScheduleContract.PlansEntry._ID)
);
Log.v("lastplanid", lastPlanId + "");
Log.v("newplanid", newPlanId + "");
if (newPlanId > lastPlanId) {
newPlans = db.query(
ScheduleContract.PlansEntry.TABLE_NAME,
null,
selection,
null,
null,
null,
sortOrder
);
Intent localIntent = new Intent(ScheduleContract.PLANSCHECKING_BROADCAST_ACTION);
localIntent.putExtra("number", newPlans.getCount());
Log.v("App", "service new plan");
try {
sendBroadcast(localIntent);
} catch (Exception e) {
}
lastPlanId = newPlanId;
newPlans.close();
}
}
lastFoundPlan.close();
// db.endTransaction();
db.close();
helper.close();
}
};
final ScheduledFuture handler = scheduler.scheduleWithFixedDelay(beeper, 0, 5, TimeUnit.SECONDS);
}
}
@Override
public void onCreate() {
HandlerThread thread = new HandlerThread("ServiceStartArguments",
Process.THREAD_PRIORITY_BACKGROUND);
thread.start();
serviceLooper = thread.getLooper();
serviceHandler = new ServiceHandler(serviceLooper);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.v("APP", "Service start");
Message message = serviceHandler.obtainMessage();
message.arg1 = startId;
serviceHandler.sendMessage(message);
return START_STICKY;
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onDestroy() {
Log.v("destroy", "destroy");
}
}
Answer the question
In order to leave comments, you need to log in
something strange you have here.
Do I understand correctly that you just need to display the newest entry from the database, while the database can be updated either in another activity, or somewhere in the service?
if so, use CoontentProvider and CursorLoader, you don’t need to pull the database every time to check it’s all there out of the box
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question