Java Android应用程序帮助
您好,我已经创建了一个任务提醒应用程序。但是,我无法让它用任务名称通知用户任务。这应该是一件相当简单的事情,但我做不到 我想让它做的是:我保存一个名为“任务3”的任务,并在下午6点提醒我。当下午6点到来时,我收到一个通知警报,上面写着“任务3”。目前,它只说“任务需要检查”。有没有办法检索任务标题并将其显示为通知而不是字符串 这是提醒服务类: 公共提醒服务(){ 超级(“提醒服务”); }Java Android应用程序帮助,java,android,notifications,Java,Android,Notifications,您好,我已经创建了一个任务提醒应用程序。但是,我无法让它用任务名称通知用户任务。这应该是一件相当简单的事情,但我做不到 我想让它做的是:我保存一个名为“任务3”的任务,并在下午6点提醒我。当下午6点到来时,我收到一个通知警报,上面写着“任务3”。目前,它只说“任务需要检查”。有没有办法检索任务标题并将其显示为通知而不是字符串 这是提醒服务类: 公共提醒服务(){ 超级(“提醒服务”); } @Override void doReminderWork(Intent intent) { Lo
@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;
}
}如果我理解正确,问题是您显示的字符串是string.xml中定义的常量字符串。您应该使用从Intent获取的行id从数据库中获取相应的提醒并显示其标题
@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);
当您在提供行id的同时从fetchReminder方法检索光标后,您可以使用以下方法获取提醒的标题:
String reminderTitle = cursor.getString(cursor.getColumnIndex(RemindersDbAdapter.KEY_TITLE));
事先确认光标不为空。HI这是正确的。我试过使用行id,但由于某些原因,它不会显示标题,只显示“id”一词。嗨,谢谢。字符串rementerTitle=cursor.getString(cursor.getColumnIndex(rementersDBAdapter.KEY_TITLE));进入fetchreminder方法内部?否,在生成通知时,必须以行id作为参数调用ReminderBadapterFetchReminder方法。它将在相应的数据库记录上返回一个游标,然后您可以使用我给您的代码行gwt标题。