Android中使用Notification在状态栏上显示通知
场景
状态栏上显示通知效果

注:
博客:
https://blog.csdn.net/badao_liumang_qizhi
关注公众号
霸道的程序猿
获取编程相关电子书、教程推送与免费下载。
实现
新建NotificationActivity,通过getSystemService方法获取通知管理器。
然后创建通知并设置通知的一些属性,再使用通知管理器发送通知。
package com.badao.relativelayouttest; import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity; import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle; public class NotificationActivity extends AppCompatActivity {
final int NOTIFYID = 0x123; //通知的ID
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_notification);
//新建通知管理器
final NotificationManager notificationManager =
(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
// 创建一个Notification对象
Notification.Builder notification = new Notification.Builder(this);
// 设置打开该通知,该通知自动消失
notification.setAutoCancel(true);
// 设置通知的图标
notification.setSmallIcon(R.drawable.dog);
// 设置通知内容的标题
notification.setContentTitle("还不赶紧关注公众号");
// 设置通知内容
notification.setContentText("点击查看详情!");
//设置使用系统默认的声音、默认震动
notification.setDefaults(Notification.DEFAULT_SOUND
| Notification.DEFAULT_VIBRATE);
//设置发送时间
notification.setWhen(System.currentTimeMillis());
// 创建一个启动其他Activity的Intent
Intent intent = new Intent(NotificationActivity.this
, DetailActivity.class);
PendingIntent pi = PendingIntent.getActivity(
NotificationActivity.this, , intent, );
//设置通知栏点击跳转
notification.setContentIntent(pi);
//发送通知
notificationManager.notify(NOTIFYID, notification.build());
}
}
点击详情时跳转到DetailActivity,设计详情页,显示文本信息
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".DetailActivity"> <TextView
android:layout_width="wrap_content"
android:text="霸道的程序猿"
android:layout_height="wrap_content"/> </LinearLayout>
Android中使用Notification在状态栏上显示通知的更多相关文章
- Android中使用Notification实现进度通知栏(Notification示例三)
我们在使用APP的过程中,软件会偶尔提示我们进行版本更新,我们点击确认更新后,会在通知栏显示下载更新进度(已知长度的进度条)以及安装情况(不确定进度条),这就是我们今天要实现的功能.实现效果如下: 在 ...
- Java乔晓松-android中调用系统拍照功能并显示拍照的图片
android中调用系统拍照功能并显示拍照的图片 如果你是拍照完,利用onActivityResult获取data数据,把data数据转换成Bitmap数据,这样获取到的图片,是拍照的照片的缩略图 代 ...
- JS文件中的中文在网页上显示为乱码解决方法
转自:http://www.pc6.com/infoview/Article_63835.html 如果JS文件中的中文在网页上显示为乱码,不妨采用本文章中的方法来试一试,或许能解决使你很头疼的问题. ...
- Android中的沉浸式状态栏效果
无意间了解到沉浸式状态栏,感觉贼拉的高大上,于是就是试着去了解一下,就有了这篇文章.下面就来了解一下啥叫沉浸式状态栏.传统的手机状态栏是呈现出黑色条状的,有的和手机主界面有很明显的区别.这一样就在一定 ...
- Android中使用Notification实现宽视图通知栏(Notification示例二)
Notification是在你的应用常规界面之外展示的消息.当app让系统发送一个消息的时候,消息首先以图表的形式显示在通知栏.要查看消息的详情需要进入通知抽屉(notificationdrawer) ...
- Android中使用Notification实现普通通知栏(Notification示例一)
Notification是在你的应用常规界面之外展示的消息.当app让系统发送一个消息的时候,消息首先以图表的形式显示在通知栏.要查看消息的详情需要进入通知抽屉(notificationdrawer) ...
- 在 Xamarin.Android 中使用 Notification.Builder 构建通知
0 背景 在 Android 4.0 以后,系统支持一种更先进的 Notification.Builder 类来发送通知.但 Xamarin 文档含糊其辞,多方搜索无果,遂决定自己摸索. 之前的代码: ...
- Android中使用POI加载与显示word文档
最近打算实现一个功能:在Android中加载显示Word文档,当然这里不是使用外部程序打开.查看一些资料后,打算采用poi实现,确定了以下实现思路: 将ftp中的word文档下载到本地. 调用poi将 ...
- Android中Drawable分类汇总(上)
Android把可绘制的对象抽象为Drawable,不同的图形图像资源就代表着不同的drawable类型.Android FrameWork提供了一些具体的Drawable实现,通常在代码中都不会直接 ...
随机推荐
- 20200104模拟赛 问题A 图样
题目 分析: 老规矩,遇到期望要准备好随时投降... 大致想到了按位处理,然后分别下去搜索,再用组合数加加减减一下... 但是两个连通块之间连边的期望怎么算呢? 很好,投降... 下来看题解... 果 ...
- 团队项目——Beta冲刺
团队项目-Beta冲刺 作业所属课程 软件工程 作业要求 团队项目-Beta冲刺 团队名称 运气王团队 作业目标 (1)SCRUM部分(2)PM 报告 成员列表: 1.团队成员的学号列表 |何宸锐(组 ...
- linux---> siege压力测试工具使用
siege压力测试工具使用 安装 下载地址 : http://download.joedog.org/siege/ CaodeMacBook-Pro:local root# tar -xzvf sie ...
- 配置虚拟环境(virtualenv+virtualenvwrapper)
安装virtualenv pip3 install virtualenv 安装virtualenvwrapper pip install virtualenvwrapper # Linux使用该条 p ...
- Python变量、方法、类的命名规则
1. 变量命名总结: - 1.单下划线开头变量:protected - 2.双下划线开头变量:private - 3.双下划线开头,双下划线结尾:系统内置变量 2. 函数命名总结: - 1.私有方法: ...
- 【译】SQ3R学习法则
SQ3R 观察-提问-阅读-复述-回顾 背景 SQ3R是一种理解策略,可帮助学生在阅读时思考他们正在阅读的文章. SQ3R通常被归类为学习策略,通过教导学生在初次阅读一篇文章时如何阅读和像高效读者一样 ...
- OpenCV3入门1—环境搭建与实验
1.环境搭建 1.1 VS2017开发环境搭建 1).下载软件包 https://opencv.org/ 2).配置环境变量 配置win10系统环境变量,把下面路径添加到path. D:\WORK\5 ...
- cpu负载高简单排查思路
首先通过uptime查看系统负载,然后使用mpstat结合pidstat来初步判断到底是cpu计算量大还是进程争抢过大或者是io过多,接着使用vmstat分析切换次数,以及切换类型,来进一步判断到底是 ...
- 练习2-13 求N分之一序列前N项和 (15 分)
练习2-13 求N分之一序列前N项和 (15 分) 输入在一行中给出一个正整数N. 输出格式: 在一行中按照“sum = S”的格式输出部分和的值S,精确到小数点后6位.题目保证计算结果不超过双精度范 ...
- non-local static 变量初始化顺序不确定,带来的问题
所谓static对象,其寿命从被构造出来直到程序结束为止,因此stack和heap-based对象都被排除.这种对象包括global对象.定义于namespace作用域内的对象,classes内.在函 ...