android8 Notification

 界面Layout:  customnotice.xml

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="match_parent"
 4     android:layout_height="100dp">
 5     <ImageView
 6         android:id="@+id/poster"
 7         android:layout_width="108dp"
 8         android:layout_height="match_parent"
 9         android:background="@drawable/poster"
10         android:contentDescription="这是专辑图片" />
11     <LinearLayout
12         android:layout_width="match_parent"
13         android:layout_height="100dp"
14         android:background="@android:color/white"
15         android:orientation="vertical">
16         <LinearLayout
17             android:layout_width="match_parent"
18             android:layout_height="50dp"
19             android:orientation="horizontal"
20             android:paddingLeft="9dp"
21             android:paddingRight="9dp"
22             android:paddingTop="9dp">
23             <TextView
24                 android:id="@+id/title"
25                 android:layout_width="match_parent"
26                 android:layout_height="match_parent"
27                 android:layout_weight="1"
28                 android:ellipsize="end"
29                 android:gravity="center"
30                 android:singleLine="true"
31                 android:text="从前的我-陈洁仪"
32                 android:textColor="#000000"
33                 android:textSize="16sp" />
34             <ImageView
35                 android:id="@+id/exit"
36                 android:layout_width="64dp"
37                 android:layout_height="45dp"
38                 android:layout_gravity="center_vertical"
39                 android:src="@drawable/exit" />
40         </LinearLayout>
41         <LinearLayout
42             android:layout_width="match_parent"
43             android:layout_height="50dp"
44             android:gravity="center"
45             android:orientation="horizontal">
46             <ImageView
47                 android:id="@+id/left"
48                 android:layout_width="0dp"
49                 android:layout_height="wrap_content"
50                 android:layout_weight="1"
51                 android:src="@drawable/left" />
52             <ImageView
53                 android:id="@+id/on"
54                 android:layout_width="0dp"
55                 android:layout_height="wrap_content"
56                 android:layout_weight="1"
57                 android:src="@drawable/on" />
58             <ImageView
59                 android:id="@+id/right"
60                 android:layout_width="0dp"
61                 android:layout_height="wrap_content"
62                 android:layout_weight="1"
63                 android:src="@drawable/right" />
64             <ImageView
65                 android:id="@+id/love"
66                 android:layout_width="0dp"
67                 android:layout_height="wrap_content"
68                 android:layout_weight="1"
69                 android:src="@drawable/love" />
70         </LinearLayout>
71     </LinearLayout>
72 </LinearLayout>

 发送通知逻辑代码:

 1              // 1、创建 manger
 2                 NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
 3                // 2、 创建  NotificationChannel(通知渠道) android 新特性
 4                 NotificationChannel channel = new NotificationChannel("1",
 5                         "Channel1", NotificationManager.IMPORTANCE_DEFAULT);
 6                 channel.enableLights(true); //是否在桌面icon右上角展示小红点
 7                 channel.setLightColor(Color.GREEN); //小红点颜色
 8                 channel.setShowBadge(true); //是否在久按桌面图标时显示此渠道的通知
 9                 notificationManager.createNotificationChannel(channel);
10                 //  3、 创建 Builder
11                 NotificationCompat.Builder builder = new NotificationCompat.Builder(MainActivity.this,"1");
12                 builder.setSmallIcon(R.drawable.music);
13                 builder.setContentTitle("title");
14                 builder.setContentText("content title");
15 //                builder.setNumber(3); //久按桌面图标时允许的此条通知的数量
16                 // 4、 绑定  layout
17                 RemoteViews rv = new RemoteViews(getPackageName(),R.layout.customnotice);

             /* 更改视图内容 */
18 //rv.setTextViewText(R.id.title,"泡沫");//修改自定义View中的歌名 19 //修改自定义View中的图片(两种方法) 20 //rv.setImageViewResource(R.id.iv,R.mipmap.ic_launcher); 21 // rv.setImageViewBitmap(R.id.poster, BitmapFactory.decodeResource(getResources(),R.drawable.music));


22 builder.setContent(rv); 23 // 5、 创建 notification 24 Notification notification = builder.build(); 25 // 6、发送通知 26 notificationManager.notify(0,notification); 27 28 // //删除NotificationChannel 29 // NotificationChannel mChannel =manager.getNotificationChannel(id); 30 // manager.deleteNotificationChannel(mChannel);

 界面效果:

 

猜你喜欢

转载自www.cnblogs.com/the-wang/p/8909815.html