Method onReceive(
) is running on the main application thread and you do not know how long insertInDatabase()
will take.
Use an IntentService
for doing the database insert and scheduling your alarms. The IntentService
will call your onHandleIntent()
on a background thread, so you can take the time you need, and the service automatically goes away when onHandleIntent()
completes.
public class MyIntentService extends IntentService {
@Override
protected void onHandleIntent(Intent intent) {
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
final WakeLock wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, wakelockName);
wakeLock.acquire();
wl.acquire();
try {
SQLiteDatabase db = helper.getWritableDatabase();
ContentValues cv = new ContentValues();
// insert data in database here
} finally {
wakeLock.release();
}
}
}
To start IntentService
from BroadcastReceiver
use AlarmManager
.
IntentService
work as queue pattern and you don't need to worry about synchronicity of operations.
Added after discussion in comments:
Your code example does not say from what place you call PARTIAL_WAKE_LOCK
. The short answer is PARTIAL_WAKE_LOCK
is not needed in the BroadcastReciever
and PARTIAL_WAKE_LOCK
is needed in the IntentService
. Maybe this will help BroadcastReceiver, Service and Wakelock