android notification小结
来源:互联网 发布:mahout推荐算法 编辑:程序博客网 时间:2024/06/06 19:02
本文简单介绍安卓 Notification 的用法,并简单对比 5.0 前后 Notification 的变化和处理方法。
1. Notification 效果举例
以微信为例,我们看下 Notification 的效果图:
2. 常用方法及作用
Notification 通常可以分为2个部分:Status Bar 中的部分和下拉通知栏后显示的部分。各个部分的名称如下图:
需要说明的是,下拉后的部分的布局是系统默认的,该布局可以通过 contentView 字段自定义设置。如天猫 APP 就使用了自定义布局如下图(Nexus 5X,Tmall V4.7.2):
我们用一段代码来说明图中上述项如何进行设置:
import android.app.Notification;import android.app.NotificationManager;import android.app.PendingIntent;import android.content.Context;import android.content.Intent;import android.graphics.Bitmap;import android.graphics.drawable.BitmapDrawable;import android.net.Uri;import android.support.v4.app.NotificationCompat;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.Button;import com.example.cmm.helloworld.R;public class NotificationActivityDemo extends AppCompatActivity { public final static int NOTIFICATION_ID = "NotificationActivityDemo".hashCode(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_notification_activity_demo); final NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); NotificationCompat.Builder builder = new NotificationCompat.Builder(this); // params int smallIconId = R.drawable.small_icon; Bitmap largeIcon = ((BitmapDrawable) getResources().getDrawable(R.drawable.large_icon)).getBitmap(); String info = "逢赌必输:我是通知内容"; // action when clicked Intent intent = new Intent(); intent.setAction(Intent.ACTION_VIEW); intent.setData(Uri.parse("host://anotheractivity")); builder.setLargeIcon(largeIcon) .setSmallIcon(smallIconId) .setContentTitle("逢赌必输") .setContentText(info) .setTicker(info) .setContentIntent(PendingIntent.getActivity(this, 0, intent, 0)); final Notification n = builder.getNotification(); Button btn = (Button) findViewById(R.id.btn); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { nm.notify(NOTIFICATION_ID, n); } }); }}
3. 5.0 后的变化
以上部分的内容是基于 4.4 及以下版本的 SDK。
5.0 及以上版本的 Notification 样式发生了改变,我们先来看个例子,直观感受一下。
下图为 24px x 24px 大小的 SmallIcon,我们可以看到其颜色艳丽精细,明显有悖 SmallIcon 简洁明了的精神:
在 4.1 和 5.0 的 Genymotion 模拟器上的效果图对比如下(很明显 5.0 上 SmallIcon 白成一坨了,完全无法辨别):
Notification 在 5.0及以上版本上的样式发生了变化,包括但不仅限于以下各项:
- 背景色,由黑色变为白色;
- smallIcon 的显示位置,由布局的右下角变为 largeIcon 的右下角,不过不设置 largeIcon,则系统会使用圆形灰色背景+SmallIcon构成largeIcon,且其右下角没有 SmallIcon,具体效果见下图;
- smallIcon 的显示方式,不再按原图显示,而只显示白色和透明色,且不再显示 ticker(提示文本);
- 如果不设置 largeIcon,则用 smallIcon 代替之,而且大图用灰色的圆包围 smallIcon 构成;
5.0 及以上的系统,Google 推荐 SmallIcon 仅使用白色和透明色两种颜色,而且尽量简单。如果使用其他颜色,系统会进行处理,仅仅显示上述两种颜色,从而导致图标变为白色的一坨,而无法辨别。
安卓 5.0(>= 4.0 同样适用) 及以上版本系统对通知图标的规则:
在5.0及以上系统,微信、大众点评、淘宝的 SmallIcon 如下图所示:
所以,我们要对 5.0 及以上版本做兼容性处理,代码类似:
// more codeboolean isAboveLollipop = Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP;builder.setSmallIcon(isAboveLollipop ? R.drawable.small_icon : R.drawable.white_small_icon);// more code
4. 结束语
本文简单介绍了 Notification 常用的 API 及在 5.0 及以上版本的变化。在实际开发中,我们要对 5.0 及以上系统做兼容性处理,也就是制作两套 SmallIcon。
- Android Notification通知小结
- android notification小结
- Android Notification 知识点自我小结
- Android状态栏通知Notification使用小结
- Android技巧小结之新旧版本Notification
- Android技巧小结之新旧版本Notification
- android开发之Notification结合PendingIntent使用小结
- ANdroid Notification
- android notification
- android notification
- android notification
- Android Notification
- android notification
- android notification
- android Notification
- Android Notification
- android Notification
- Android notification
- 2016 CocoaPods安装
- 华为multicast简单配置
- 2.4.4节练习
- java获取当月天数,指定年月的天数,指定日期获取对应星期
- Eclipse for C/C++(六)调试
- android notification小结
- ubuntu java 问题汇集
- Java中的static关键字解析
- 函数类型
- 【62.89%】【BZOJ 1072】[SCOI2007]排列perm
- HTML网页制作基础第二弹
- Windows 10下 JDK 与 ADB 环境变量的配置
- python list
- Codeforces-340A-The Wall