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();
}
相关文章
|
1月前
|
Android开发
基于android-11.0.0_r39,系统应用的手动签名方法和过程
本文介绍了基于Android 11.0.0_r39版本进行系统应用手动签名的方法和解决签名过程中遇到的错误,包括处理`no conscrypt_openjdk_jni-linux-x86_64`和`RegisterNatives failed`的问题。
85 2
|
30天前
|
JavaScript 前端开发 Java
[Android][Framework]系统jar包,sdk的制作及引用
[Android][Framework]系统jar包,sdk的制作及引用
39 0
|
2月前
|
搜索推荐 Android开发 iOS开发
探索安卓与iOS系统的用户界面设计哲学
现代移动操作系统的设计哲学不仅仅是技术的表现,更是用户体验与功能实现的结合。本文将深入分析安卓与iOS两大主流系统在用户界面设计方面的差异与共通之处,探讨它们背后的思维模式及其对用户体验的影响。 【7月更文挑战第11天】
|
4天前
|
监控 Android开发 iOS开发
深入探索安卓与iOS的系统架构差异:理解两大移动平台的技术根基在移动技术日新月异的今天,安卓和iOS作为市场上最为流行的两个操作系统,各自拥有独特的技术特性和庞大的用户基础。本文将深入探讨这两个平台的系统架构差异,揭示它们如何支撑起各自的生态系统,并影响着全球数亿用户的使用体验。
本文通过对比分析安卓和iOS的系统架构,揭示了这两个平台在设计理念、安全性、用户体验和技术生态上的根本区别。不同于常规的技术综述,本文以深入浅出的方式,带领读者理解这些差异是如何影响应用开发、用户选择和市场趋势的。通过梳理历史脉络和未来展望,本文旨在为开发者、用户以及行业分析师提供有价值的见解,帮助大家更好地把握移动技术发展的脉络。
|
1天前
|
Dart 开发工具 Android开发
在 Android 系统上搭建 Flutter 环境的具体步骤是什么?
在 Android 系统上搭建 Flutter 环境的具体步骤是什么?
|
24天前
|
Android开发 UED 开发者
Android经典实战之WindowManager和创建系统悬浮窗
本文详细介绍了Android系统服务`WindowManager`,包括其主要功能和工作原理,并提供了创建系统悬浮窗的完整步骤。通过示例代码,展示了如何添加权限、请求权限、实现悬浮窗口及最佳实践,帮助开发者轻松掌握悬浮窗开发技巧。
48 1
|
1月前
|
Java 物联网 Android开发
移动应用与系统:技术演进与未来展望探索安卓应用开发:从新手到专家的旅程
【8月更文挑战第28天】本文将深入探讨移动应用开发的技术演进、移动操作系统的发展历程以及未来的发展趋势。我们将通过实例和代码示例,展示如何利用最新的技术和工具来开发高效、可靠的移动应用。无论你是初学者还是经验丰富的开发者,这篇文章都将为你提供有价值的信息和见解。 【8月更文挑战第28天】在这个数字时代,掌握安卓应用的开发技能不仅是技术人员的追求,也成为了许多人实现创意和梦想的途径。本文将通过深入浅出的方式,带领读者从零基础开始,一步步走进安卓开发的奇妙世界。我们将探讨如何配置开发环境,理解安卓应用的核心组件,以及如何通过实际编码来构建一个功能完整的应用。无论你是编程新手还是希望提升自己的开发者
|
1月前
|
存储 安全 物联网
Android经典实战之跳转到系统设置页面或其他系统应用页面大全
本文首发于公众号“AntDream”,关注获取更多技巧。文章总结了Android开发中跳转至系统设置页面的方法,包括设备信息、Wi-Fi、显示与声音设置等,并涉及应用详情与电池优化页面。通过简单的Intent动作即可实现,需注意权限与版本兼容性。每日进步,尽在“AntDream”。
159 2
|
29天前
|
安全 Android开发 iOS开发
安卓与iOS的终极对决:哪个系统更适合你?
在智能手机的世界里,安卓和iOS两大操作系统如同两座巍峨的山峰,各自拥有庞大的用户群体。本文将深入浅出地探讨这两个系统的优缺点,并帮助你找到最适合自己的那一款。让我们一起揭开这场技术盛宴的序幕吧!
|
2月前
|
Android开发 Kotlin
kotlin开发安卓app,如何让布局自适应系统传统导航和全面屏导航
使用`navigationBarsPadding()`修饰符实现界面自适应,自动处理底部导航栏的内边距,再加上`.padding(bottom = 10.dp)`设定内容与屏幕底部的距离,以完成全面的布局适配。示例代码采用Kotlin。
99 15