Search code examples
androidsqlitebroadcastreceiver

Storing incoming SMS from a particular no into sqlite DB


I am using BroadcastReceiver to listen to the incoming message and to display the message in the WebView if it is from a particular no,I have implemented this part...now how can I store this message from the particular sender into the SQLite Database on a Button click event...thanks in advance


Solution

  • Well, like you would save any data in a database. Read this: http://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html

    You can also use an ORM to make everything easier so you won't have to work with databases yourself. Sugar ORM is a good option for android developers: http://satyan.github.io/sugar/


    EDIT:

    In order to create your own Database and handle the creation of tables and such yourself, you have to create a class that extends SQLiteOpenHelper which I haven't worked with that much. But you can see a sample code here:

    http://paste.ubuntu.com/11695543/

    And if you want to use Sugar ORM, you have to add its library to your project and your data classes, the ones you want to save in the database, should now extend SugarRecord. It's all in the documentation. For example:

    public class Box extends SugarRecord<Box> {
    
    public int picID;
    public int lockedPicId;
    public int isBought;
    public int id;
    public int numberOfLevels;
    public String packageName;
    
    
    public int getNumberOfPassed(){
        ArrayList<Level> levels = (ArrayList<Level>) Level.find(Level.class, "level_Box = ?", this.getId() + "");
        int res = 0;
        for(int i = 0; i < levels.size(); i++){
            if(levels.get(i).isFinished == 1)
                res++;
        }
        return res;
    }
    public Box(int id, int numberOfLevels, String packageName, int isBought, int picID, int lockedPicId) {
        this.id = id;
        this.numberOfLevels = numberOfLevels;
        this.packageName = packageName;
        this.isBought = isBought;
        this.picID = picID;
        this.lockedPicId = lockedPicId;
    }
    
    public Box() {
    
    }
    
    }
    

    So from now on, if you wanna save an object you just write:

    Box box = new Box();
    //do anything you want here.
    box.save();
    

    That's it. For retrieving them, you can read the documentation.