If I run my onReceive() method with just a simple toast, my app runs. But when I try to perform a loop and then show a toast, my program crashes. Is there something saying that I can’t perform loops in an onReceive() method?
@Override
public void onReceive(Context ctx, Intent intent) {
DBAdapter havetododb = new DBAdapter(ctx);
Cursor cursor = havetododb.fetchAllItems();
if (cursor.moveToFirst()) {
do {
String title = cursor.getString(cursor.getColumnIndex("item"));
Toast.makeText(ctx, title, Toast.LENGTH_LONG).show();
} while (cursor.moveToNext());
}
cursor.close();
}
I tried to search my logcat for errors, but it seems to come up empty.
You can run any code, but if you run for too long on the main thread (I think it is 10 seconds, but it depends on what release) you will get an Application Not Responding (ANR) interruption, and you get killed. That should show up in the log though. Surround your code by a try/catch that catches all and see what you get.
If you run out of time an [IntentService]/http://developer.android.com/reference/android/app/IntentService.html) will help you. It could also be enough to execute in an AsyncTask but that does less effort to keep your process alive.