Android实现系统下拉栏的消息提示——Notification

简介: Android实现系统下拉栏的消息提示——Notification系统默认样式默认通知(通用)效果图按钮实现/** * 系统下拉栏默认的通...

Android实现系统下拉栏的消息提示——Notification

系统默认样式

默认通知(通用)

效果图

P1

按钮

<Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="notificationDefault"
        android:text="默认通知(通用)" />

实现

/**
 * 系统下拉栏默认的通用通知
 */
public void notificationDefault(View view) {
    // 获取NotificationManager管理者对象
    NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    // 创建一个PendingIntent,和Intent类似,不同的是由于不是马上调用,需要在下拉状态条出发的Activity,所以采用的是PendingIntent,即点击Notification跳转启动到哪个Activity
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, MainActivity.class), 0);
    // 获取Notification对象
    Notification notificationDefault = new Notification();
    // 设置显示的图标
    notificationDefault.icon = R.mipmap.ic_launcher;
    // 设置Title信息
    notificationDefault.tickerText = "TickerText:您有新短消息,请注意查收!";
    // 获取当前系统时间
    notificationDefault.when = System.currentTimeMillis();
    // 设置显示的信息
    notificationDefault.setLatestEventInfo(this, "Title信息", "信息内容", pendingIntent);
    // 设置右下角显示的提示数字
    notificationDefault.number = 1;
    // FLAG_AUTO_CANCEL表明当通知被用户点击时,通知将被清除
    notificationDefault.flags |= Notification.FLAG_AUTO_CANCEL;
    // 通过通知管理器来发起通知。
    manager.notify(NOTIFICATION_FLAG, notificationDefault);
}

默认通知(API 11+)

效果图

P2

按钮

<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:onClick="notificationAPI_11p"
    android:text="默认通知(API 11+)" />

实现

/**
 * 系统下拉栏默认的通知(API 11+)
 */
public void notificationAPI_11p(View view) {
    // 获取NotificationManager管理者对象
    NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    // 创建一个PendingIntent,和Intent类似,不同的是由于不是马上调用,需要在下拉状态条出发的Activity,所以采用的是PendingIntent,即点击Notification跳转启动到哪个Activity
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, MainActivity.class), 0);
    // 通过Notification.Builder来创建通知,注意API Level 11之后才支持
    Notification notificationAPI_11p = new Notification.Builder(this)
            // 设置状态栏中的小图片,尺寸一般建议在24×24,这个图片同样也是在下拉状态栏中所显示,如果在那里需要更换更大的图片,可以使用setLargeIcon(Bitmap icon)
            .setSmallIcon(R.mipmap.ic_launcher)
                    // 设置在status bar上显示的提示文字
            .setTicker("TickerText:" + "您有新短消息,请注意查收!")
                    // 设置在下拉status bar后显示的标题
            .setContentTitle("这里是标题(API 11+)")
                    // 设置在下拉status bar后显示的内容
            .setContentText("这里是显示的内容")
                    // 关联PendingIntent
            .setContentIntent(pendingIntent)
                    // 设置在下拉status bar后显示的数字
            .setNumber(1)
                    // 需要注意build()是在API level 16及之后增加的,在API 11中可以使用getNotificatin()来代替
            .getNotification();
    // FLAG_AUTO_CANCEL表明当通知被用户点击时,通知将被清除
    notificationAPI_11p.flags |= Notification.FLAG_AUTO_CANCEL;
    // 通过通知管理器来发起通知。
    manager.notify(NOTIFICATION_FLAG, notificationAPI_11p);
}

默认通知(API 16+)

效果图

P3

按钮

<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:onClick="notificationAPI_16p"
    android:text="默认通知(API 16+)" />

实现

/**
 * 系统下拉栏默认的通知(API 16+)
 */
