android - setLatestEventInfo() 无法在 Android Studio 中解析

标签 android notifications android-notifications

我在 Android Studio 中工作并尝试在特定日期和时间生成通知。一切正常,但是,在我的服务类 setLatestEventInfo() 方法中无法解析。 我在 eclipse 中做过同样的演示,eclipse 没有任何问题。 我不想在任何按钮的点击或任何手动事件生成时生成通知,而是在我指定的特定日期和时间生成通知。

服务类代码如下:

public class MyRemiderService extends Service {
private NotificationManager mManager;

@Override
public IBinder onBind(Intent intent) {

    return null;
}

@Override
public void onCreate() {

    super.onCreate();
}

@Override
@Deprecated
public void onStart(Intent intent, int startId) {
    super.onStart(intent, startId);
    mManager = (NotificationManager) getApplicationContext()
            .getSystemService(getApplicationContext().NOTIFICATION_SERVICE);
    Intent intent1 = new Intent(this.getApplicationContext(),
            HomeActivity.class);
    Notification notification = new Notification(R.drawable.notification_template_icon_bg,
            "This is a test message!", System.currentTimeMillis());
    intent1.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP
            | Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingNotificationIntent = PendingIntent.getActivity(
            this.getApplicationContext(), 0, intent1,
            PendingIntent.FLAG_UPDATE_CURRENT);
    notification.flags |= Notification.FLAG_AUTO_CANCEL;
    notification.setLatestEventInfo(this.getApplicationContext(),
            "AlarmManagerDemo", "This is a test message!",
            pendingNotificationIntent);


    mManager.notify(0, notification);
}

@Override
public void onDestroy() {

    super.onDestroy();
}

请让我提供解决方案。谢谢。

最佳答案

如这里所示 setLatestEventInfo :

setLatestEventInfo 方法已从 Notification 类中移除

创建通知使用Notification.Builder分类为:

Notification.Builder builder = new Notification.Builder(MyRemiderService.this);
.....
builder.setSmallIcon(R.drawable. notification_template_icon_bg)
       .setContentTitle("ContentTitle")
       .....
       .setContentIntent(pendingNotificationIntent);

Notification notification = builder.getNotification();
notificationManager.notify(R.drawable.notification_template_icon_bg, notification);

关于android - setLatestEventInfo() 无法在 Android Studio 中解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32564038/

相关文章:

android - 通知区 "clear all"按钮回调

android - 如何停止在每次启动 android 应用程序时收到通知?

android - Cordova "Local Notifications"插件?

java - 仅在订单状态更改时执行该功能

android-notifications - 带有 “null” PendingIntent的通知

android - java.lang.RuntimeException : java. lang.NoSuchMethodException : <init> [class android. view.View]

java - 完全消除对 Android 时钟的依赖并使用来自服务器的自定义时间

Android 在菜单项旁边显示通知编号

Android 语音转文本示例

javascript - 通知 - 替代 Object.observe?