Sunday, August 30, 2015

Andoid service not running - Java

If you have created a class extending IntentService, and your service is not getting called, here is the possible cause.

If your Java onHandleIntent function is never called, then you might forgot to add your service to AndroidManifest.xml file.

You will receive no error if the declaration is missing in AndroidManifest.xml, because IntentService is a class just as any other in code.

So, make sure you put declaration of your service in your Android manifest file!


Thursday, August 27, 2015

Android and SQLite - Delete all but top n rows from a database table

After some testing, I have found that in order to delete all but top n rows from an Android SQLite database , you should use SQLiteDatabase::execSQL instead of SQLiteDatabase::rawQuery.

 SQLiteDatabase::execSQL works as expected, and deletes records below limit. Function rawQuery produces no error, but still it does not delete the rows in table.

So, here is a sample code to delete all but n top rows in Android Java / SQLite:

[code]
public void DeleteAllButTopNRows(SQLiteDatabase db, int limit)
{

//this line works:
  db.execSQL("DELETE FROM table WHERE idField NOT IN (SELECT idField FROM table ORDER BY sortField DESC LIMIT " + limit) ;


//this line does not work:
  db.rawQuery("DELETE FROM table WHERE idField NOT IN (SELECT idField FROM table ORDER BY sortField DESC LIMIT " + limit, null) ;



}

[/code]

Hope this helps!

PSR-1 and PSR-2 coding standards for PHP

Visual aspects of code play a significant role in raising or drowning developer's productivity. In case that there's too much clutte...