public void notificationAPI_16p(View view) {
    // 获取NotificationManager管理者对象
    NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    // 创建一个PendingIntent,和Intent类似,不同的是由于不是马上调用,需要在下拉状态条出发的Activity,所以采用的是PendingIntent,即点击Notification跳转启动到哪个Activity
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, MainActivity.class), 0);
    // 通过Notification.Builder来创建通知,注意API Level 16之后才支持
    Notification notificationAPI_16p = new Notification.Builder(this)
            // 设置状态栏中的小图片,尺寸一般建议在24×24,这个图片同样也是在下拉状态栏中所显示,如果在那里需要更换更大的图片,可以使用setLargeIcon(Bitmap icon)
            .setSmallIcon(R.mipmap.ic_launcher)
                    // 设置在status bar上显示的提示文字
            .setTicker("TickerText:" + "您有新短消息,请注意查收!")
                    // 设置在下拉status bar后显示的标题
            .setContentTitle("这里是标题(API 16+)")
                    // 设置在下拉status bar后显示的内容
            .setContentText("这里是显示的内容")
                    // 关联PendingIntent
            .setContentIntent(pendingIntent)
                    // 设置在下拉status bar后显示的数字
            .setNumber(1)
                    // 需要注意build()是在API level 16及之后增加的,API11可以使用getNotificatin()来替代
            .build();
    // FLAG_AUTO_CANCEL表明当通知被用户点击时,通知将被清除。
    notificationAPI_16p.flags |= Notification.FLAG_AUTO_CANCEL;
    // 通过通知管理器来发起通知
    manager.notify(NOTIFICATION_FLAG, notificationAPI_16p);
}

自定义样式

效果图

P4

自定义提示框布局

layout目录下添加my_notification.xml文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#FF000000"
    android:orientation="vertical">

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

    <TextView
        android:id="@+id/text_content"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/icon"
        android:gravity="center"
        android:textColor="#FFFFFFFF"
        android:textSize="20sp" />

    <ProgressBar
        android:id="@+id/pb"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/text_content"
        android:layout_gravity="center_vertical"
        android:layout_toRightOf="@+id/icon" />

</RelativeLayout>

按钮

<Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="myselfNotification"
        android:text="自定义通知" />

实现

/**
 * 系统下拉栏自定义的通知
 */
public void myselfNotification(View view) {
    // 获取NotificationManager管理者对象
    mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    // Notification myNotify = new Notification(R.drawable.message,
    // "自定义通知:您有新短信息了,请注意查收!", System.currentTimeMillis());
    mNotification = new Notification();
    // 显示的图片
    mNotification.icon = R.mipmap.ic_launcher;
    // 设置在status bar上显示的提示文字
    mNotification.tickerText = "TickerText:您有新短消息,请注意查收!";
    // 获取当前系统时间
    mNotification.when = System.currentTimeMillis();
    // 表明当通知被用户点击时,通知不自动清除。
    mNotification.flags = Notification.FLAG_NO_CLEAR;
    // 加载自定义的布局
    mRemoteViews = new RemoteViews(getPackageName(), R.layout.my_notification);
    // 设置图片
    mRemoteViews.setImageViewResource(R.id.icon, R.mipmap.ic_launcher);
    // 设置文字
    mRemoteViews.setTextViewText(R.id.text_content, "下载进度");
    // 设置进度
    mRemoteViews.setProgressBar(R.id.pb, 100, 10, false);
    // 设置显示的自定义布局
    mNotification.contentView = mRemoteViews;
    // 设置点击通知栏的响应动作
    Intent intent = new Intent(Intent.ACTION_MAIN);
    // 设置通知的点击响应
    PendingIntent contentIntent = PendingIntent.getActivity(this, 1, intent, 1);
    mNotification.contentIntent = contentIntent;
    // 通过通知管理器来发起通知
    mNotificationManager.notify(NOTIFICATION_FLAG, mNotification);
}

清除指定ID的通知

按钮

<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:onClick="cleanNotificationById"
    android:text="清除指定ID的通知" />

