android - 为什么 Eclipse 无法识别 Notification.Builder 类中的 build() 方法来返回 Notification 对象?

标签 android eclipse deprecated notificationmanager

这是我的代码:


NotificationManager mNotificationManager = (NotificationManager) c.getSystemService(ns);

    //Instantiate the notification

    CharSequence tickerText = "Hello";
    long when = System.currentTimeMillis();
    Notification.Builder builder = new Notification.Builder(c)
                                .setTicker(tickerText)
                                .setWhen(when)
                                .setContentTitle("Test Notification")
                                .setContentText(arg1.getStringExtra("info"))
                                .setSmallIcon(R.drawable.ic_launcher)
                                .setAutoCancel(true);
    Notification notification = builder.getNotification();
    mNotificationManager.notify(88, notification);

它可以找到,但不推荐使用 Notification notification = builder.getNotification();。因为我应该这样做 Notification notification = builder.build();。 问题是 Eclipse 没有识别它,这意味着它不会让我编译。该文档清楚地表明 build() 存在并且它是首选方法,但它对我不起作用。我想使用未弃用的代码,因此非常感谢任何帮助。

导入


import android.app.Notification;
import android.app.Notification.Builder;
import android.app.NotificationManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

请注意 import android.app.Notification.Builder; 表示它未被使用。

最佳答案

如果你想开发低于 11版本的SDK,你可以使用这段代码代替android.app.Notification.Builder创建通知类:

private void createNotification(){
    NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    Notification notification = new Notification(R.drawable.ic_launcher, "Hello", System.currentTimeMillis());
    Intent notificationIntent = new Intent(this, YourActivity.class);

    Random r = new Random();
    int notificationId = r.nextInt();
    notificationIntent.putExtra("n_id", notificationId);
    PendingIntent contentIntent = PendingIntent.getActivity(this, notificationId, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    notification.defaults |= Notification.DEFAULT_SOUND;
    notification.setLatestEventInfo(this, "Party", "Welcome!", contentIntent);
    mNotificationManager.notify(notificationId, notification);      
}

您可以像这样在 YourActivity 中取消此通知:

public class YourActivity extends Activity{ 

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.your_activity);

    int notificationId = getIntent().getIntExtra("n_id", -1);

    if (notificationId!=-1) {
        NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        mNotificationManager.cancel(notificationId);
    }
  }
}

关于android - 为什么 Eclipse 无法识别 Notification.Builder 类中的 build() 方法来返回 Notification 对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11372055/

相关文章:

Java 函数正在更新其输入变量

java - 覆盖已弃用的方法是不好的做法吗?

android - 在 Android 4.2 快速设置面板中添加项目

java - 如何在 Java 中创建一个包含其他数组数据的新数组?

css - 如何在 Eclipse 中链接 CSS 文件夹和图像文件夹?

java - 弃用 Java JRE 方法

flutter - 插件 `XXXXX` 使用不推荐使用的 Android 嵌入版本

android - 如何使用毕加索在谷歌地图上的信息窗口中获取图像 - Android

android - 尽管在 ExoPlayer 中添加了 ConcatenatingMediaSouce,但时间轴为空

android - Android App 无法播放 "opus"文件