Reputation: 568
I'm beginner in android,and want to open the database,in this code:
public class AlarmReciever extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
try {
SQLiteDatabase db;
db=SQLiteDatabase.openOrCreateDatabase(DATABASE_NAME,context.MODE_PRIVATE, null);
//...
}catch(Exception e)
{
}
}
in this line:
db=SQLiteDatabase.openOrCreateDatabase(DATABASE_NAME,myContext.MODE_PRIVATE, null);
How can i solve that?thanks.
Upvotes: 0
Views: 315
Reputation: 132992
Use context
which is first parameter in onReceive
for calling openOrCreateDatabase
method as:
db=SQLiteDatabase.openOrCreateDatabase(DATABASE_NAME,
context.MODE_PRIVATE, null);
Why i can not open sqlite database in android BroadcastReceiver?
Because broadcast life is very less so it's not possible to perform long running tasks in onReceive
method like API calls, DB operations or any other task which taking more time.
Suggestion : Instead of doing task inside onReceive
use IntentService which will do task for application and stop self when task complete.
Upvotes: 1