android o的区别,Android O上Notifaction的正确用法

引言

随着Android发展,很多老组件的用法也需要调整了。比如今天在升级某个个人项目时就碰到一个老工程的Notifiaction忽然出不来了。遂查资料,如果设置compileSdkVersion 26及以上需要传入一个NotifactionChannel类。那么NotificationChannel到底是个什么呢?谷歌在源码中如是说:

A representation of settings that apply to a collection of similarly themed notifications.

可以看到本质上就是对通知样式的一个描述集合。于是整理一个简单封装了一个DEMO类,抛砖引玉。

代码

import android.annotation.TargetApi;

import android.app.Notification;

import android.app.NotificationChannel;

import android.app.NotificationManager;

import android.content.Context;

import android.graphics.Color;

import android.os.Build;

import android.support.v4.app.NotificationCompat;

import android.text.TextUtils;

import android.util.Log;

/**

* Created by MoXiao on 2018/4/15.

*/

public class NotificationUtils {

private static final String TAG = "NotificationUtils";

private static final String DEFAULT_CHANNEL_ID = "default_id";

private static final String DEFAULT_CHANNEL_NAME = "default_channel_name";

private static final int DEFAULT_NOTIFY_ID = 1;

/**

* @param context

* @param title

* @param content

* @param smallIcon 显示在状态栏上的小图标,必需

*/

public static void notification(Context context, String title, String content, int smallIcon) {

//无论对于哪个API Level,都需要

NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

if (Build.VERSION.SDK_INT >= 26) {

boolean buildResult = notification_api_26_or_Above(notificationManager, DEFAULT_CHANNEL_ID);

if (!buildResult) {

Log.e(TAG, "Fail to build notifiactionChannel");

return;

}

}

//NotifyID,这个以前也需要

int notifyID = DEFAULT_NOTIFY_ID;

//ChannelID, 指明需要哪个channel。这里我们使用默认的。

String CHANNEL_ID = DEFAULT_CHANNEL_ID;

NotificationCompat.Builder nb = new NotificationCompat.Builder(context,CHANNEL_ID);

Notification notification = nb.setContentTitle(title)

.setContentText(content)

.setSmallIcon(smallIcon)

.build();

notificationManager.notify(notifyID, notification);

}

@TargetApi(26)

private static boolean notification_api_26_or_Above(NotificationManager notificationManager, String channelId) {

if (notificationManager == null && TextUtils.isEmpty(channelId)) {

return false;

}

//Android O及以上必需的channelID

String id = channelId;

//Channel名,有说这个字段对用户可见。但我还没有找到可见的ROM/Android版本。有知道的同学麻烦告知一下

String name = DEFAULT_CHANNEL_NAME;

//Channel的描述

String description = "我就是一个通知的channel";

//通知的优先级

int importance = NotificationManager.IMPORTANCE_LOW;

NotificationChannel channel = createDefaultChannel(id, name, description, importance);

//将NotificationChannel对象传入NotificationManager

notificationManager.createNotificationChannel(channel);

return true;

}

/**

* 创建默认封装的通知,其他样子依瓢画葫芦就行

* @param channelId 见上方注释

* @param channelName 见上方注释

* @param desc 见上方注释

* @param imp 见上方注释

* @return 生成好的默认行为的channel

*/

@TargetApi(26)

private static NotificationChannel createDefaultChannel(String channelId, String channelName, String desc, int imp) {

NotificationChannel channel = new NotificationChannel(channelId, channelName, imp);

channel.setDescription(desc);

//是否使用指示灯

channel.enableLights(true);

//指示灯颜色,这个取决于设备是否支持

channel.setLightColor(Color.RED);

//是否使用震动

channel.enableVibration(true);

//震动节奏

channel.setVibrationPattern(new long[]{100, 200});

return channel;

}

}

调用方式:

//在需要通知处

NotificationUtils.notification(this, "I am title", "I am content", R.mipmap.ic_av_timer_black_48dp);

可看到,在Android O以上版本的适配中,绝大部分都跟以前一致。区别就是NotificationChannel的创建而已。

Notifaction的更多相关知识点可以参考这篇文章:

Notification知识点总结

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值