0

我一直在这个问题上停留了一段时间。

有多个数据库光标的广播接收器有什么好的例子吗?

问题:我已经实现了 PagerTabStrip,还有广播接收器和提醒通知。

所以当我点击设备屏幕上的通知时,它只打开第一个光标,它不会打开另一个。我很确定,我已经关闭了我的光标。

这只是打开了没有我想要的东西的空白活动。

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();
        }
4

1 回答 1

0

了解您的问题后,我很确定 fetchAllGeneralRemindersByDefault() 中存在错误。它正在返回一个空游标。如果这是因为代码或数据库为空,我无法判断。

代码重构建议:

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");
}
}
于 2013-09-28T21:54:45.510 回答