如何在安卓系统中添加动态图像而不是通知图标?

5
我使用以下代码在通知栏中显示通知。
它运行良好。
但我需要动态地显示来自Web服务的通知图标。
我该怎么做?
NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

Notification note = new Notification(R.drawable.image,"status message", System.currentTimeMillis());

        Intent in = new Intent(Notify.this,CommentU.class);
        PendingIntent pi = PendingIntent.getActivity(Notify.this, 0, in, 0);
        note.setLatestEventInfo(Notify.this,"NotificationTitle", "You have a new Commentio", pi);
        note.number = ++count;
        note.vibrate = new long[] { 500l, 200l, 200l, 500l };
        note.flags |= Notification.FLAG_AUTO_CANCEL;
        nm.notify(NOTIFY_ME_ID, note);

提前感谢。


请查看此示例:http://code4reference.com/2012/07/android-jelly-bean-notification/ - itsrajesh4uguys
2个回答

2

我有一个建议:您需要下载要显示的图像,然后可以将其设置为位图。请查看以下代码。我已经创建了一个BITMAP。 它的外观如下:

enter image description here

为此,您需要添加android-support-v4.jar

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
                this).setSmallIcon(R.drawable.ic_launcher)
                .setContentTitle("My notification").setLargeIcon(BITMAP)
                .setContentText("Hello World!");

        Intent resultIntent = new Intent(this, test.class);

        TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);

        stackBuilder.addParentStack(test.class);

        stackBuilder.addNextIntent(resultIntent);
        PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,
                PendingIntent.FLAG_UPDATE_CURRENT);
        mBuilder.setContentIntent(resultPendingIntent);
        NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        mNotificationManager.notify(NOTIFY_ME_ID, mBuilder.build());

要了解更多详细信息,请查看此链接

移除通知

通知会一直保持可见,直到发生以下情况之一:

用户单独或使用“全部清除”(如果通知可以被清除)来关闭通知。

用户点击通知,且在创建通知时调用了setAutoCancel()

您为特定的通知ID调用cancel()。该方法还会删除正在进行中的通知。

您调用cancelAll(),从而删除先前发布的所有通知。

已编辑:请替换此内容。

mBuilder = new NotificationCompat.Builder(
                this).setSmallIcon(R.drawable.ic_launcher)
                .setContentTitle("My notification").setLargeIcon(BITMAP)
                .setAutoCancel(true)
                .setContentText("Hello World!");

谢谢您的答复。但是,当我点击通知时,如何使通知栏中的通知消失呢? - user2159475
我使用了 setAutoCancel() 方法来使其消失。 - user2159475

0

只需在您的资源文件夹中添加一些图标,然后

int myIcon = R.drawable.image;

您的逻辑在这里....

根据您的逻辑更改图标值,例如myIcon = R.drawable.somenewimage;

最后设置您的通知

Notification note = new Notification(myIcon,"状态消息", System.currentTimeMillis());


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接