Reputation: 1783
I have created a class to manage a SQLite database. Part of its code is as follows:
public class Database extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "name";
private static final int DATABASE_VERSION = 1;
public Database (Context ctx) {
super(ctx, DATABASE_NAME, null, DATABASE_VERSION);
}
Then I have created another class, which is an SMS listener (extending BroadcastReceiver
). Inside of this class I cannot create a database with the following command:
db = new Database(this);
because I get this error message:
The constructor Database(SmsListener) is undefined
How should I change the code to be able to open a database connection from inside the SmsListener
class?
Upvotes: 1
Views: 2605
Reputation: 83557
Your Database
constructor takes a Context
as its only parameter. The error message tells you that this
is an SmsListener
which does not extend Context
.
Upvotes: 0
Reputation: 36449
Your onReceive()
method in the BroadcastReceiver
gets a Context
passed, use that.
@Override
public void onReceive(Context context, Intent intent){
db = new Database(context);
//more stuff
}
Also be aware that inside a BroadcastReceiver
, you are only guaranteed 10 seconds of execution time, after that, Android may kill your Receiver. So be quick with what you're doing and if the database operation is lengthy, consider using a separate Thread
.
Upvotes: 6