notification

本文详细介绍了Android中的通知机制,包括如何创建和展示普通通知及折叠通知。通过代码示例展示了如何设置通知的各种属性如图标、点击行为等,并提供了实现自定义通知的方法。

https://github.com/wanglianghai/LightNotification
注意:折叠通知

 android:roundIcon="@mipmap/ic_launcher_round"

这要去了不然有冲突和

notification.bigContentView = remoteViews;

因为这字段废弃了,不维护了
普通通知:
这是个行为intent

Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.baidu.com"));

*小知识:*1.修改启动图标android:icon=”@drawable/launcher”
2.用Android资源android:background=”@android:color/white”
3.BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher)转换资源图片为bitmap类型的

创建普通通知


        PendingIntent pendingIntent = PendingIntent.getActivity(NotificationActivity.this, 0, intent, 0);
        Notification notification = new Notification.Builder(NotificationActivity.this)
                .setContentIntent(pendingIntent)
                .setSmallIcon(R.drawable.launcher)//*1
                .setAutoCancel(true)
                .setContentTitle("普通通知")
                .setContentText("default")
             .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
                .build();

启用通知:1.从系统中得到通知管理者

private NotificationManager mManager;
mManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

启用通知

mManager.notify(0, notification);

二:折叠通知,自定义通知
普通通知添加下面代码
创建布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="100dp">

    <ImageView
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:src="@mipmap/ic_launcher"/>
</LinearLayout>

代码实例化布局
视图的创建和显示不在同一进程中用RemoteView

RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.folder_view);

设置使用(折叠通知)

notification.bigContentView = remoteViews;
//notification.contentView = remoteViews;普通自定义通知

1:/*
* Set the small icon resource, which will be used to represent the notification in the
* status bar.
* 设置这资源小图标,将被用来展现通知在状态栏
* The platform template for the expanded view will draw this icon in the left, unless a
* {@link #setLargeIcon(Bitmap) large icon} has also been specified,
这平台的样板式扩大这视图画成图标在左边,除非setLargeIcon(Bitmap) 被指定了
5.0
新部分:悬挂通知和显示等级

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值