Reputation: 284
I am creating an Android app in which i will receive SMS and store it in SQlite database. i have achieved database functionalities i.e insert, update, delete and show etc. Here is my code:
public void addRecord(String number){
ContentValues values = new ContentValues();
values.put(COLUMN_BLOCKEDLIST, number);
SQLiteDatabase db = getWritableDatabase();
db.insert(TABLE_BLOCKEDLIST, null,values);
db.close();
}
//delete record from database
public void deletRecord(String nummber){
SQLiteDatabase db = getWritableDatabase();
db.execSQL("DELETE FROM " + TABLE_BLOCKEDLIST + " WHERE " + COLUMN_BLOCKEDLIST + "=\"" + nummber + "\";");
db.close();
}
//show all records from database
public String showAllRecords(){
String allRecords = "";
SQLiteDatabase db = getWritableDatabase();
String query = "SELECT * FROM " + TABLE_BLOCKEDLIST + " WHERE 1";
Cursor cursor = db.rawQuery(query, null);
cursor.moveToFirst();
while (cursor.moveToNext()){
allRecords += cursor.getString(0) + "\n";
}
db.close();
return allRecords;
}
now i need the functionally to delete every SMS after 24 hour of its insertion time. Any help will be appreciated.
Upvotes: 1
Views: 2594
Reputation: 304
Well, you could insert a column INSERTION_TIME in the TABLE_BLOCKEDLIST database when you will create a Date instance of insertion, and compare it with the current date. If it's longer than 24 hours, delete it. If you receive twice or more in 24 hours, you can do this check when calling methods addRecord, deleteRecord or showAllRecords. If you want your app to check the database for such records in, let's say, 5 minutes, you can create a long running service that will implement this functionality (BroadcastReceiver).
Upvotes: 0
Reputation: 13348
when you received sms get the current time then minus the 24 hours from current time(get the time value).Then perform delete operation when time is before on that value.(write code SMS onreceive method).
Thats its. when you received sms its automatically delete the old message that before 24 hours.if some case you not receive any sms for few days its not deleted so call the same method code in application activity oncreate also..
Upvotes: 1