在通知中使用毕加索的最简单方法(图标)

Pie*_*lse 9 multithreading android android-notifications 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));
        }
Run Code Online (Sandbox Code Playgroud)

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

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

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

Pie*_*lse 16

我会自己回答这个问题,因为我找到了一个不错的方法,使用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, iconId, notifId, notification);
//Picasso.with(getApplicationContext()).load("http://i.stack.imgur.com/CE5lz.png").into(bigContentView, bigIconId, notifId, notification);
Run Code Online (Sandbox Code Playgroud)

  • 这不起作用,它给出了错误 java.lang.IllegalStateException:方法调用应该从主线程发生。 (3认同)
  • 如果从服务调用则不起作用,因为它必须在 UI 线程上运行。此外,Notification.contentView 已被弃用,并且可以在 Nougat 上返回 null。 (2认同)

小智 6

不知道为什么你的代码不起作用,但它的编译对我来说很好,在API级别21和Android Studio上进行了测试.

我做了一些改变以满足我的需求,例如消除了时间延迟.

唯一明显的区别是我的logcat中的以下输出:

Setting airplane_mode_on has moved from android.provider.Settings.System to android.provider.Settings.Global, returning read-only value.  
Run Code Online (Sandbox Code Playgroud)

这是正常的基于链接:这个

我的更新代码是:

    Bitmap contactPic = null;

    final String getOnlinePic = GET_AVATAR;

    try {
        contactPic = new AsyncTask<Void, Void, Bitmap>() {
            @Override
            protected Bitmap doInBackground(Void... params) {
                try {
                    return Picasso.with(ctx).load(getOnlinePic)
                    .resize(200, 200)
                    .placeholder(R.drawable.ic_action_user_purple_light)
                    .error(R.drawable.ic_action_user_purple_light)
                    .get();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                return null;
            }
        }.execute().get();
    } catch (InterruptedException e) {
        e.printStackTrace();
    } catch (ExecutionException e) {
        e.printStackTrace();
    }

    if (contactPic != null) {
        builder.setLargeIcon(contactPic);
    } else {
        builder.setLargeIcon(BitmapFactory.decodeResource(ctx.getResources(), R.drawable.ic_action_user_purple_light));
    }
Run Code Online (Sandbox Code Playgroud)


try*_*ryp 6

我建议以最简单的方式将远程图片集成为带有毕加索的大图标。

// your notification builder
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle(getString(R.string.app_name))
                .setContentText(message)
                .setAutoCancel(true)
                .setSound(defaultSoundUri)
                .setContentIntent(pendingIntent);

String picture = "http://i.stack.imgur.com/CE5lz.png"; 
Bitmap bmp = Picasso.with(getApplicationContext()).load(picture).get();

notificationBuilder.setLargeIcon(bmp);

NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());
Run Code Online (Sandbox Code Playgroud)

  • Picasso.wi ..... get()是一个同步调用。它将阻止当前线程-&gt;确保您正在使用工作线程,否则应用程序的UI将冻结。 (2认同)