首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何准确地使用Notification.Builder

如何准确地使用Notification.Builder
EN

Stack Overflow用户
提问于 2011-06-18 05:08:05
回答 11查看 167.7K关注 0票数 102

我发现我使用了一个不推荐使用的通知方法(notification.setLatestEventInfo())

上面说要使用Notification.Builder。

我要使用it?吗?

  • How

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

代码语言:javascript
复制
Notification.Builder cannot be resolved to a type
EN

回答 11

Stack Overflow用户

回答已采纳

发布于 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

票数 86
EN

Stack Overflow用户

发布于 2012-01-15 20:35:28

Notification.Builder API 11NotificationCompat.Builder API 1

这是一个使用示例。

代码语言:javascript
复制
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);
票数 152
EN

Stack Overflow用户

发布于 2013-01-04 12:09:59

除了选择的答案之外,下面是来自Source TricksNotificationCompat.Builder类的一些示例代码:

代码语言:javascript
复制
// 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);  
}  
票数 70
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6391870

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档