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实现,通常在代码中都不会直接 ...
随机推荐
- Java中的循环语句
1.1 while 循环语句 while 语句也称为条件判断语句. 循环方式 : 利用一个条件来控制是否要反复执行这个语句. 语法 : while(条件表达式){ 执行语句 } 当条件表达式的返回值为 ...
- Python3之json文件操作
json函数 使用json函数之前,首先需要导入json模块,import json 1).json.dumps()函数 该函数是将 Python 对象编码成 JSON 字符串,例如: import ...
- CSS-12-盒子模型
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- ios--->特定构造方法NS_DESIGNATED_INITIALIZER
特定构造方法 1> 后面带有NS_DESIGNATED_INITIALIZER的方法,就是特定构造方法 2> 子类如果重写了父类的[特定构造方法],那么必须用super调用父类的[特定构造 ...
- mysql --->mysql 外键总结
mysql 外键总结 1.设置外键MySQL ERROR 1005 错误 MySQL ERROR 1005 (主要是约束不一样导致的)例如: 1.两表外键的引用类型不一样,如主键是int外键是char ...
- 强大的Guava中的新集合类型: Multiset, Multimap, BiMap, Table, ClassToInstanceMap, RangeSet, RangeMap等
一 Multiset /** * 新类型集合: Multiset: Multiset就是可以保存多个相同的对象,并且无序 * 占据了List和Set之间的一个灰色地带 * 其他实现: TreeMult ...
- Maven 项目无法在Ecplise加进tomcat server
当把用Maven项目 加进 tomcat server 时,出现 "There are no resources that can be added or removed from the ...
- Unicode标准以及其常见的编码方案
目录 基本概念 码位 码位的类型 编码方案 UTF-32 UTF-16 UTF-8 参考资料 Unicode标准为每一个字符提供一个唯一的数字,而不用区分平台.语言等因素. The Unicode S ...
- vuex之getter(二)
说明 使用vue,如果想对data数据派生一些状态,我们就用到计算属性或者侦听器,同样vux想要派生state中的一些状态,可以在store中定义一个getters属性,它相当于state的计算属性. ...
- Codeforces 1296F Berland Beauty
题目链接:http://codeforces.com/problemset/problem/1296/F 思路: 1————2————3————4————5————6 1->3 2 2-> ...