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实现,通常在代码中都不会直接 ...
随机推荐
- windows上apache配置php5
windows上apache配置php5 重点:1.php5里的php.ini的extension_dir要改为绝对目录(带'/'斜杠),如果只是写个ext,在apache+mod_php里面是不会加 ...
- Linux系统搭建私有CA证书服务器
一.CA简介 CA是什么?CA是Certificate Authority的简写,从字面意思翻译过来是凭证管理中心,认证授权.它有点类似我们生活中的身份证颁发机构,这里的CA就相当于生活中颁发身份证的 ...
- Tensorflow下指定显卡占用比例参数配置
tensorflow在训练时默认占用所有GPU的显存. 可以通过以下方式解决该问题: 1.在构造tf.Session()时候通过传递tf.GPUOptions作为可选配置参数的一部分来显式地指定需要分 ...
- Data for the People: How to Make Our Post-Privacy Economy Work for You
等翻译成 chinese在看吧
- 使用Route Prefix 使用属性路由 精通ASP-NET-MVC-5-弗瑞曼
- JS-02-js的变量
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- 网络流(最大流-Dinic算法)
摘自https://www.cnblogs.com/SYCstudio/p/7260613.html 网络流定义 在图论中,网络流(Network flow)是指在一个每条边都有容量(Capacity ...
- java容器(一) Collection类框架图解
- 事务管理(ACID)
目录 一.事务管理(ACID) 原子性(Atomicity) 一致性(Consistency) 持久性(Durability) 隔离性(Isolation) 二.事务隔离级别 脏读 不可复读 虚读(幻 ...
- Leetcode 题目整理-1
1. Two Sum Given an array of integers, return indices of the two numbers such that they add up to a ...