mardi 13 janvier 2015

Query SQLite database from service

I am building an app which works like this:



  • user creates a lot of data which needs to be processed sequentially

  • after he clicks submit, a lot of new rows are added to database and service starts

  • service checks for the first row in database, process the data and sends broadcast on finish

  • brodcastreceiver receives broadcast and deletes that row from database


This is my service:



public class MyService extends Service {
private final IBinder mBinder = new MyBinder();
private ArrayList<String> list = new ArrayList<String>();

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// Should check for database change here?

// fetches data from database
Data data = Manager.get(context).getFirstRowData();
int id = data.getId();

// set extras (row id etc) to intent for broadcast and send broadcast

}

@Override
public IBinder onBind(Intent arg0) {
return mBinder;
}

public class MyBinder extends Binder {
MyService getService() {
return MyService.this;
}
}

}


The service is started like this with alarm manager:



AlarmManager service = (AlarmManager) context
.getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(context, MyStartServiceReceiver.class);
PendingIntent pending = PendingIntent.getBroadcast(context, 0, i,
PendingIntent.FLAG_CANCEL_CURRENT);
Calendar cal = Calendar.getInstance();
// start 30 seconds after boot completed
cal.add(Calendar.SECOND, 30);
// fetch every 30 seconds
// InexactRepeating allows Android to optimize the energy consumption
service.setInexactRepeating(AlarmManager.RTC_WAKEUP,
cal.getTimeInMillis(), REPEAT_TIME, pending);


And this actually starts the service:



public class MyStartServiceReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
Intent service = new Intent(context, MyService.class);
context.startService(service);
}
}


This is how I get first row inside SQLiteOpenHelper:



Cursor cursor = db.query(TABLE_NAME, new String[] { "*" },
null,
null, null, null, null, null);

if(cursor != null)
{
if (cursor.moveToFirst()) {
arrData = new String[cursor.getColumnCount()];

arrData[0] = cursor.getString(0); // DeviceID
arrData[1] = cursor.getString(1); // EmailID
arrData[2] = cursor.getString(2); // Event
arrData[3] = cursor.getString(3); // Operator
arrData[4] = cursor.getString(4); // EventOperator
}
}

cursor.close();


The problem is that after I receive broadcast that data is processed and delete that table row inside my BroadcastReceiver, I am again receiving the same row when service starts again. It's always the same row. So, deletion works only once. After I try again to fetch first row from table, I will again receive the row which has id 1.


What is wrong in this process? It successfully gets the first row first time, passes data to BrodcastReceiver with that row id, BroadcastReceiver deletes that row and row is not present anymore in the database. But when service starts again, like the row is still there.


Aucun commentaire:

Enregistrer un commentaire