2013-09-28 5 views
0

Я застрял на этой проблеме некоторое время.Широковещательный приемник для нескольких баз данных Курсор

Есть ли хороший пример для широковещательного приемника с несколькими курсорами базы данных?

ПРОБЛЕМА: Я реализовал PagerTabStrip, а также приемник BroadCast и уведомление для напоминания.

Так что, когда я нажимаю на уведомлении на экране устройства, он открывает только первый курсор, он тоже не открывает другого. Я уверен, что я закрыл свои курсоры.

ЭТО ТОЛЬКО ОТКРЫВАЕТ ЗАЯВЛЕНИЕ БЕСПЛАТНО БЕСПЛАТНО.

public class ReminderService extends WakeReminderIntentService{ 

public ReminderService(){ 
    super("ReminderService"); 
} 

@SuppressWarnings("deprecation") 
void doReminderWork(Intent intent){ 
    Log.d("ReminderService", "Doing work."); 
    Long rowId = intent.getExtras().getLong(TaskDatabase.KEY_ROWID); 

    NotificationManager mgr = (NotificationManager)getSystemService(NOTIFICATION_SERVICE); 

    Intent notificationIntent = new Intent(this, TaskEdit.class); 
    notificationIntent.putExtra(TaskDatabase.KEY_ROWID, rowId); 

    PendingIntent pi = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_ONE_SHOT); 

/// Остальные страны.

BroadcastReceiver (этот класс получает курсор)

public void onReceive(Context context, Intent intent){ 
    ReminderManager reminderMgr = new ReminderManager(context); 

    TaskDatabase dbHelper = new TaskDatabase(context); 
    dbHelper.open(); 
    Cursor cursor = dbHelper.fetchAllGeneralRemindersByDefault(); 
    if(cursor != null){ 

     cursor.moveToFirst(); 
     int rowIdColumnIndex = cursor.getColumnIndex(TaskDatabase.KEY_ROWID); 
     int dateTimeColumnIndex = cursor.getColumnIndex(TaskDatabase.KEY_DATE_TIME); 

     while(cursor.isAfterLast() == false){ 
      Log.d(TAG, "Adding alarm from boot."); 
      Log.d(TAG, "Row Id Column Index - " + rowIdColumnIndex); 
      Log.d(TAG, "Date Time Column Index - " + dateTimeColumnIndex); 

      Long rowId = cursor.getLong(rowIdColumnIndex); 
      String dateTime = cursor.getString(dateTimeColumnIndex); 

      Calendar cal = Calendar.getInstance(); 
      SimpleDateFormat format = new SimpleDateFormat(TaskEdit.DATE_TIME_FORMAT); 
      try{ 
       java.util.Date date = format.parse(dateTime); 
       cal.setTime(date); 

       reminderMgr.setReminder(rowId, cal); 
      }catch(java.text.ParseException e){ 
       Log.e("OnBootReceiver", e.getMessage(), e); 
      } 
      cursor.moveToNext(); 
     } 
+0

В чем проблема? – cYrixmorten

+0

вопрос обновлен –

+0

вы можете показать код, который обрабатывает уведомление, нажмите? – cYrixmorten

ответ

0

С пониманием вашего вопроса на месте, я почти уверен, есть ошибка в fetchAllGeneralRemindersByDefault(). Он возвращает en пустой курсор. Если это из-за кода или пустой базы данных, я не могу сказать.

Предложение для кода реорганизовать:

public void onReceive(Context context, Intent intent){ 
ReminderManager reminderMgr = new ReminderManager(context); 

TaskDatabase dbHelper = new TaskDatabase(context); 
dbHelper.open(); 
// returns an empty cursor at index -1 (that is normal behaviour for cursors) 
Cursor cursor = dbHelper.fetchAllGeneralRemindersByDefault(); 
if(cursor != null && cursor.size() > 0){ // added check 

    int rowIdColumnIndex = cursor.getColumnIndex(TaskDatabase.KEY_ROWID); 
    int dateTimeColumnIndex = cursor.getColumnIndex(TaskDatabase.KEY_DATE_TIME); 

    // when you called moveToNext on the empty cursor 
    // it corresponds to calling list.get(0) on an empty ArrayList 
    while(cursor.moveToNext()){ 
     Log.d(TAG, "Adding alarm from boot."); 
     Log.d(TAG, "Row Id Column Index - " + rowIdColumnIndex); 
     Log.d(TAG, "Date Time Column Index - " + dateTimeColumnIndex); 

     Long rowId = cursor.getLong(rowIdColumnIndex); 
     String dateTime = cursor.getString(dateTimeColumnIndex); 

     Calendar cal = Calendar.getInstance(); 
     SimpleDateFormat format = new SimpleDateFormat(TaskEdit.DATE_TIME_FORMAT); 
     try{ 
      java.util.Date date = format.parse(dateTime); 
      cal.setTime(date); 

      reminderMgr.setReminder(rowId, cal); 
     }catch(java.text.ParseException e){ 
      Log.e("OnBootReceiver", e.getMessage(), e); 
     } 
    } 
} else { 
    Log.e("OnBootReceiver", "fetchAllGeneralRemindersByDefault() returned empty cursor"); 
} 
} 
+0

эй, я, наконец, понял, это не ошибка в базе данных. Какой бы код я ни работал, я просто передал значение через Intent, и это сработало. Эта проблема была сложной, я не мог объяснить ничего лучшего, чем то, что я сделал. Но спасибо за ваши усилия и время. Спасибо –

+0

Добро пожаловать :-) – cYrixmorten

Смежные вопросы