• QQ
  • nahooten@sina.com
  • 常州市九洲新世界花苑15-2

Android

说说8.0下 Android 通知(Notification)

原创内容,转载请注明原文网址:http://homeqin.cn/a/wenzhangboke/jishutiandi/Android/2019/0611/535.html

 

当运用程序不在前台运转,这时便借助关照( Notification )向用户发送少许提醒消息。 发出关照后,手机非常上方的状况栏中就会表现一个关照图标,下拉状况栏就会看到关照的细目。

 

1 根基用法

//获取体系关照服无

NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

 

String contentTitle = "重庆现非常修长电梯"; //题目

String contentText = "宽度仅包容一人爆红网页";//内容

Notification notification = new NotificationCompat.Builder(context).setContentTitle(contentTitle)

        .setContentText(contentText).setWhen(System.currentTimeMillis()).setSmallIcon(R.mipmap.ic_launcher)

        .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher)).build();

manager.notify(1, notification);

1

2

3

4

5

6

7

8

9

*应用 NotificationCompat 可以包管关照功效能够在当前所有的 Android 版本中都适合。 

*NotificationCompat 的 Builder 支持应用连缀的配置要领,这一点很像 jQuery。

 

Builder 配置要领:

 

要领名 申明

setContentTitle(CharSequence title) 配置题目。

setContentText(CharSequence text) 配置内容。

setWhen(long when) 配置建立关照的光阴,单元是毫秒。

setSmallIcon(int icon) 配置关照的小图标,会表现在手机的左上角。

setLargeIcon(Bitmap icon) 配置关照的大图标,下拉体系的状况栏时,便看到它啦。

notify(int id, Notification notification) 要领用于表现关照,它有两个参数:id 是我们运用中为关照定义的唯一标识符;notification 就是我们确立的关照工具。

 

运转后:

 

关照小图标

 

下拉体系状况栏,即可看到我们新建的关照消息:

 

 

 

这时的关照消息尚未实现点击结果,我们可以通过 PendingIntent 来实现。它与 Intent 的差别之处是:

 

Intent - 登时执行某个动作。

PendingIntent - 在某个合适的机遇去执行某个动作。

我们新建一个活动,当用户点击关照消息后,会跳转到这个活动中。

 

结构文件:

 

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:id="@+id/activity_notification"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    >

 

    <TextView

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:textSize="26sp"

        android:text="在重庆自由碑自满世界左近,有一部迷你电梯,因其非常局促惹起了宽泛的关注,对于这部魔性的电梯,有网友表示“这是对胖子的深深歹意······”"

        />

 

</RelativeLayout>

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

这个活动很简单,只是展现少许文本内容。

 

接着,点窜以前的活动代码:

 

//获取体系关照服无

NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

 

//建立 PendingIntent

int requestCode = 0;

int flags = PendingIntent.FLAG_UPDATE_CURRENT;

Intent intent = new Intent(context, NotificationActivity.class);//启动 NotificationActivity 活动

PendingIntent pendingIntent = PendingIntent.getActivity(context, requestCode, intent, flags);

 

//建立关照

String contentTitle = "重庆现非常修长电梯"; //题目

String contentText = "宽度仅包容一人爆红网页";//内容

Notification notification = new NotificationCompat.Builder(context).setContentTitle(contentTitle)

        .setContentText(contentText).setWhen(System.currentTimeMillis()).setSmallIcon(R.mipmap.ic_launcher)

        .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher)).setContentIntent(pendingIntent).setAutoCancel(true).build();

manager.notify(1, notification);

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

这里建立 PendingIntent 工具,并把它放入 NotificationCompat.Builder 中。

 

PendingIntent.getActivity 定义如下:

 

 public static PendingIntent getActivity(Context context, int requestCode,

            Intent intent, @Flags int flags)

1

2

参数 形貌

Context 高低文,在 Activity 类中就是本身(this)。

requestCode 要求码。

intent Intent 工具。

flags 举动体例。

flags 举动体例范例:

 

范例 形貌

FLAG_ONE_SHOT PendingIntent 工具只能被应用一次。

FLAG_NO_CREATE 要是 PendingIntent 工具不存在,则回笼 null。

FLAG_CANCEL_CURRENT 即便以前的 PendingIntent 工具曾经存在,也会建立一个新的 PendingIntent 工具。

FLAG_UPDATE_CURRENT 要是以前的 PendingIntent 工具曾经存在,辣么会更新它的内容。(常用)

留意:我们构建 Build 的过程当中加入了 setAutoCancel(true),则表示当用户点击了关照后,手机左上角的图标就不会再表现啦。也能够应用 NotificationManager 的 cancel 要领主动作废,这可以运用于某些分外场景:

 

