我发现我正在使用已弃用的方法进行通知 (notification.setLatestEventInfo())
它说使用 Notification.Builder。
- 如何使用它?
当我尝试创建一个新实例时,它告诉我:
Notification.Builder cannot be resolved to a type
最佳答案
Notification.Builder API 11或 NotificationCompat.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/