安卓自定义通知栏

Posted

技术标签:

【中文标题】安卓自定义通知栏【英文标题】:Android custom notification bar 【发布时间】:2012-09-07 14:56:53 【问题描述】:

根据通知栏我确实拥有:http://developer.android.com/guide/topics/ui/notifiers/notifications.html

我很喜欢这个教程:

            Notification notification = new Notification();
            notification.flags = Notification.FLAG_ONGOING_EVENT;
            notification.icon = icon;

            RemoteViews contentView = new RemoteViews(getPackageName(),
                    R.layout.custom_notification);
            contentView.setImageViewResource(R.id.image, R.drawable.b_10);
            contentView.setTextViewText(R.id.title, "Custom zgłoszenie");
            contentView.setTextViewText(R.id.text, "test test test");
            notification.contentView = contentView;

            NotificationIntent Intent = new Intent(BatteryInfoService.this,
                    BatteryInfoActivity.class);
            ContentIntent PendingIntent = PendingIntent.getActivity(ta, 0,
                    notificationIntent, 0);
            notification.contentIntent = contentIntent;

            mNotificationManager.notify(BATTERY_ID, notification);

行中有错误:

            NotificationIntent Intent = new Intent(BatteryInfoService.this,
                    BatteryInfoActivity.class);
            ContentIntent PendingIntent = PendingIntent.getActivity(ta, 0,
                    notificationIntent, 0);
            notification.contentIntent = contentIntent;

错误:

NotificationIntent cannot be resolved to a type

Multiple markers at this line
    - ContentIntent cannot be resolved to 
     a type
    - ta cannot be resolved to a variable

contentIntent cannot be resolved to a variable

【问题讨论】:

用 Intent 代替 NotificationIntent。 【参考方案1】:

将 NotificationIntent 替换为 Intent(android 不提供 NotificationIntent,除非它是自定义类)。

你要找的是

Intent notificationIntent = new Intent(BatteryInfoService.this,BatteryInfoActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);

请仔细阅读教程。 Intent 和 PendingIntent 是 Android 类,而 NotificationIntent 和 ContentIntent 不是。如果您在这些名称下创建了自定义类,则可以导入相应的包。

【讨论】:

PendingIntent类型中的getActivity(Context, int, Intent, int)方法不适用于参数(new BroadcastReceiver(), int, Intent, int) 如果您在广播接收器中使用它,请尝试使用“MyClass.this”而不是“this”。这是您将正确传递活动的上下文。

以上是关于安卓自定义通知栏的主要内容,如果未能解决你的问题,请参考以下文章

Xamarin Android - 自定义通知托盘图标(状态栏)

Android,自定义通知,总是给我白色文本

Android种使用Notification实现通知管理以及自定义通知栏(示例四)

Android通知栏版本兼容解决方案

wex5 实战 极光推送自定义通知步骤与要领

自定义通知,如 Inshorts