android 通知Notification详解及实例代码
作者:lqh 发布时间:2023-06-26 12:11:42
标签:android,Notification
android Notification实例详解
1.使用Builder模式来创建
2.必须要设置一个smallIcon,还可以设置setTicker
3.可以设置 setContentTitle,setContentInfo,setContentText,setWhen
4.可以设置setDefaults(闪屏,声音,震动),通过Notification设置flags(能不能被清除)
5.发送需要获取一个NotificationManager(getSystemService来获取);notify(int id,Notification)
6.清除 manager.cancelAll(清除所有) cancal(int id); 如果需要16一下的api也能访问,需要导入v4包,使用notificationCompat兼容类。
自定义通知
使用RemoteViews来建立一个布局,然后使用setContent()设置;
点击事件使用PendingIntent来完成
下面是一个案例
MainActivity类
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void clearNotification(View v) {
// 通过代码来清除 NO_CLEAR
// 清除也需要manager
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
// 只能清除由本应用程序发出去的通知
manager.cancelAll();
}
public void sendNotification(View v) {
// 他是 在v4包中的通知,用于16以下版本发送通知
// NotificationCompat
// 通知的创建
Notification.Builder builder = new Notification.Builder(this);
// NotificationCompat.Builder builder2 = new NotificationCompat.Builder(
// this);
// 显示在通知条上的图标 必须的
builder.setSmallIcon(R.drawable.ic_launcher);
// 显示通知条上的文字 不是必须的
builder.setTicker("您有一条新的消息!!");
// 状态栏中的
builder.setContentTitle("大标题");
builder.setContentText("文本");
builder.setWhen(System.currentTimeMillis());
builder.setContentInfo("Info");
builder.setDefaults(Notification.DEFAULT_ALL);
// 点击事件使用Pending
Intent intent = new Intent(this, MainActivity.class);
PendingIntent pi = PendingIntent.getActivity(this, 1, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(pi);
// 生成对象 16API以上,支持低版本需要使用v4包中的notificationCompat
Notification notify = builder.build();
// 设置不能清除
notify.flags = Notification.FLAG_NO_CLEAR;
// 如何将通知发送出去
NotificationManager mananger = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
// 通知的唯一值,如果id重复,表明是更新一条通知,而不是新建
mananger.notify((int) (Math.random() * 1000), notify);
}
public void diyNotification(View v) {
// 展示在通知上面的视图
RemoteViews views = new RemoteViews(getPackageName(), R.layout.layout);
Notification notification = new Notification.Builder(this)
.setSmallIcon(R.drawable.ic_launcher).setTicker("自定义通知 ")
// 布局
.setContent(views).build();
// 使用RemoteViews来设置点击事件
views.setTextColor(R.id.tv, Color.RED);
Intent intent = new Intent(this, OneActivity.class);
// 音乐播放是放在Service
PendingIntent pi = PendingIntent.getActivity(this, 2, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
views.setOnClickPendingIntent(R.id.tv, pi);
Intent intent2 = new Intent(this, MainActivity.class);
PendingIntent pi2 = PendingIntent.getActivity(this, 1, intent2,
PendingIntent.FLAG_UPDATE_CURRENT);
views.setOnClickPendingIntent(R.id.iv, pi2);
// 发送
NotificationManager notify = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notify.notify(1, notification);
}
}
OneActivity类
public class OneActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView tv = new TextView(this);
tv.setText("跳转界面");
setContentView(tv);
}
}
activity.main.xml
<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="com.example.lesson8_notification.MainActivity" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="sendNotification"
android:text="普通的通知" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="clearNotification"
android:text="清除所有通知" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="diyNotification"
android:text="自定义通知" />
</LinearLayout>
layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<ImageView
android:id="@+id/iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" />
</LinearLayout>
最后记得注册activity
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
来源:http://blog.csdn.net/dr_abandon/article/details/53121192


猜你喜欢
- 本文实例为大家分享了Android Scroller的使用方法,供大家参考,具体内容如下1、scrollTo和ScrollByView类定义
- Android Notification使用详解Notification核心代码(链式调用):适用于Android 4.0以上(
- 在value目录下,创建styles.xml文件<?xml version="1.0" encoding=&quo
- java 高并发中volatile的实现原理摘要: 在多线程并发编程中synchronized和Volatile都扮演着重要的角色,Vola
- #define Testusing System;namespace Wrox.ProCSharp.ParameterTestSample.
- 1、修改application.properties新建 Mapper、实体类 相应的文件夹,将不同数据源的文件保存到对应的文件夹下# te
- Java的static关键字和C/C++语言的关键字有所不同:一旦在Java里使用了static关键字,那么这样的内容不再属于对象自己,而是
- 最近安装了idea,觉得比eclipse好用很多,今天不知道为啥yml文件就不识别了,上面显示一个问号,我查了半天,解决办法就是安装一个插件
- 问题发现今天发生了一件事,令我非常郁闷,就是我在使用一个SDK时,当我调用他的方法时,提示我方法中的参数var1, var2如下:// 方法
- 前几天在看一个cameraCTSbug时,结果在一个java for循环上有点蒙。正好赶上这个点总结一下。java中的控制结构:条件结构这里
- 本文实例讲述了C#微信公众号开发之接收事件推送与消息排重的方法。分享给大家供大家参考。具体分析如下:微信服务器在5秒内收不到响应会断掉连接,
- 本篇给大家介绍Spring Boot 与 kotlin 使用Thymeleaf模板引擎渲染web视图。静态资源访问在我们开发Web应用的时候
- 代码如下一、创建EdgeLight.xaml代码如下。<ResourceDictionary xmlns="htt
- 判断字符串是否为IP地址通常都是基于正则表达式实现的,无论是引入外部的依赖包亦或是自己写正则实现,基本都是基于正则表达式实现的判断。然而比较
- 1.DRUID连接池介绍Druid是阿里巴巴开发的号称为监控而生的数据库连接池,Druid是目前最好的数据库连接池。在功能、性能、扩展性方面
- 本文实例演示了Java多线程死锁。分享给大家供大家参考,具体如下:package com.damlab.fz;public class De
- 需求某航空公司物流单信息查询,是一个post请求。通过后台模拟POST HTTP请求发现无法获取页面数据,通过查看航空公司网站后,发现网站使
- 适配器(Adapter)模式:适配器模式把一个类的接口变换成客户端所期待的另一种接口,从而使原本因接口不匹配而无法在一起工作的两个类能够在一
- 在Android开发过程中,有时会需要一些消息提示,大多数情况可以用dialog来做,但有些消息不需要用户去点击取消并且不能对用户体验产生影
- 自动注入和@Autowire@Autowire不属于自动注入!注入方式(重要)在Spring官网上(文档),定义了在Spring中的注入方式