学堂 学堂 学堂公众号手机端

Android UI设计:Notification

lewis 1年前 (2024-04-23) 阅读数 10 #技术


一、Notification介绍

  Notification是一种具有全局效果的通知,它展示在屏幕的顶端。随着Android版本的更新,Notification的使用方法也在随着版本更新,Android 3.0就是它的一个分界。

  通知一般通过NotificationManager服务发送一个Notification对象来完成通知,NotificationManager是一个重要的系统级服务,该对象位于应用程序的框架层中,应用程序可以通过它向系统发送全局的通知。


  

它的通知可以分为两种事件:

1、正在进行的事件

2、通知

如下图所示:

二、用法

1、 由于Notification是由NotificationManager统一管理的,因此在使用不论版本的新旧都需要先获得系统的通知服务。
2、一条通知的一般格式是先有一个图片+内容点击后会启动其他Intent
因此使用时需要对其图片、内容、PendingIntent 进行设置。

新的使用方法
//通过NotificationManager来获得系统的通知服务
mNotifycationManager=(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
//PendingIntent 的创建需要一个Intent对象,先创建Itent,基于上下文
Intent intent=new Intent(getApplicationContext(),MainActivity.class);
PendingIntent pending=PendingIntent.getActivity(getApplicationContext(), 0, intent, 0);
//创建Nitification对象,通过Notification.Builder.build()
Notification notification=new Notification.Builder(MainActivity.this).setAutoCancel(true).
setContentTitle("我是一个标题").setContentText("我是一个内容").setContentInfo("我是一个info").
setTicker("我是一个消息").setWhen(System.currentTimeMillis()).setSmallIcon(R.drawable.ic_launcher)
.setShowWhen(true).setContentIntent(pending).build();

mNotifycationManager.notify(1,notification);

效果图

旧版本用法(API 16之前使用)

由于google源代码的关系,旧版本的notification存在着问题那就是图片的设置,文本的设置等都是直接使用的”=“号,不太符合代码的一般书写格式,但是实质上是一样的。

代码:

;
notification=new Notification();
notification.icon=R.drawable.ic_launcher;
notification.tickerText="我是一个消息";
//设置自动取消
notification.flags=Notification.FLAG_AUTO_CANCEL;
notification.when=System.currentTimeMillis();
//通知的总数量
notification.number=1;

Intent intent=new Intent(getApplicationContext(),MainActivity.class);
PendingIntent pending=PendingIntent.getActivity(getApplicationContext(), 1, intent, 0);
notification.setLatestEventInfo(getApplicationContext(), "我是一个标题", "我是一个文本", pending);
//通过NotifycationManager.notify()来产生notification
mNotifycationManager.notify(1,notification);
自定义Notification

关于自定义的Notification的使用存在着一些奇怪的地方,尽管我们已经定义好了我们将要展示的Notification的界面且内容固定,但是在进行创建时仍然需要对他的Icon、Ticker等属性进行设置,如果不设置将不会进行显示,没有任何动作,但是写了并不影响你自定义的界面的展示。
自定义布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:gravity="center"
<ImageView
android:id="@+id/img_left"
android:layout_width="30dp"
android:layout_height="30dp"
android:src="@drawable/ic_launcher"
<TextView
android:layout_width="0dip"
android:layout_height="30dp"
android:layout_weight="1"
android:text="我是一则通知"
<ImageView
android:id="@+id/img_right"
android:layout_width="30dp"
android:layout_height="30dp"
android:src="@drawable/ic_launcher"
</LinearLayout>

调用时跟新用法一样

.class);
PendingIntent pending=PendingIntent.getActivity(getApplicationContext(), 0, intent, 0);
RemoteViews views=new RemoteViews(getPackageName(),R.layout.notification_define);
mNotifycationManager=(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification notification=new Notification.Builder(MainActivity.this).setAutoCancel(true).setContent(views).
setContentTitle("我是一个标题").setContentText("我是一个内容").setContentInfo("我是一个info").
setTicker("我是一个消息").setWhen(System.currentTimeMillis()).setSmallIcon(R.drawable.ic_launcher)
.setShowWhen(true).setContentIntent(pending).build();
mNotifycationManager.notify(1,notification);

取消Notification语句
.cancel(1);
PendingIntent 与Intent的区别

pendingIntent根据字面意思是延迟的intent,是一种特殊的Intent。它与Intent的主要区别在于Intent的执行立刻的,而pendingIntent的执行不是立刻的。
pendingIntent主要用来在某个事件完成后执行特定的Action。PendingIntent包含了Intent及Context,所以就算Intent所属程序结束,PendingIntent依然有效,可以在其他程序中使用。常用在通知栏及短信发送系统中。

PendingIntent一般作为参数传给某个实例,在该实例完成某个操作后自动执行PendingIntent上的Action,也可以通过PendingIntent的send函数手动执行,并可以在send函数中设置OnFinished表示send成功后执行的动作


版权声明

本文仅代表作者观点,不代表博信信息网立场。

热门