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();
}
相关文章
|
24天前
|
搜索推荐 Android开发 iOS开发
安卓与iOS系统的用户界面设计对比分析
本文通过对安卓和iOS两大操作系统的用户界面设计进行对比分析,探讨它们在设计理念、交互方式、视觉风格等方面的差异及各自特点,旨在帮助读者更好地理解和评估不同系统的用户体验。
18 1
|
2月前
|
搜索推荐 Android开发 iOS开发
探析安卓与iOS系统的优劣
【2月更文挑战第7天】安卓与iOS是当今手机市场上最主流的两款操作系统,各有优劣。本文将从用户体验、开放程度、生态系统等方面对两者进行深入探析,以期帮助读者更好地了解它们的特点。
|
2月前
|
Android开发 数据安全/隐私保护 iOS开发
安卓与iOS系统的发展趋势与比较分析
【2月更文挑战第6天】 在移动互联网时代,安卓和iOS系统作为两大主流移动操作系统,各自呈现出不同的发展趋势。本文将从技术角度出发,对安卓和iOS系统的发展方向、特点及未来趋势进行比较分析,以期为读者提供更深入的了解和思考。
35 4
|
3月前
|
Linux 调度 Android开发
【系统启动】Kernel怎么跳转到Android:linux与安卓的交界
【系统启动】Kernel怎么跳转到Android:linux与安卓的交界
46 0
|
4月前
|
数据库 Android开发
Android Studio开发之应用组件Application的讲解及实战(附源码,通过图书管理信息系统实战)
Android Studio开发之应用组件Application的讲解及实战(附源码,通过图书管理信息系统实战)
49 0
|
4月前
|
JSON 自然语言处理 Java
Android App开发语音处理之系统自带的语音引擎、文字转语音、语音识别的讲解及实战(超详细 附源码)
Android App开发语音处理之系统自带的语音引擎、文字转语音、语音识别的讲解及实战(超详细 附源码)
120 0
|
18天前
|
机器学习/深度学习 人工智能 搜索推荐
探索安卓应用中的新趋势:人工智能驱动的智能推荐系统
传统的应用推荐系统已经无法满足用户日益增长的个性化需求。本文将探讨如何通过引入人工智能技术,构建智能推荐系统,为用户提供更加精准、个性化的应用推荐体验,进而提升应用的用户满意度和留存率。
17 0
|
1月前
|
搜索推荐 测试技术 定位技术
基于Android的自助导游系统的设计与实现(论文+源码)_kaic
基于Android的自助导游系统的设计与实现(论文+源码)_kaic
|
1月前
|
搜索推荐 安全 Android开发
安卓与iOS系统的用户体验比较
【2月更文挑战第11天】 在当今移动设备市场上,安卓和iOS系统一直是两大主流操作系统。本文将从用户界面设计、应用生态、系统定制性等方面对安卓和iOS系统进行比较分析,旨在探讨两者的优势和劣势,为用户选择合适的操作系统提供参考。
|
2月前
|
人工智能 vr&ar Android开发
探索安卓与iOS系统的发展趋势
【2月更文挑战第9天】 过去,人们对于安卓和iOS系统的争论主要集中在性能、用户体验和生态系统的比较上。然而,随着移动互联网的快速发展,两大操作系统在人工智能、物联网、安全性等方面的发展趋势也备受关注。本文将探讨安卓与iOS系统在技术发展方面的差异以及未来的发展趋势。