我发现我使用了一个不推荐使用的通知方法(notification.setLatestEventInfo())
上面说要使用Notification.Builder。
我要使用it?吗?
当我尝试创建一个新实例时,它告诉我:
Notification.Builder cannot be resolved to a type
发布于 2011-06-18 05:35:21
This是在API11中的,所以如果你正在为早于3.0的任何东西进行开发,你应该继续使用旧的API。
API : NotificationCompat.Builder类已经添加到支持包中,所以我们可以使用它来支持级别的和更高版本:
http://developer.android.com/reference/android/support/v4/app/NotificationCompat.Builder.html
发布于 2012-01-15 20:35:28
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);
发布于 2013-01-04 12:09:59
除了选择的答案之外,下面是来自Source Tricks的NotificationCompat.Builder
类的一些示例代码:
// Add app running notification
private void addNotification() {
NotificationCompat.Builder builder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Notifications Example")
.setContentText("This is a test notification");
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(contentIntent);
// Add as notification
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(FM_NOTIFICATION_ID, builder.build());
}
// Remove notification
private void removeNotification() {
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.cancel(FM_NOTIFICATION_ID);
}
https://stackoverflow.com/questions/6391870
复制相似问题