实现

/**
 * 清除指定ID的提示
 * @param view
 */
public void cleanNotificationById(View view){
    // 获取NotificationManager管理者对象
    mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    // 清除id为NOTIFICATION_FLAG的通知
    mNotificationManager.cancel(NOTIFICATION_FLAG);
}

清除所有通知

按钮

<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:onClick="cleanNotificationAll"
    android:text="清除所有通知" />

实现

/**
 * 清除所有的提示
 *
 * @param view
 */
public void cleanNotificationAll(View view) {
    // 获取NotificationManager管理者对象
    mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    // 清除所有的通知
    mNotificationManager.cancelAll();
}
相关文章
|
2月前
|
Linux 测试技术 语音技术
【车载Android】模拟Android系统的高负载环境
本文介绍如何将Linux压力测试工具Stress移植到Android系统,用于模拟高负载环境下的CPU、内存、IO和磁盘压力,帮助开发者优化车载Android应用在多任务并发时的性能问题,提升系统稳定性与用户体验。
221 6
|
2月前
|
Java 数据库 Android开发
基于Android的电子记账本系统
本项目研究开发一款基于Java与Android平台的开源电子记账系统,采用SQLite数据库和Gradle工具,实现高效、安全、便捷的个人财务管理,顺应数字化转型趋势。
|
人工智能 搜索推荐 物联网
Android系统版本演进与未来展望####
本文深入探讨了Android操作系统从诞生至今的发展历程,详细阐述了其关键版本迭代带来的创新特性、用户体验提升及对全球移动生态系统的影响。通过对Android历史版本的回顾与分析,本文旨在揭示其成功背后的驱动力,并展望未来Android可能的发展趋势与面临的挑战,为读者呈现一个既全面又具深度的技术视角。 ####
|
7月前
|
安全 搜索推荐 Android开发
Android系统SELinux安全机制详解
如此看来,SELinux对于大家来说,就像那位不眠不休,严阵以待的港口管理员,守护我们安卓系统的平安,维护这片海港的和谐生态。SELinux就这样,默默无闻,却卫士如山,给予Android系统一份厚重的安全保障。
260 18
|
算法 JavaScript Android开发
|
IDE Java 开发工具
移动应用与系统:探索Android开发之旅
在这篇文章中,我们将深入探讨Android开发的各个方面,从基础知识到高级技术。我们将通过代码示例和案例分析,帮助读者更好地理解和掌握Android开发。无论你是初学者还是有经验的开发者,这篇文章都将为你提供有价值的信息和技巧。让我们一起开启Android开发的旅程吧!
|
缓存 Java Shell
Android 系统缓存扫描与清理方法分析
Android 系统缓存从原理探索到实现。
484 15
Android 系统缓存扫描与清理方法分析
|
监控 Java Android开发
深入探讨Android系统的内存管理机制
本文将深入分析Android系统的内存管理机制,包括其内存分配、回收策略以及常见的内存泄漏问题。通过对这些方面的详细讨论,读者可以更好地理解Android系统如何高效地管理内存资源,从而提高应用程序的性能和稳定性。
492 16
|
11月前
|
监控 Java Android开发
深入探索Android系统的内存管理机制
本文旨在全面解析Android系统的内存管理机制,包括其工作原理、常见问题及其解决方案。通过对Android内存模型的深入分析,本文将帮助开发者更好地理解内存分配、回收以及优化策略,从而提高应用性能和用户体验。
|
11月前
|
存储 安全 Android开发
探索Android系统的最新安全特性
在数字时代,智能手机已成为我们生活中不可或缺的一部分。随着技术的不断进步,手机操作系统的安全性也越来越受到重视。本文将深入探讨Android系统最新的安全特性,包括其设计理念、实施方式以及对用户的影响。通过分析这些安全措施如何保护用户免受恶意软件和网络攻击的威胁,我们希望为读者提供对Android安全性的全面了解。