Status Bar Notification

Android的Notification分为三种,适用于不同的情况. 

1. Toast Notification,  此为运行的Activity或者Service,弹出提示信息, 短时间显示之后,消失.

2. Status Bar Notification, 这个是将提示显示显示在Status Bar上, 用户通过下拉Status Bar获取Notification列表及详细信息, 此多用于需要用户异步关注并处理的提示信息.

3. Dialog Notification, 提示信息通过弹出一个对话框来显示,需要用户干预的才会关闭.而其相关的Activity在Dialog弹出后, 会失去焦点.


这里主要说一下Status Bar Notification. Android使用NotificationManager 来管理Status Bar

Status Bar Notification的显示

Status Bar  Notification的显示有两个阶段, 一个是先Status Bar上的显示, 一般包括一个图标和一小段文字,  二是用户下拉Status Bar后, 在Notification列表中的显示,在列表中的显示可以定制.


创建并显示Status Bar Notification的示例代码:

//取得NotificationManager
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
//创建Notification示例,并设置在Status Bar上显示的信息-图标和文字
int icon = R.drawable.notification_icon;
CharSequence tickerText = "Hello";
long when = System.currentTimeMillis();
Notification notification = new Notification(icon, tickerText, when);
//设置在下拉列表中的信息,其中PendingIntent用于设置,Notication项被按下后,发送的Intent.此处为打开MyClass Activity
Context context = getApplicationContext();
CharSequence contentTitle = "My notification";
CharSequence contentText = "Hello World!";

Intent notificationIntent = new Intent(this, MyClass.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
//通过NotificationManager将Notication发出去
private static final int HELLO_ID = 1;
mNotificationManager.notify(HELLO_ID, notification);


列表项的定制.

从上面的代码可以看到, 列表项显示以及点击消息定义是通过Notification的setLatestEventInfo()来完成的. 在定制实现时,则是采用给Notification的contentView和contentIntent赋值来完成. contentView为RemoteViews类型,而contentIntent为PendingIntent类型.

先来看contentView的赋值过程

首先要为View定义一个layout文件,比如 res/layout/custom_notification_layout.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/layout" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:padding="10dp" > 
    <ImageView android:id="@+id/image" 
        android:layout_width="wrap_content" 
        android:layout_height="fill_parent" 
        android:layout_alignParentLeft="true" 
        android:layout_marginRight="10dp" /> 
    <TextView android:id="@+id/title" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_toRightOf="@id/image" 
       /> 
    <TextView android:id="@+id/text" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_toRightOf="@id/image" 
        android:layout_below="@id/title"  />
       </RelativeLayout>


然后创建一个RemoteViews赋值给Notification的contentView

RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.custom_notification_layout); 
contentView.setImageViewResource(R.id.image, R.drawable.notification_image); 
contentView.setTextViewText(R.id.title, "Custom notification"); 
contentView.setTextViewText(R.id.text, "This is a custom layout"); 
notification.contentView = contentView;


接着来看看contentIntent的赋值,PengdingIntent对象的创建与初始化跟上面没什么区别。

Intent notificationIntent = new Intent(this, MyClass.class); 
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0); 
notification.contentIntent = contentIntent;


最后还是Notification的notify函数,将Notification发送出去。

mNotificationManager.notify(CUSTOM_VIEW_ID, notification);

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值