2011-05-08 61 views
0

嗨我已经创建了一个任务提醒应用程序。但是,我无法让它通知任务名称的任务的用户。这应该是一件相当简单的事情,但我无法做到。Android应用帮助

我想要做的是:我保存一个名为“任务3”的任务并在下午6点提醒我。当下午6点到来时,我收到一条通知提示“任务3”。目前,它只是说“任务​​需要审查”有没有办法检索任务标题并将其作为通知而不是字符串呈现?

这是提醒服务类类:

公共ReminderService(){ 超( “ReminderService”); }

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

    NotificationManager mgr = (NotificationManager)getSystemService(NOTIFICATION_SERVICE); 

    Intent notificationIntent = new Intent(this, ReminderEditActivity.class);     
    notificationIntent.putExtra(RemindersDbAdapter.KEY_ROWID, rowId); 

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

//这是我需要改变的方式。

Notification notification=new Notification(android.R.drawable.stat_sys_warning, getString(R.string.notify_new_task_message), System.currentTimeMillis()); 
    notification.setLatestEventInfo(this, getString(R.string.notify_new_task_title), getString(R.string.notify_new_task_message), pi); 

,这是remindersadapter类

* @param reminderDateTime the date and time the reminder should remind the user 
* @return rowId or -1 if failed 
*/ 
public long createReminder(String title, String body, String reminderDateTime) { 
    ContentValues initialValues = new ContentValues(); 
    initialValues.put(KEY_TITLE, title); 
    initialValues.put(KEY_BODY, body); 
    initialValues.put(KEY_DATE_TIME, reminderDateTime); 

    return mDb.insert(DATABASE_TABLE, null, initialValues); 
} 

/** 
* Delete the reminder with the given rowId 
* 
* @param rowId id of reminder to delete 
* @return true if deleted, false otherwise 
*/ 
public boolean deleteReminder(long rowId) { 

    return mDb.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) > 0; 
} 

/** 
* Return a Cursor over the list of all reminders in the database 
* 
* @return Cursor over all reminders 
*/ 
public Cursor fetchAllReminders() { 

    return mDb.query(DATABASE_TABLE, new String[] {KEY_ROWID, KEY_TITLE, 
      KEY_BODY, KEY_DATE_TIME}, null, null, null, null, null); 
} 

/** 
* Return a Cursor positioned at the reminder that matches the given rowId 
* 
* @param rowId id of reminder to retrieve 
* @return Cursor positioned to matching reminder, if found 
* @throws SQLException if reminder could not be found/retrieved 
*/ 
public Cursor fetchReminder(long rowId) throws SQLException { 

    Cursor mCursor = 

      mDb.query(true, DATABASE_TABLE, new String[] {KEY_ROWID, 
        KEY_TITLE, KEY_BODY, KEY_DATE_TIME}, KEY_ROWID + "=" + rowId, null, 
        null, null, null, null); 
    if (mCursor != null) { 
     mCursor.moveToFirst(); 
    } 
    return mCursor; 

} 

/** 
* Update the reminder using the details provided. The reminder to be updated is 
* specified using the rowId, and it is altered to use the title, body and reminder date time 
* values passed in 
* 
* @param rowId id of reminder to update 
* @param title value to set reminder title to 
* @param body value to set reminder body to 
* @param reminderDateTime value to set the reminder time. 
* @return true if the reminder was successfully updated, false otherwise 
*/ 
public boolean updateReminder(long rowId, String title, String body, String reminderDateTime) { 
    ContentValues args = new ContentValues(); 
    args.put(KEY_TITLE, title); 
    args.put(KEY_BODY, body); 
    args.put(KEY_DATE_TIME, reminderDateTime); 

    return mDb.update(DATABASE_TABLE, args, KEY_ROWID + "=" + rowId, null) > 0; 
} 

}

回答

0

如果我理解正确的,问题是你显示你的string.xml定义的常量字符串。您应该使用从Intent获取的行ID从您的数据库中获取相应的提醒并显示其标题。

一旦你从你的fetchReminder方法检索光标同时提供行ID,您可以使用让您的提醒标题:

String reminderTitle = cursor.getString(cursor.getColumnIndex(RemindersDbAdapter.KEY_TITLE)); 

验证你的光标不是null提前。

+0

你好,这是正确的。我尝试使用行ID,但由于某种原因,不会显示标题,只是单词“ID”。 – Superunknown 2011-05-08 19:18:17

+0

在上面的答案中查看我的更新。 – Moystard 2011-05-08 19:52:15

+0

嗨,谢谢。字符串提示是否= cursor.getString(cursor.getColumnIndex(RemindersDbAdapter.KEY_TITLE)); 去fetchreminder方法?因为“光标”无法解析,因此请将其替换为“ – Superunknown 2011-05-08 20:31:12