Android编程开发之NotiFication用法详解
作者:sgx425021234 发布时间:2023-01-19 22:50:11
本文实例讲述了Android编程开发之NotiFication用法。分享给大家供大家参考,具体如下:
notification就是通知的意思,安卓中指通知栏,一般用在电话,短信,邮件,闹钟铃声,在手机的状态栏上就会出现一个小图标,提示用户处理这个快讯,这时手从上方滑动状态栏就可以展开并处理这个快讯。
在帮助文档中,是这么说的, notification类表示一个持久的通知,将提交给用户使用NotificationManager。已添加的Notification.Builder,使其更容易构建通知。
notification是一种让你的应用程序在没有开启情况下或在后台运行警示用户。它是看不见的程序组件(Broadcast Receiver,Service和不活跃的Activity)警示用户有需要注意的事件发生的最好途径。
先来区分以下状态栏和状态条的区别:
1、状态条就是手机屏幕最上方的一个条形状的区域;
在状态条有好多信息量:比如usb连接图标,手机信号图标,电池电量图标,时间图标等等;
2、状态栏就是手从状态条滑下来的可以伸缩的view;
在状态栏中一般有两类(使用FLAG_标记):
(1)正在进行的程序; (2)是通知事件;
一个Notification传送的信息有:
1、一个状态条图标;
2、在拉伸的状态栏窗口中显示带有大标题,小标题,图标的信息,并且有处理该点击事件:比如调用该程序的入口类;
3、闪光,LED,或者震动;
下面对Notification类中的一些常量,字段,方法简单介绍一下:
常量:
DEFAULT_ALL 使用所有默认值,比如声音,震动,闪屏等等
DEFAULT_LIGHTS 使用默认闪光提示
DEFAULT_SOUNDS 使用默认提示声音
DEFAULT_VIBRATE 使用默认手机震动
注意:在加入手机震动效果时一定要在项目清单文件中加入手机震动权限:
<uses-permission android:name="android.permission.VIBRATE" />
下面通过简单额小实例来具体实现notification功能:
MainActivity.java:
package com.example.lession16_notifi;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.res.Resources.NotFoundException;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends Activity {
private NotificationManager notificationManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 第一步:通过getSystemService()方法得到NotificationManager对象;
notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
}
// 测试
public void test1(View v) {
showNotification("来短信了", "5557", "hello!", R.drawable.ic_launcher,
R.drawable.ic_launcher);
}
// 第二步:对Notification的一些属性进行设置比如:内容,图标,标题,相应notification的动作进行处理等等;
public void showNotification(String tickerText, String contentTitle,
String contentText, int iconId, int notiId) {
// 创建一个Notification
Notification notification = new Notification();
// 设置通知 消息 图标
notification.icon = iconId;
// 设置发出消息的内容
notification.tickerText = tickerText;
// 设置发出通知的时间
notification.when = System.currentTimeMillis();
// 设置显示通知时的默认的发声、振动、Light效果
notification.defaults = Notification.DEFAULT_VIBRATE;// 振动
// Notification notification = new Notification(R.drawable.ic_launcher,"有新的消息", System.currentTimeMillis());
// 3步:PendingIntent android系统负责维护
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,getIntent(), 0);
// 4步:设置更加详细的信息
notification.setLatestEventInfo(this, contentTitle, contentText,pendingIntent);
// 5步:使用notificationManager对象的notify方法 显示Notification消息 需要制定
// Notification的标识
notificationManager.notify(notiId, notification);
}
// 6步:使用notificationManager对象的cancelAll()方法取消
public void clearNoti(View v) {
notificationManager.cancelAll();// 清除所有
}
}
布局文件activity_main.xml:
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginTop="22dp"
android:onClick="test1"
android:text="@string/text_notifi" />
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button1"
android:layout_below="@+id/button1"
android:layout_marginTop="60dp"
android:onClick="clearNoti"
android:text="@string/text_clear" />
</RelativeLayout>
布局效果:
切记实现震动效果在清单文件中加入权限:
<uses-permission android:name="android.permission.VIBRATE" />
实现效果如下:
希望本文所述对大家Android程序设计有所帮助。


猜你喜欢
- 逻辑描述:现在我们想在B层和D层加上接口层,并使用工厂。而我们可以将创建B和创建D看作是两个系列,然后就可以使用抽象工厂进行创建了。配置文件
- RocketMQ 是什么Github 上关于 RocketMQ 的介绍:RcoketMQ 是一款低延迟、高可靠、可伸缩、易于使用的消息中间件
- 1. web.xml中配置了CharacterEncodingFilter,配置这个是拦截所有的资源并设置好编号格式。encoding设置成
- 引出泛型我们通过如下的示例,引出为什么泛型的概念。public class Test {public static void main(St
- Android手机或平板都会存在横竖屏切换的功能,通常是由物理重力感应触发的,但是有时候也不尽然,通常在设置里面我们可以对手机的横竖屏切换进
- mybatis简单的CURD就不用多说了,网上相关博客文档一大堆。分析一下Mybatis里面的collection聚集查询。 假设一个班级有
- Scala小程序详解1. 交互式模式在命令行窗口中,输入Scala命令:xiaosi@Qunar:~$ scalaWelcome to S
- 先给大家展示下效果图,大家觉效果满意,请参考实现代码。直接上代码:private void setDialog(){View view =
- 在Java中如果一个类同时继承接口A与B,并且这两个接口中具有同名方法,会怎么样?动手做实验:interface A{ void
- 一、选择结构大纲if单选择结构if双选择结构if多选择结构嵌套的if结构switch多选择结构二、if单选择结构我们很多时候需要去判断一个东
- 这个功能,大家也都可以去百度以下,千篇一律都自己写的(抄的)封装好的公共类,此处还是得膜拜下原创的大佬,可以花时间去搞这个,我看着都头皮发麻
- 前言本篇我们就来讲讲Fragment管理中的 Add() 方法Add()在我们动态的添加、管理Fragment中,Add属于最基础的方法了;
- 之前使用OnSharedPreferenceChangeListener,遇到了点小问题,就是有些时候OnSharedPreferenceC
- 1集合的概念把集合看做是一个容器,集合不是一个类,是一套集合框架,框架体系包含很多的集合类,java api提供了集合存储任意类型(基本包装
- 计数排序是非比较的排序算法,用辅助数组对数组中出现的数字计数,元素转下标,下标转元素计数排序优缺点优点:快缺点:数据范围很大,比较稀疏,会导
- 零、关于HibernateHibernate是冬眠的意思,它是指动物的冬眠,但是本文讨论的Hibernate却与冬眠毫无关系,而是接下来要讨
- 缘起经过前面三章的入门,我们大概了解了Mybatis的主线逻辑是什么样子的,在本章中,我们将正式进入Mybatis的源码海洋。Mybatis
- 方法一(推荐):设置datetimepicker的属性ShowCheckBox为true在窗口初始化时候,添加代码this.datetime
- 使用java语言用集合存储数据实现学生信息管理系统,在控制台上编译执行可以实现基本的学生信息增加、删除、修改、查询功能IO版可以参考我的另外
- 前言Spring的一个核心功能是IOC,就是将Bean初始化加载到容器中,Bean是如何加载到容器的,可以使用Spring注解方式或者Spr