0

我有以下通知代码。

public void sendBasicNotification(View view) {
    Notification notification = new Notification.Builder(this)
        .setContentTitle("Basic Notification")
        .setContentText("Basic Notification, used earlier")
        .setSmallIcon(R.drawable.ic_launcher_share).build();
    notification.flags |= Notification.FLAG_AUTO_CANCEL;
    NotificationManager notificationManager = getNotificationManager();
    notificationManager.notify(0, notification);
 }

上面的代码仅适用于 min Sdk 16。我如何为低于 16 的版本编写代码?我应该完全编写不同的代码吗?我该怎么办?

4

2 回答 2

0

使用NotificationCompat.BuilderAndroid 支持包中的 ,它适用于 API 级别 4 及更高级别。

于 2013-05-15T13:28:47.750 回答
0

不要使用Builder.

public void sendBasicNotification(View view) {
    Notification notification = new Notification(R.drawable.ic_launcher_share, "Status message!", System.currentTimeMillis());

    notification.flags |= Notification.FLAG_AUTO_CANCEL;
    // you can use different constructors or setLatestEventInfo method to set text, title etc. But it is deprecated after api level 11
    PendingIntent i = PendingIntent.getActivity(this, 0, new Intent(this, "Your push activity class"), 0);
notification.setLatestEventInfo(this, "Notification Title", "This is the notification message", i);
    NotificationManager notificationManager = getNotificationManager();
    notificationManager.notify(0, notification);
}
于 2013-05-15T13:38:15.340 回答