0

我想将位图图像显示为通知,但下面的代码在我出错的地方不起作用我无法找到我正在以位图形式从 url 获取图像但无法显示。

我在 NotiIcon 变量中获取位图图像,它是位图类型。我添加了获取位图图像。

提前致谢 。

public static void ShowNotification()
{    
    AsyncTask<Void, Void, Void> t = new AsyncTask<Void, Void, Void>(){  
        protected Void doInBackground(Void... p) {
            String NS =  Context.NOTIFICATION_SERVICE;

            /*  String dtitle = Title;
            String dbody = body; */

            Intent notificationIntent1 =new Intent(appContext, ChromeTestActivity.class);
            PendingIntent contentIntent1 = PendingIntent.getActivity(appContext, 0, notificationIntent1, 0);
            NotificationManager mNm = (NotificationManager)   appContext.getSystemService(NS);
            Notification.Builder builder = new Notification.Builder(appContext);
            builder.setContentIntent(contentIntent1).setLargeIcon(NotiIcon)
                    .setWhen(System.currentTimeMillis())
                    .setAutoCancel(false)
                    .setContentTitle(Title)
                    .setContentText(body);
            Notification n = builder.getNotification();
            mNm.notify(NOTIFICATION_ID, n);
            textTitle.setText(Title);
            textBody.setText(body);
            textTitle.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    Log.v(LOGTAG, "Inside NotificationUIManager handleButtonClick");
                }
            });

            textBody.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    Log.v(LOGTAG, "Inside NotificationUIManager handleButtonClick");
                }
            });
            return null;
        }
    };
    t.execute();
}

public static Bitmap DecodeIconUrl(String paramString1)
{
    NotiIcon = null;  
    try
    {
        URL iconurl =  new URL(paramString1);
        URLConnection conn =  iconurl.openConnection();
        conn.connect();
        InputStream is = conn.getInputStream();
        BufferedInputStream bis =  new BufferedInputStream(is);
        NotiIcon = BitmapFactory.decodeStream(bis);
        bis.close();
        is.close();
    }
    catch(IOException e){

    }
return NotiIcon;
}
4

1 回答 1

0

我们必须像这样将图标添加到通知中

检查这个

String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);

long when = System.currentTimeMillis();
Notification notification = new Notification(R.drawable.notify_icon, "Name", when);

notification.defaults |= Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE;

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

notification.setLatestEventInfo(context, "Title", "contentText", contentIntent);
mNotificationManager.notify(ID, notification);
于 2012-06-22T03:59:21.850 回答