RemoteViews和PendingIntent

本文介绍了RemoteViews如何跨进程更新界面,特别是在通知栏和桌面小部件中的使用。详细阐述了如何使用RemoteViews自定义通知栏,并展示了桌面小部件的开发流程,包括布局定义、配置信息、实现类及在AndroidManifest.xml中的声明。此外,文章还讨论了PendingIntent在处理RemoteViews点击事件中的关键作用,解释了不同类型的PendingIntent获取方法以及其匹配规则和常用标志位。
摘要由CSDN通过智能技术生成

RemoteViews提供了一组操作用于跨进程更新界面,在Android中主要应用在通知栏和桌面小部件。
使用系统默认样式弹出通知栏

Notification.Builder builder =  new Notification.Builder(this);
        builder.setSmallIcon(R.mipmap.ic_launcher);
        builder.setContentTitle("通知,通知");
        builder.setContentInfo("这是一个通知,点击可以跳转到Main2Activity");
        builder.setWhen(System.currentTimeMillis());
        Intent intent = new Intent(this,Main2Activity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_UPDATE_CURRENT);
        builder.setContentIntent(pendingIntent);
        NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        notificationManager.notify(1,builder.getNotification());

使用RemoteViews弹出自定义通知栏,这里必须要setIcon,否则通知不显示

RemoteViews remoteViews = new RemoteViews(getPackageName(),R.layout.notification);
        remoteViews.setTextViewText(R.id.textView, "老板黄鹤带着他的小姨子跑路了,黄鹤你他妈的不是人");
        Intent intent = new Intent(this,Main2Activity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_UPDATE_CURRENT);
        remoteViews.setOnClickPendingIntent(R.id.button,pendingIntent);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值