简单Notification
创建Builder
haxe1
2
3
4
5NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("My notification")
.setContentText("Hello World!");添加Active
java1
2
3
4
5
6
7
8
9
10
11
12// 2. 创建点击回调动作
Intent resultIntent = new Intent(this, ResultActivity.class);
// Because clicking the notification opens a new ("special") activity, there's
// no need to create an artificial back stack.
PendingIntent resultPendingIntent =
PendingIntent.getActivity(
this,
0,
resultIntent,
PendingIntent.FLAG_UPDATE_CURRENT
);
mBuilder.setContentIntent(resultPendingIntent);通知
less1
2// Builds the notification and issues it.
mNotifyMgr.notify(101, mBuilder.build());后台调用
kotlin1
2
3
4
5
6
7
8
9
10
11
12// Creates an Intent for the Activity
Intent resultIntent = new Intent(this, ResultActivity.class);
// Sets the Activity to start in a new, empty task
resultIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
Intent.FLAG_ACTIVITY_CLEAR_TASK);
// Creates the PendingIntent
notifyIntent = PendingIntent.getActivity(
this,
0,
resultIntent,
PendingIntent.FLAG_UPDATE_CURRENT
);
移除Notification
- 调用后自动清除abnf
1
builder.setAutoCancel(true);
- 指定的 notification ID调用了cancel()
- 指定的 notification ID调用了cancelAll()
接下来的几个样式就不说了,简单,先贴图再看代码
BigView之Text
BigView之Picture
BigView之Inbox
BigView之Custom
Code
代码部分
java
1 | package com.xuie.notificationdemo; |
custom_notification.xml
xml1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:orientation="horizontal">
<ImageView
android:id="@+id/singer_pic"
android:layout_width="64dp"
android:layout_height="64dp"
android:src="@mipmap/ic_big_view"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:orientation="horizontal">
<ImageView
android:id="@+id/previous"
android:layout_width="0dp"
android:layout_height="48dp"
android:layout_weight="1"
android:src="@mipmap/previous"/>
<ImageView
android:id="@+id/play_pause"
android:layout_width="0dp"
android:layout_height="48dp"
android:layout_weight="1"
android:src="@mipmap/pause"/>
<ImageView
android:id="@+id/next"
android:layout_width="0dp"
android:layout_height="48dp"
android:layout_weight="1"
android:src="@mipmap/next"/>
</LinearLayout>
</LinearLayout>custom_expanded_notification.xml
xml
1 |
|