int notifyId = 1;//关照 ID

manager.notify(notifyId, notification);

 

//耽误 5 s,要不关照一会儿就会被作废啦

try {

    Thread.sleep(5000);

} catch (InterruptedException e) {

    e.printStackTrace();

}

manager.cancel(notifyId);

1

2

3

4

5

6

7

8

9

10

运转程序,再次点击体系关照栏中的关照,这时将会弹出关照活动页:

 

 

 

2 高级功效

2.1 自定义关照音

翻开 Android Device Monitor,可以看到在 "/system/media/audio/notifications/ 下自带了良多关照音,我们可以选定乃至是自定义一个喜好的关照音:

 

体系自带的关照音

 

//自定义关照音

Uri customSound = Uri.fromFile(new File("/system/media/audio/notifications/Altair.ogg"));

1

2

可以通过以下体例获得体系的关照音:

 

Uri sound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

1

非常后应用 setSound 要领来配置关照音:

 

 public Builder setSound(Uri sound)

1

是不是很简单呀O(∩_∩)O哈哈~

 

2.2 关照振动

通过配置 vibrate 属性,可以让关照出现时让手机振动,它定义如下:

 

public Builder setVibrate(long[] pattern) 

1

pattern 是长整型数组,它被用来配置手机稳定和振动的时长,以毫秒为单元。它是一种交替的配置形式,好比下标 0 表示稳定的时长,下标 1 表示振动的时长,下标 2 又表示振动的时长,以此类推。

 

Notification notification = new NotificationCompat.Builder(context)

...

.setVibrate(new long[]{0,1000,1000,1000}).build();

1

2

3

接着,在 AndroidManifest.xml 中申明振动权限:

 

<uses-permission android:name="android.permission.VIBRATE"/>

1

如许,当用户收到关照时,手机就会振动啦 O(∩_∩)O哈哈~

 

2.3 呼吸灯

呼吸灯是指灯光在微电脑的掌握之下实现由亮到暗的渐渐变更,感受好像是人在呼吸,它起到一个关照提醒的感化。

 

通过配置 setLights ,可以掌握呼吸灯的变更频率,它定义如下:

 

public Builder setLights(@ColorInt int argb, int onMs, int offMs) 

1

参数 申明

argb 灯的色彩,通过 Color 类来配置。

onMs 灯亮的时长,单元:毫秒。

offMs 灯灭的时长,单元:毫秒。

//自定义色彩-紫色

int color=Color.rgb(255,0,255);

 

Notification notification = new NotificationCompat.Builder(context).setContentTitle(contentTitle)

...

.setLights(color,1000,1000).build();

1

2

3

4

5

6

2.4 富文本

NotificationCompat.Builder 类中的 setStyle() 要领,可以让我们构建出富文本的关照内容, 这个要领汲取一个 NotificationCompat.style 参数,通过它用来建立出详细的富文本信息,如长文字 、 图片等 。

 

2.4.1 长文本

要是 setContentText() 传入的内容过长,辣么一行内表现不完的内容就会变为不祥号:

 

 

 

//长文字

String longContentText = " 即日,重庆现非常修长电梯走红网页,毕竟怎么样环境呢?在重庆自由碑自满世界左近,有一部显得十分“修长”的迷你电梯,经测量,宽度约57.5厘米,只能容下一个人乘坐,一般而言,一般的电梯可以站两个人,但这部迷你电梯相对局促,于是有很多旅客特地来打卡摄影。";//内容

android.support.v4.app.NotificationCompat.BigTextStyle bigTextStyle=new NotificationCompat.BigTextStyle().bigText(longContentText);

 

 

Notification notification = new NotificationCompat.Builder(context).setContentTitle(contentTitle)

        ...

        .setStyle(bigTextStyle).build();

1

2

3

4

5

6

7

8

长文本表现结果

 

2.4.2 图片

图片表现结果

 

2.5 优先级

NotificationCompat.Builder 类中的 setPriority() 要领可用于配置关照的紧张程度,它汲取一个整型参数,在 NotificationCompat 中定义了 5 级优先级常量值:

 

可用的优先级参数

 

 int priority=NotificationCompat.PRIORITY_MAX;

 

Notification notification = new NotificationCompat.Builder(context).setContentTitle(contentTitle)

...

                        .setPriority(priority)

                        .build();

1

2

3

4

5

6

要是配置为非常高优先级,辣么它会干脆表现在屏幕顶部:

 

非常高优先级结果

 

确保这个关照对于用户来说确实是至关紧张的,否则要是用户发生了反感,辣么有可能会卸载掉这个 APP 哦。

 

上篇:上一篇:Android开发之Intent.Action
下篇:下一篇:AccessibilityService的应用