android – 在通知中使用Picasso最简单的方法(图标)

我正在寻找一种简单的方法来使用Picasso来加载一个显示图标(这是一个远程网页上的URL).在以前版本的应用程序我正在研究这个代码似乎工作:
Bitmap speakerPic = null;
        try {
            speakerPic = new AsyncTask<Void,Void,Bitmap>() {
                @Override
                protected Bitmap doInBackground(Void... params) {
                    try {
                        return Picasso.with(c).load(session.getSpeaker().getPhotoUrl()).get();
                    } catch (IOException e) {
                        e.printstacktrace();
                    }
                    return null;
                }
            }.execute().get(1500,TimeUnit.MILLISECONDS);
        } catch (InterruptedException e) {
            e.printstacktrace();
        } catch (ExecutionException e) {
            e.printstacktrace();
        } catch (TimeoutException e) {
            e.printstacktrace();
        }

        if (speakerPic != null) {
            builder.setLargeIcon(speakerPic);
        } else {
            builder.setLargeIcon(BitmapFactory.decodeResource(c.getResources(),R.drawable.ic_launcher));
        }

但现在我每次都得到一个TimeOutException(我回退到res文件夹中的认图标).我必须使用这个AsyncTask,因为Picasso(/ network)可能不会在UI线程上发生. (虽然我在这里阻止了1.5秒的UI线程..).

我知道Picasso可以处理远程视图,但我不想使用自定义视图来表示我的意见.另外,我找不到一种方法获取NoticifationIcon的RemoteView.

有没有办法只使用毕加索设置我的通知图标?

解决方法

我会自己回答这个问题,因为我找到了一个不错的方法,使用Picasso和RemoteViews.经过测试并与Picasso 2.5.2一起使用:
// Default stuff; making and showing notification
final Context context = getApplicationContext();
final notificationmanager notificationmanager = (notificationmanager) getSystemService(Context.NOTIFICATION_SERVICE);
final Notification notification = new NotificationCompat.Builder(context)
        .setSmallIcon(R.mipmap.ic_launcher) // Needed for the notification to work/show!!
        .setContentTitle("Title of notification")
        .setContentText("This is the description of the notification")
        // Uncomment if you want to load a big picture
        //.setStyle(new NotificationCompat.BigPictureStyle())
        .build();
final int notifId = 1337;
notificationmanager.notify(notifId,notification);

// Get RemoteView and id's needed
final RemoteViews contentView = notification.contentView;
final int iconId = android.R.id.icon;

// Uncomment for BigPictureStyle,Requires API 16!
//final RemoteViews bigContentView = notification.bigContentView;
//final int bigIconId = getResources().getIdentifier("android:id/big_picture",null,null);

// Use Picasso with RemoteViews to load image into a notification
Picasso.with(getApplicationContext()).load("http://i.stack.imgur.com/CE5lz.png").into(contentView,iconId,notifId,notification);

// Uncomment for BigPictureStyle
//Picasso.with(getApplicationContext()).load("http://i.stack.imgur.com/CE5lz.png").into(bigContentView,notification);
//Picasso.with(getApplicationContext()).load("http://i.stack.imgur.com/CE5lz.png").into(bigContentView,bigIconId,notification);

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...