当前位置: 首页 > 知识库问答 >
问题:

未为类型通知定义方法setLatestEventInfo(myService,String,String,PendingIntent)

史承福
2023-03-14

此代码用于工作...

public void displayNotification(String msg) { 
    NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    Notification notification = new Notification(R.drawable.aow, msg, System.currentTimeMillis());
    // The PendingIntent will launch activity if the user selects this
    // notification
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
            new Intent(this,
                    com.redcricket.whale_ship_essex_banner.whale_ship_essex_bannerActivity.class),
            0);

    notification.setLatestEventInfo(this, getResources().getString(R.string.app_name),
            com.redcricket.whale_ship_essex_banner.whale_ship_essex_bannerActivity.track_titles[ currentTrack ], contentIntent);

    manager.notify(0, notification);
}

...但是现在我得到这些错误消息:

The constructor Notification(int, CharSequence, long) is deprecated
The method setLatestEventInfo(whale_ship_essex_bannerService, String, String, PendingIntent) is undefined for the type Notification

有人能告诉我需要修改什么或者一些关于如何重写此代码的文档吗?

谢了!

共有1个答案

牟正真
2023-03-14

您应该使用NotificationBuilder而不是构造函数。

NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setContentTitle("Some title")
            .setSmallIcon(R.drawable.ic_launcher);
Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
builder.setLargeIcon(bm);
Notification notification = builder.build();
 类似资料: