Android学习之Notification的简单使用

手把手教你搞定安陆FPGA开发环境:从软件安装到AL-LINK驱动配置 本文详细介绍了安陆FPGA开发环境的完整配置流程,包括TangDynasty软件安装、license许可配置以及AL-LINK调试器驱动的安装与调试。通过实战经验和疑难问题解决方案,帮助开发者快速搭建稳定的开发环境,提升FPGA项目开发效率。 阅读详情

这次来谈谈notification,notification一般用在电话,短信,邮件,闹钟铃声等,在手机的状态栏上就会出现一个小图标,提示用户处理这个快讯,这时手从上方滑动状态栏就可以展开并处理这个快讯。

废话不多说,上程序:

1 主界面布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".NotificationAct" >

    <Button 
        android:id="@+id/btn_1"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:text="较老方法"
        />
    
    <Button 
        android:id="@+id/btn_2"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:text="官方推荐"
        />
    
    <Button 
        android:id="@+id/btn_3"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:text="自定义"
        />

</LinearLayout>
效果图如下:


2 主程序代码

package com.example.notificationact;

import android.os.Bundle;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.TaskStackBuilder;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.RemoteViews;

public class NotificationAct extends Activity {

	private Button btn_1 = null;
	private Button btn_2 = null;
	private Button btn_3 = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.act_notification);

		buildBtn();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.act_notification, menu);
		return true;
	}

	public void buildBtn() {
		btn_1 = (Button) findViewById(R.id.btn_1);
		btn_2 = (Button) findViewById(R.id.btn_2);
		btn_3 = (Button) findViewById(R.id.btn_3);

		btn_1.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				showNotification("今天下雪", "很大的雪", "不适合出行",
						R.drawable.ic_launcher, R.drawable.ic_launcher);
			}
		});

		btn_2.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				showNotificationByNotificationCompat();
			}
		});

		btn_3.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				showMusicPlayerNotification("天天动听", R.drawable.appwidget_icon,
						R.drawable.appwidget_icon, R.drawable.appwidget_icon,
						"蝴蝶", "胡彦斌");
			}
		});
	}
    
	/**
	 * 较老的方法
	 * @param tickerText
	 * @param contentTitle
	 * @param contentText
	 * @param id
	 * @param resId
	 */
	public void showNotification(String tickerText, String contentTitle,
			String contentText, int id, int resId) {
		NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

		Notification notification = new Notification(resId, tickerText,
				System.currentTimeMillis());
		notification.defaults = Notification.DEFAULT_VIBRATE;// 使用默认的震动

		PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
				getIntent(), 0);

		notification.setLatestEventInfo(this, contentTitle, contentText,
				contentIntent);

		notificationManager.notify(id, notification);
	}
    
	/**
	 * 使用官方推荐方法
	 */
	public void showNotificationByNotificationCompat() {
		Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
				R.drawable.ic_launcher_mail);

		NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
				this).setLargeIcon(bitmap)
				.setSmallIcon(R.drawable.ic_menu_compose_normal_holo_light)
				.setContentTitle("您有5封邮件").setContentText("baidu@gmail.com")
				.setContentInfo("12").setWhen(System.currentTimeMillis())
				.setAutoCancel(false);
		// Creates an explicit intent for an Activity in your app
		Intent resultIntent = new Intent(this, NotificationAct.class);

		// The stack builder object will contain an artificial back stack for
		// the
		// started Activity.
		// This ensures that navigating backward from the Activity leads out of
		// your application to the Home screen.
		TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
		// Adds the back stack for the Intent (but not the Intent itself)
		stackBuilder.addParentStack(NotificationAct.class);
		// Adds the Intent that starts the Activity to the top of the stack
		stackBuilder.addNextIntent(resultIntent);
		PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,
				PendingIntent.FLAG_UPDATE_CURRENT);
		mBuilder.setContentIntent(resultPendingIntent);
		NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
		// mId allows you to update the notification later on.
		mNotificationManager.notify(1, mBuilder.build());
	}
	
	/**
	 * 自定义的notification,仿天天动听
	 * 
	 * @param tickerText
	 * @param id
	 * @param resId
	 * @param photoId
	 * @param songName
	 * @param singerName
	 */
	public void showMusicPlayerNotification(String tickerText, int id,
			int resId, int photoId, String songName, String singerName) {
		NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

		Notification notification = new Notification(resId, tickerText,
				System.currentTimeMillis());

		PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
				getIntent(), 0);

		// 自定义界面
		RemoteViews remoteView = new RemoteViews(getPackageName(),
				R.layout.notification);
		remoteView.setTextViewText(R.id.tv_song_name, songName);
		remoteView.setTextViewText(R.id.tv_singer_name, singerName);
		remoteView.setImageViewResource(R.id.iv_singer_photo,
				R.drawable.appwidget_icon);
		notification.contentView = remoteView;

		notificationManager.notify(id, notification);
	}

	
}
其中第三个函数 showMusicPlayerNotification( )为自定义View的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="64dp" >

    <ImageView
        android:id="@+id/iv_singer_photo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:src="@drawable/appwidget_icon" />

    <LinearLayout
        android:id="@+id/ll_song_operation"
        android:layout_width="96dp"
        android:layout_height="fill_parent"
        android:layout_alignParentRight="true"
        android:layout_marginRight="5dp"
        android:gravity="center"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/btn_previous"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@drawable/nc_previoussong_pressed" />

        <Button
            android:id="@+id/btn_pause"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@drawable/nc_pause_pressed" />

        <Button
            android:id="@+id/btn_next"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@drawable/nc_nextsong_pressed" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/ll_song_detail"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_marginLeft="5dp"
        android:layout_toLeftOf="@id/ll_song_operation"
        android:layout_toRightOf="@id/iv_singer_photo"
        android:gravity="center_vertical"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/tv_song_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:singleLine="true"
            android:ellipsize="end"
            android:text="蝴蝶"
            android:textColor="#000000"
            android:textSize="18sp"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/tv_singer_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="胡彦斌"
            android:textColor="#696969"
            android:textSize="13sp" />
    </LinearLayout>

</RelativeLayout>


最终运行程序,效果图如下:



3 结语

上面所述是使用notification的基本用法,其中showNotificationByNotificationCompat()函数中的setWhen()方法不起作用,我也不知道为啥,有知道的童鞋请不吝赐教。关于官方推荐的请参考developer.android.com-Notification
对于Notification的更高级的用法,等到仔细研究后再进行补充~



安路TD软件+EG4S20开发板:新手避坑指南,从驱动安装到流水灯下载一次成功 本文详细介绍了使用安路TD软件和EG4S20开发板进行FPGA开发的完整流程,从驱动安装到流水灯实验的避坑指南。重点解决了Windows环境下的常见问题,包括驱动安装失败、约束文件配置错误等,帮助新手快速上手FPGA开发。 阅读详情

相关推荐

如何使用Notification的相关Style实现不同类型的Notification

Notification基本上是每个商用APP都会使用的一个用户交互控件(其实我也不是很清楚Notification到底是属于控件还是UI还是都包含了),好了,这不影响下面继续介绍如何正确的使用Notification!!!好了,废话不多少,从这篇博客中您可以学到如何使用Notification,使用Notification的BigText样式、bigPicture样式、Inbox样式等

朱玉强的博客 3606

Android篇——Notification的自定义布局的使用和事件绑定(4.1+)

Notification的自定义布局的使用和事件绑定(4.1+)

u011391361的博客 2078

【AI基础学习系列】二十一、一文搞懂GPT

本文系统介绍了GPT(Generative Pre-trained Transformer)模型的核心内容。首先阐述了GPT的本质是基于Transformer的预训练语言模型,其创新在于使用单向Transformer编码器。其次详细解析了GPT架构,包括输入层的Tokenization和Embedding处理、Transformer解码器堆叠的核心组件(自注意力机制、前馈神经网络等)以及预训练和微调两阶段训练过程。接着说明了GPT的工作原理:通过无监督预训练学习语言统计规律,再结合有监督微调适应具体任务。

ASH_GUARDIAN的博客 1692

Android10.0通知Notification的使用这一篇就够了

文章目录前言通知概述不同android版本上通知功能通知的结构创建通知1.创建渠道2.设置点击事件3.构造Notification对象并显示通知通知的操作通知渠道通知的级别 前言 最近一段时间写的都是android源码的文章,前几天在公司做了一个需求是关于前台服务的,在写前台服务的时候深入使用到了通知,今天就先写一篇文章总结一下通知的相关内容,后面有时间了在介绍一下前台服务的相关内容。 通知概述 ...

一只特立独行的猪 5万+

Omnibox 2.11 NAS部署指南:家庭影音中枢的轻量化落地实践

家庭影音平台本质是本地化内容调度中枢,其核心在于聚合NAS媒体库、统一管理直播/IPTV/音频/视频等多源内容,并通过智能元数据匹配与硬解加速实现零门槛播放体验。技术原理上依赖Java轻量Web容器(Jetty)、SQLite嵌入式数据库双缓存架构,以及宿主机FFmpeg动态链接与HTTP/2直播代理治理能力。该方案显著降低4K播放卡顿、EPG加载失败、海报缺失等高频问题,适用于群晖、飞牛、绿联等主流NAS设备,尤其适配ARM64芯片与Realtek网卡等国产硬件生态。本文聚焦Omnibox 2.11在真实

weixin_30846347的博客 478

YOLOv11 模型改进全流程:从代码修改到性能跃升,让你打造专属检测利器

无论是科研中需要发表创新点满满的论文,还是企业里要解决特定场景的检测难题,这套方法都能让你在YOLOv11的基础上打造出“量身定制”的检测模型。别再让通用模型的“平庸性能”限制你的项目上限——现在就动手修改代码,让你的YOLOv11模型在精度、速度或小样本适应能力上实现跨越式提升,用技术突破证明你的AI研发实力!本文将带你从代码层面入手,完成从骨干网络到损失函数的全维度改进,真正掌握让YOLOv11“为你所用”的技术实力。该改进对小样本类别(如数据集中仅占5%的“稀有缺陷”)的识别精度提升显著。

m0_71589036的博客 406

Android-Notification 通知使用方法

Android 8.0以上通知用法及进阶,解决通知等级设为高了仍不显示消息横幅问题

qq_43691887的博客 1617

基于数字图像处理方法的迷宫问题图解 : 基于骨架提取、四邻域

问题是,这条红色的线是人为标记出来的能通的路线,对计算机来看,黑色和红色的线没有任何差异,所以之前的想法压根不起作用。对应于数字图像的表达,就是从远离主干的分支端点出发,如果4邻域中只有一个可以通的道路,那么这个点在分支上。闪电划过,有一道连接了天空和大地,就是迷宫的解,其余的分支就是迷宫中对应的死路。类似这个闪电图,假设图中最粗的主干(迷宫解的路线)是最先形成的,其余的细小的分支是慢慢从主干上形成的。因此,只要在端点的4邻域中,找到可以走的道路,那么一定就是分支,也就是要移除的路线。

Henry的博客 2078

Android Notification使用

  在应用的开发中,我们必然会接触到应用通知这个知识,而在通知是随着Android版本进行不断变化,为了能在高版本和低版本中使用,就需要开发者去做适配,也属于查漏补缺。了解之前,先看一个效果图吧。   通知的使用的内容还是比较多的,此篇文章将会尽可能详细的介绍Notification的内容。下面就开始我们的适配之旅吧。新建一个名为NotificationStudy的项目,如下图所示:  点击Finish完成项目创建,然后在app的build.gradle中的android{}闭包中开启viewBinding

晨曦 7580

STM32(HAL库)驱动OLED

通过STM32F103C8T6单片机采用HAL库方式对0.96寸OLED(IIC通讯)屏幕进行驱动。

weixin_44597885的博客 8817

1、初探 Tiny Machine Learning

Tiny Machine Learning(TinyML)是机器学习与嵌入式系统的融合领域,致力于在资源受限的微控制器上运行AI模型。本文介绍了机器学习基础、TinyML的核心概念、应用领域(如智能家居、工业物联网、医疗保健)、面临的挑战(如资源限制、模型压缩、数据质量),以及从硬件准备到模型部署的实践步骤。通过利用训练与推理的资源不对称性,可在PC上训练模型并部署至低功耗设备进行实时推理,推动边缘智能的发展。

loss4bartender的博客 130

Notification使用详解之一:基础应用

在消息通知时,我们经常用到两个组件Toast和Notification。特别是重要的和需要长时间显示的信息,用Notification就最合适不过了。当有消息通知时,状态栏会显示通知的图标和文字,通过下拉状态栏,就可以看到通知信息了,Android这一创新性的UI组件赢得了用户的一致好评,就连苹果也开始模仿了。今天我们就结合实例,探讨一下Notification具体的使用方法。 首先说明一下我们需...

t225com 302

2023年全国城市房价分布网格数据

本文介绍了一份2023年全国主要城市房价分布的网格数据,数据以250米×250米的网格为单位,记录了住宅小区数量、均价、最高价和最低价等信息。数据采用GCJ-02坐标系,提供ShapeFile、GeoJSON和CSV三种格式

J_Giser的博客 390

Android之Notification的多种用法

我们在用手机的时候,如果来了短信,而我们没有点击查看的话,是不是在手机的最上边的状态栏里有一个短信的小图标提示啊?你是不是也想实现这种功能呢?今天的Notification就是解决这个问题的。        我们也知道Android系统也是在不断升级的,有关Notification的用法也就有很多种,有的方法已经被android抛弃了,现在我实现了三种不同的方法,并适应不同的android版本。

非著名程序员 13万+

【机器学习-09】特征工程

特征工程是机器学习模型构建的初步工作,但它的重要性却贯穿整个模型生命周期。在进行特征工程时,我们需要充分利用数据科学的知识和技巧,结合具体的应用场景和业务需求,来制定出最合适的特征提取和转换策略。只有这样,我们才能为机器学习模型提供最有价值的输入,从而使其发挥出最大的潜力。总之,特征工程是机器学习中的一项关键任务,它需要我们用心去探索、去实践、去创新。只有不断地优化和完善特征工程的过程,我们才能构建出更加准确、更加可靠的机器学习模型,为实际应用带来更大的价值。

qq_38614074的博客 2万+

廖雪峰Python教程[完整版]

廖雪峰的python教程整理的,已经排好版了,而且还是完整的,适合小白

Android如何使用Notification进行通知

有两张图片素材会放在末尾 activity代码,和XML布局 package com.example.myapplication; import androidx.appcompat.app.AppCompatActivity; import androidx.core.app.NotificationCompat; import android.app.Notification; impor...

南 墙 1万+

RU DOS版、EFI版32bit/64bit BIOS常用工具

DOS版、EFI版的RU工具,可用来查看IO、MEM、PCI、SMBIOS等资源。亲测在EFI SHELL下可用。

Notification 通知使用方法详解

通知是应用向用户显示的消息提示,当发送通知时,通知将先以图标的形式显示在通知区域中。用户可以打开下拉通知栏查看通知的详细信息。 通知区域和下拉通知栏均是由系统控制的区域,...

程序员Android 1136

贝叶斯统计 [韦来生编]

贝叶斯(Bayes)统计是近几十年来迅速发展起来的数理统计的一个重要分支。贝叶斯方法与经典统计方法的主要不同之处是进行统计推断时除了利用样本信息外,还要利用参数的先验信息,因此可以提高统计推断或统计决策的效果。它在经济、金融、生物、医学、自然科学和社会科学等许多领域具有广泛的应用。贝叶斯方法的研究已渗透到了统计学的几乎所有领域。作者在给中国科学技术大学概率论与数理统计专业本科生开设的“数理统计”课程中有一章专门讲授“贝叶斯方法和统计决策理论”,近十年来也给中国科学技术大学概率统计专业研究生开设过几次“贝叶斯分析”课。本书是在对过去讲稿的内容作了适当的增减和调整的基础上完成的。  本书共分六章。第1章是绪论,介绍了贝叶斯统计的若干基本概念,同时对必要的数理统计的基础知识有重点地作了回顾。第二章介绍了确定先验分布的若干可供选择的方法。第三章介绍了几类常见统计模型参数的后验分布的主要结果和计算方法。第四章和第五章分别介绍了“贝叶斯统计推断”和“贝叶斯统计决策”的内容。第六章介绍了贝叶斯统计计算的若干方法,包括蒙特卡洛方法和马尔可夫链蒙特卡洛(MCMC)方法以及相关统计软件的简介。本书的每一章附右大量的例题和习题,书末附有常用的几个表格和部分习题解答供读者查用。  大约可在54学时内讲授本书的第1章至第六章的主要内容,教师可根据学时需要选讲本书第1章至第六章的部分内容。书中标“*”号的章节可略去不讲,留给读者作为阅读材料。第六章例题中的R代码和部分例题的数据文件等可从作者个人主页下载。  作者在编写本书的过程中参考了J.O.Berger教授的《统计决策论及贝叶斯分析》和茆诗松教授编写的《贝叶斯统计》等书中的一些内容,以及《贝叶斯分析》一书中由张伟平博士编写的有关贝叶斯统计计算的一些内容。在此表示衷心感谢。  本书编写准备过程中,中国科学技术大学统计与金融系的几个研究生帮助完成了本书部分中文的录入和排版,作者对他们的辛勤工作表示真诚的感谢。高等教育出版社为本书的出版给予了大力支持。在此一并致谢。

VB摄像头监控

该VB模块可对摄像头捕获的画面进行录像等,可在创建的窗口进行控制

AI在辅助生殖中的应用:从胚胎图像分析到妊娠预测的多模态融合技术

深度学习作为人工智能的核心技术,通过卷积神经网络(CNN)和Transformer等模型,能够从图像、序列等多模态数据中自动提取深层特征与模式。其技术价值在于将主观、经验驱动的决策过程转化为客观、可量化的数据驱动分析,显著提升复杂场景下的决策一致性与效率。在医疗健康领域,这一能力被广泛应用于医学影像分析、疾病预测等场景。具体到辅助生殖技术(ART),AI通过处理胚胎静态图像、时序发育影像,并结合临床结构化数据,实现了对胚胎质量的自动化分级与妊娠成功率的精准预测。这为解决传统胚胎评估中主观性强、标准不一的问题

weixin_30616969的博客 1097

Android通知(Notification)详细解读一——基本使用

概述 Notification,是一种具有全局效果的通知,可以在系统的通知栏中显示。当 APP 向系统发出通知时,它将先以图标的形式显示在通知栏中。用户可以下拉通知栏查看通知的详细信息。通知栏和抽屉式通知栏均是由系统控制,用户可以随时查看。 添加支持库 创建基本通知 设置通知内容 更新通知 删除通知 给 Notification 设置 Action ...

心田祭思博客 7188

Android Notification 的四种使用方式

实现通知步骤 一搬实现通知需要如下步骤: 1.获取 NotificationManager 实例管理通知; 2.实例 Notification 对象; 3.管理事件 Intent; 4.发送通知。

生活就是历史,永远没有如果。 1万+

Notification的使用

安卓创建通知流程介绍

qq_45763513的博客 798

Notification的基本用法

本篇博客主要介绍通知栏的基本用法,包括进度条、通知的更新、删除,点击事件的设定等,

朱玉强的博客 1784

安卓 Notification 使用方法小结

本文简单介绍安卓 Notification 的用法,并简单对比 5.0 前后 Notification 的变化和处理方法。

关注移动端、Web、微信小程序等大前端领域 1万+

Notification 使用方法详解

通知是应用向用户显示的消息提示,当发送通知时,通知将先以图标的形式显示在通知区域中。用户可以打开下拉通知栏查看通知的详细信息。 通知区域和下拉通知栏均是由系统控制的区域,用户可以随时查看。 Notification 简介 通知的创建 通知的管理 简单的通知 可以 扩展的通知 通知中含下载进度条 通知中含媒体播放控件 自定义通知内容 欢迎关注微信公众号:程序员Android ...

程序员Android 2481

Android中Notification的简单使用

今天整个Notification,写哪哪报错,点哪哪不行。。(当然最终效果还是出来了) 所以我必须要说一下Notification最简单的使用 Notification简介: 在状态栏显示提示信息,除非用户查看或关闭信息,状态栏才取消显示 一般使用在收到短信后、收到应用的推送消息后、收到未接电话等场合 使用时需要借助NotificationManager(通知管理器)来实现 Notification使用基本流程 1.获取通知管理器服务(NotificationManager); NotificationMa

sls666 2288

【转】如何使用Notification功能

参考资料: http://www.ablanxue.com/prone_8980_1.html http://my.oschina.net/u/1395677/blog/176823

shenlong_no1的博客 330
上一篇: Android学习之Toast
下一篇: Android学习之ActionBar(仿Gmail主界面)
彩虹之西
博客等级 码龄16年 18粉丝 133原创
评论 2
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值