Android通知栏显示网络图片并点击跳转传值
Android通知栏显示网络图片并点击跳转传值
有时通知栏需要显示网络图片,新闻类或者音乐播放。不过越高的版本越难搞。
一、思路:
用NotificationCompat的.setLargeIcon
二、效果图:
也可以看视频直观点:
Android开发教程案例源码分享-通知栏显示网络图片并点击跳转传值
三、关键代码:
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val imageNetUrl = "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fi0.hdslb.com%2Fbfs%2Farticle%2F845eb3dc0fdc4ad2f7a9fc11a43a50d956125e3f.jpg&refer=http%3A%2F%2Fi0.hdslb.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1638346070&t=e6e4d52cdbc52ea2da6102a02bd73508"
var imageBitmap : Bitmap? = null
Glide.with(this).asBitmap().load(imageNetUrl)
.apply(RequestOptions().circleCrop().autoClone())
.into(object : SimpleTarget<Bitmap>(){
override fun onResourceReady(resource: Bitmap?, transition: Transition<in Bitmap>?) {
imageBitmap = resource
}
})
val manager = getSystemService(Context.NOTIFICATION_SERVICE) as
NotificationManager
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val channel = NotificationChannel("important", "Important",NotificationManager.
IMPORTANCE_HIGH)
manager.createNotificationChannel(channel)
}
//Glide生成bitmap需要时间,这点要注意
findViewById<View>(R.id.tv_click).setOnClickListener {
val intent = Intent(this,NotificationToActivity::class.java)
intent.putExtra("notify_msg","通知栏包括的信息")
//这里requestCode参数不能为0,否则没法传值
val pi = PendingIntent.getActivity(this,((Math.random() * 1000) + 1).toInt(),intent,PendingIntent.FLAG_IMMUTABLE)
val notification = NotificationCompat.Builder(this, "important")
.setContentTitle("This is title")
.setContentText("This is content text")
四、项目demo源码结构图:
有问题或者需要完整源码demo的私信我
原文地址:https://blog.csdn.net/u010074743/article/details/144019247
免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!