当应用程序不是 运行 时,无法在 intentservice 上使用 Picasso 目标获取图像的位图

can't get bitmap of image using Picasso target on intentservice when app is not running

此代码用于在警报管理器触发警报时显示大图样式通知,并且大图图像必须使用 picasso 和 intentservice 从互联网下载,即使应用程序未 运行 在前台或后台但在以下情况下工作正常应用 运行 在前台或后台

当应用程序处于前台和后台时,我收到从互联网下载图像的通知 检查视频以了解问题请将音频静音,因为它非常嘈杂

https://www.youtube.com/watch?v=ID3PLuBvaRE&t=35s

class ShowNotificationFromAlarm : IntentService("ShowNotificationFromAlarm") {

    var target = object : Target {
        override fun onPrepareLoad(placeHolderDrawable: Drawable?) {
            Log.d("alarmnoti", "onprepareload")
        }

        override fun onBitmapFailed(errorDrawable: Drawable?) {
            Log.d("alarmnoti", "bitmapfailed")
        }

        override fun onBitmapLoaded(bitmap: Bitmap?, from: Picasso.LoadedFrom?) {
            Log.d("alarmnoti", "bitmaploaded")
            val builder = Notification.Builder(this@ShowNotificationFromAlarm)
                    .setSmallIcon(R.drawable.ic_share)
                    .setContentTitle(this@ShowNotificationFromAlarm.getString(R.string.app_name))
                    .setContentText("this is text")
                    .setLargeIcon(bitmap)
                    .setDefaults(Notification.DEFAULT_ALL)
                    .setPriority(Notification.PRIORITY_HIGH) //must give priority to High, Max which will considered as heads-up notification
                    .setStyle(Notification.BigPictureStyle()
                            .setBigContentTitle("this is big content title")
                            .setSummaryText("this is summary text")
                            .bigPicture(bitmap)
                    )

            //.addAction(action)
            val notification = builder.build()
            NotificationManagerCompat.from(this@ShowNotificationFromAlarm).notify(100, notification)
        }
    }

    override fun onHandleIntent(intent: Intent?) {    
        Log.d(this.packageName + "\t intentservice", "intent started")

        if (intent != null) {
            val uiHandler = Handler(Looper.getMainLooper())
            uiHandler.post(Runnable {
                shownotification(this@ShowNotificationFromAlarm.applicationContext, intent)
            })
        }
    }

    private fun shownotification(context: Context, intent: Intent) {    
        val intent = Intent(context, MainActivity::class.java)
        val pIntent = PendingIntent.getActivity(context, System.currentTimeMillis().toInt(), intent, 0)    
        Picasso.with(context)
                .load("https://cartodb-basemaps-d.global.ssl.fastly.net/dark_nolabels/3/3/4.png")
                //.load(context.filesDir.absolutePath + File.pathSeparator+"logo-2.png")
                .into(target)
    }
}

由于您正在使用 IntentService,它已经在单独的 thread.You 上运行,可以直接下载图像,而不是将下载任务推送到其他线程。请参阅下面的代码。

public class DownloadClass extends IntentService{
public DownloadClass(String name) {
    super(name);
}

@Override
protected void onHandleIntent(@Nullable Intent intent) {
    String url =intent.getStringExtra("url");
    Bitmap bitmap=downloadImageBitmap(url);
    if(bitmap!=null){
        // Use bitmap here
    }
}

private Bitmap downloadImageBitmap(String sUrl) {
    Bitmap bitmap = null;
    try {
        InputStream inputStream = new URL(sUrl).openStream();   // Download Image from URL
        bitmap = BitmapFactory.decodeStream(inputStream);       // Decode Bitmap
        inputStream.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return bitmap;
}
}