广播接收器用于多个数据库Cursor

时间:2013-09-28 21:04:34

标签: android cursor broadcastreceiver

我一直坚持这个问题。

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

问题: 我已经实现了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);

///其余的COED。

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

1 个答案:

答案 0 :(得分: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");
}
}