您好我已经创建了一个任务提醒应用程序。但是我无法通过任务名称通知用户该任务。这应该是一件相当简单的事情,但我无法做到。
我想要它做的是:我保存一个名为“任务3”的任务,并提醒我下午6点。当下午6点到来时,我收到一条通知警告“任务3”。目前,它只是说“需要审查任务”是否有办法检索任务标题并将其作为通知而不是字符串显示?
这是提醒服务类类:
public 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);
这是提醒适配器类
* @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 :(得分:0)
如果我理解正确,问题是你显示在string.xml中定义的常量字符串。您应该使用从Intent获取的行ID来从您的数据库中获取相应的提醒并显示其标题。
在提供行ID时,从fetchReminder方法中检索光标,您可以使用以下命令获取提醒的标题:
String reminderTitle = cursor.getString(cursor.getColumnIndex(RemindersDbAdapter.KEY_TITLE));
事先验证光标是否为空。