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实现,通常在代码中都不会直接 ...
随机推荐
- 修改 Apache 的默认端口
修改 Apache 的默认端口修改 Apache 端口的方法是最妥协的方法了,后果是当你开启 Apache 服务器的时候,访问的本机地址将会附带端口号,比如 http://localhost:8888 ...
- OpenCV2.4.13+Qt5.6.2配置方法
[1.环境变量] D:\Soft\OpenCV2\MinGW_build\bin; C:\Qt\Qt5.6.2\Tools\mingw492_32\bin; D:\Soft\Programming\C ...
- 机器学习-TensorFlow应用之classification和ROC curve
概述 前面几节讲的是linear regression的内容,这里咱们再讲一个非常常用的一种模型那就是classification,classification顾名思义就是分类的意思,在实际的情况是非 ...
- CSS-16-margin值重叠问题
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- Codeforces_812
A. 每条人行道有六条车道会撞到. #include<bits/stdc++.h> using namespace std; ],b[],c[],d[]; int main() { ios ...
- CCF_201604-1_折点计数
(a[i]-a[i-1])*(a[i]-a[i+1]) > 0 的点符合条件 #include<cstdio> #include<iostream> using name ...
- JavaWeb前置知识 : 动态和静态的区别、两种架构、常见状态码
JavaWeb程序设计(一) : 前置知识 1.动态网页与静态网页的区别: a.不要和是否有"动感"混为一谈. b.是否随着时间.地点.用户操作的改变而改变 (例如 : 在百度上搜 ...
- C++ map通过key获取value
c++的map中通过key获取value的方法 一般是value =map[key],或者另一种迭代器的方式 1.在map中,由key查找value时,首先要判断map中是否包含key. 2.如果不 ...
- NR / 5G - Polar Coding
5G New Radio Polar Coding Introduction The selection of polar codes as the channel coding technique ...
- Python3(六) 面向对象
一.类的定义 1. class Student(): name = ' ' #定义变量 age = 0 def print_file(self): #定义函数 ...