notification

来源:互联网 发布:加工中心编程视频教程 编辑:程序博客网 时间:2024/06/04 13:37

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
新部分:悬挂通知和显示等级

原创粉丝点击