java - setLatestEventInfo 方法无法解析

标签 java android android-notifications

我正在尝试为我的应用程序实现提醒功能(API 23)。

但是,提醒功能包含一个错误,无法解决“setLatestEventInfo”方法。

enter image description here

我做了一些研究,结果发现这种方法在 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 所示.

您可以检查方法 setTickersetSmallIconsetContentTitlesetContentTextsetContentIntentsetAutoCancelsetDefaults 来重现您的行为。

关于java - setLatestEventInfo 方法无法解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35570767/

相关文章:

java - Android Java JNI 和 C Char 数组无法识别值

android - 您如何做到这一点,以便在流程结束时,正在进行的通知振动并播放声音?

java - PaintComponent() 绘制图形,但即使调用 repaint() 也不使用更新的值

java - 将 java.sql.Timestamp 保存到 mysql 日期时间列会更改时间

android - 错误 : Failed to resolve: com. afollestad :material-dialogs:2. 0.0

java - 单击 Firebase 通知时打开 MainActivity 以外的其他 Activity

java - 没有大括号的 if 子句出现奇怪的编译器错误

java - 如何开发在部署时请求数据库凭据的 Java Web 服务?

Android:是否可以将指纹从一台设备复制到另一台设备?