【问题标题】:Why does Eclipse not recognize the build() method from the Notification.Builder class to return a Notification object?为什么 Eclipse 无法识别 Notification.Builder 类中的 build() 方法来返回 Notification 对象?
【发布时间】:2012-07-07 03:35:45
【问题描述】:

这是我的代码:


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; 表示它没有被使用。

【问题讨论】:

  • 您的目标是 API 16 吗? build() 是果冻豆的新成员。
  • 哈哈,不是JellyBean。集成电路。如果您不介意回答与此问题无关的快速问题,请附带说明。 ICS 中的大多数 API 是否都适用于 JellyBean。我问是因为我想升级到新的操作系统并尝试一下,但如果我的开发手机不支持我正在构建我的应用程序的话。
  • 任何不在 Jelly Bean 中的 ICS 中的 API 只会被弃用,因此它们在 API 16 中仍然可以安全使用。但我会在升级之前在 4.1 模拟器上运行您的应用程序你的设备……以防万一。

标签: android eclipse deprecated notificationmanager


【解决方案1】:

如果您想为版本 SDK 低于 11 进行开发,您可以使用此代码代替 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);
    }
  }
}

【讨论】:

  • 管道和等号是什么意思|=。它是java特定的吗?不过谢谢。
  • 是的。它是 Java 中的逻辑运算符之一。 按位包含 OR ( | ) 运算符对两个操作数的每个并行位对执行按位包含 OR 运算。在每一对中,如果第一位或第二位为 1(或两者均为 1),则结果为 1。否则结果为 0。当与 x |= y 之类的复合语句一起使用时,将被读取为 x = x |是
【解决方案2】:

这仅在您针对不包含所需 method/api 的版本(在您的情况下为 build())编译您的应用程序代码时发生。

如果您正在编译的版本中没有 build,我建议您使用更高版本的 Android 进行编译,为了向后兼容,您始终在 manifest 中有 最低 sdk 版本

谢谢

【讨论】:

  • 但是我的sdk版本是ICS,应该有这个。虽然我明白你在说什么。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-02
  • 2023-03-14
相关资源
最近更新 更多