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

我正在寻找一种简单的方法来使用Picasso来加载一个显示图标(这是一个远程网页上的URL).在以前版本的应用程序我正在研究这个代码似乎工作:

Bitmap speakerPic = null;

try {

speakerPic = new AsyncTask() {

@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);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值