我正在尝试为我的应用程序实现提醒功能(API 23)。
但是,提醒功能包含一个错误,无法解决“setLatestEventInfo”方法。
我做了一些研究,结果发现这种方法在 API 23 中已被弃用。我知道那里也有类似的问题,但解决方案对我不起作用。
相关代码如下:
public class ReminderService extends WakeReminderIntentService {
public ReminderService() {
super("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 note=new Notification(android.R.drawable.stat_sys_warning, getString(R.string.notify_new_task_message), System.currentTimeMillis());
note.setLatestEventInfo(this, getString(R.string.notify_new_task_title), getString(R.string.notify_new_task_message), pi);
note.defaults |= Notification.DEFAULT_SOUND;
note.flags |= Notification.FLAG_AUTO_CANCEL;
// An issue could occur if user ever enters over 2,147,483,647 tasks. (Max int value).
// I highly doubt this will ever happen. But is good to note.
int id = (int)((long)rowId);
mgr.notify(id, note);
}
}
如何在不降低 API 级别的情况下解决此问题?
最佳答案
使用NotificationCompat.Builder构建您的通知,如 documentation 所示.
您可以检查方法 setTicker
、setSmallIcon
、setContentTitle
、setContentText
、setContentIntent
、setAutoCancel
、setDefaults
来重现您的行为。
关于java - setLatestEventInfo 方法无法解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35570767/