Notification相关和一些关键点收集

本文详细介绍了在Android中如何创建和管理Notification,包括Notification的分类、默认与自定义生成、展示、更新和取消的方法。重点讨论了自定义Notification的RemoteViews配置和点击响应,以及Notification在前台服务中的应用,以确保后台服务不被系统杀死。
摘要由CSDN通过智能技术生成

Notification相关和一些关键点收集

最近项目需求写一个自定义Notification,将其中的一些知识总结下,方便自己回顾,也给其他人一些参考,个人理解可能有误,欢迎大家交流。

Notification官方说明和使用指导先放上来

Notification的分类

  • 默认样式的Notification,API=1就引入
  • 扩展Notification,从Android 4.3引入
  • 自定义Notification(将自定义的RemotveViews传入Notification的ContentView)

生成默认Notification

  • icon属性必须设置,避免Notification不被系统显示
  • API=11及以前:
public Notification() {
    this.when = System.currentTimeMillis();
}

public Notification(int icon, CharSequence tickerText, long when) {
    this.icon = icon;
    this.tickerText = tickerText;
    this.when = when;
}

然后使用

public void setLatestEventInfo(Context context,
            CharSequence contentTitle, CharSequence contentText, PendingIntent contentIntent) {
        RemoteViews contentView = new RemoteViews(context.getPackageName(),                com.android.internal.R.layout.status_bar_latest_event_content);
        if (this.icon != 0) {
 contentView.setImageViewResource(com.android.internal.R.id.icon, this.icon);
        }
        if (contentTitle != null) {            contentView.setTextViewText(com.android.internal.R.id.title, contentTitle);
        }
        if (contentText != null) {            contentView.setTextViewText(com.android.internal.R.id.text, contentText);
        }
        if (this.when != 0) {
            Date date = new Date(when);
            CharSequence str = 
                DateUtils.isToday(when) ? DateFormat.getTimeFormat(context).format(date)                  DateFormat.getDateFormat(context).format(date);            contentView.setTextViewText(com.android.internal.R.id.time, str);
        }
        this.contentView = contentView;
        this.contentIntent = contentIntent;
    }

生成默认的Notification样式,然而在API11时被标记为decepate在api23时被移除

  • 3.0以后
    Notification notification = new Notification.Builder.getNotification();
    获取基本的Notification对象

  • 4.3版本以后
    Notification notification = new Notification.Builder.builer();

可扩展Notification

// XXX TODO 后续补充

自定义Notification

  • 自定义Notification的主要操作在于生成正确的RemoteViews并设置合适的点击点击响应。(由于在Android 3.0以前不支持自定义RemoteViews中的组件响应,所以常见的需要操作的自定义Notification都会隐藏,eg.酷狗音乐等等)
  • RemoteViews只支持系统提供的基础组件,如果包含自定义View会报错。背景色可以使用一张充满的ImageView实现。在Notification RemoteViews中如果最外层为LinearLayout会无法实现对内容的居中。布局样例如下:
<RelativeLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

    <ImageView
        android:id="@+id/background_img"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <FrameLayout
            android:id="@+id/notice_toggle_one_layout"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background=
    "@drawable/notification_toggle_click_selector"
            android:clickable="true">
            <ImageView
                android:id="@+id/notice_toggle_one"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:src=
    "@drawable/notification_toggle_icon"
                />
        </FrameLayout>

    </LinearLayout>

</RelativeLayout>
  • remoteViews.setImageViewResource(R.id.
    background_img,R.drawable.background)和
    remoteViews.setOnClickPendingIntent(R.id.notice_click_id, pendingIntent)实现对RemoteViews的图片配置和点击响应配置

展示Notification

NotificationManager mNotificationManager =  (NotificationManager)context.getSystemService(Intent.notification_service);
mNotificationManager.notify(id,notification);

程序公用一套ID,需要区分各个Notification,避免在后续更新时造成错误,建议为自己的application建立一个Notification Id的常数类

更新Notification

mNotificationManager.notify(id,notification);

取消Notification

  • 可以设置FLAG
  • 使用代码
mNotificationManager.cancel(id);

或则

mNotificationManager.cancelAll();

如果手动设置了Notification的Notification.FLAG_FOREGROUND_SERVICE会导致NotificationManager.cancel(id)或则cancelAll()方法不生效

Notification的作用

  • Notification Toggle 常用快捷快关,便于随时操作
  • 设置为前台服务,避免后台服务被杀死

前台服务

  • 作用:避免后台服务被杀死,提高程序的存活率
  • 开启前台服务:service.startFrontService(id,notification);id不能为0
  • 关闭前台服务:service.stopFrontService(boolean);true:删除前台NOtification;false:不删除Notification,等待后续的NotificationManager调用cancel()方法
  • 在4.3版本以前可以直接使用一个空的Notification开启其前台服务startForegroundService(id,new Notification())

TAG

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值