android - 究竟如何使用 Notification.Builder

标签 android api notifications deprecated

我发现我正在使用已弃用的方法进行通知 (notification.setLatestEventInfo())

它说使用 Notification.Builder。

  • 如何使用它?

当我尝试创建一个新实例时,它告诉我:

Notification.Builder cannot be resolved to a type

最佳答案

Notification.Builder API 11NotificationCompat.Builder API 1

这是一个使用示例。

Intent notificationIntent = new Intent(ctx, YourClass.class);
PendingIntent contentIntent = PendingIntent.getActivity(ctx,
        YOUR_PI_REQ_CODE, notificationIntent,
        PendingIntent.FLAG_CANCEL_CURRENT);

NotificationManager nm = (NotificationManager) ctx
        .getSystemService(Context.NOTIFICATION_SERVICE);

Resources res = ctx.getResources();
Notification.Builder builder = new Notification.Builder(ctx);

builder.setContentIntent(contentIntent)
            .setSmallIcon(R.drawable.some_img)
            .setLargeIcon(BitmapFactory.decodeResource(res, R.drawable.some_big_img))
            .setTicker(res.getString(R.string.your_ticker))
            .setWhen(System.currentTimeMillis())
            .setAutoCancel(true)
            .setContentTitle(res.getString(R.string.your_notif_title))
            .setContentText(res.getString(R.string.your_notif_text));
Notification n = builder.build();

nm.notify(YOUR_NOTIF_ID, n);

关于android - 究竟如何使用 Notification.Builder,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6391870/

相关文章:

android - 重命名应用程序和新应用程序图标 - 最佳做法是什么?

java - 从 SQLite 数据库加载数据到 ListView

api - 自动关闭和启动 Amazon EC2 实例

c# - 是否有用于 VMware VIX API 的 C# 包装器?

android - 如何设置动态小图标通知

java - Android:如何在 sharedpreferences 中存储和检索 Hashmap 数据?

java - Android - 传感器或监听器为空

android - 适用于 Android 和 IOS 的乐谱 API?

c++ - 如何在 SQLite3 中使用 Qt QSqlDriver::subscribeToNotification?

javascript - 通过简单地轮询网络服务器来浏览器推送通知