Notification的发送与取消
来源:互联网 发布:网络明星孤烟暮蝉 编辑:程序博客网 时间:2024/06/05 01:10
我们经常能在生活中收到各种通知,短信就是最明显的例子。。
关于android的通知我们通过一个小案例来简单实现以下。。
首先看xml代码,我放置了两个按钮,分别为取消和发送:
<?xml version="1.0" encoding="utf-8"?><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="com.example.xiati.notification1.MainActivity"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="send notifucation" android:id="@+id/send" android:layout_marginTop="30dp" android:layout_alignParentTop="true" android:layout_alignParentStart="true"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="cancel notification" android:id="@+id/cancel" android:layout_alignTop="@+id/send" android:layout_toEndOf="@+id/send"/></RelativeLayout>
然后看activity的处理:
package com.example.xiati.notification1;import android.app.Notification;import android.app.NotificationManager;import android.app.PendingIntent;import android.content.Context;import android.content.Intent;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.Button;public class MainActivity extends AppCompatActivity implements View.OnClickListener{ private Button sendBtn; private Button cancelBtn; private NotificationManager notificationManager; public static final int NOTIFICATION_ID = 1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); sendBtn = (Button)findViewById(R.id.send); cancelBtn = (Button)findViewById(R.id.cancel); // 发送通知点击事件 sendBtn.setOnClickListener(this); // 取消通知点击事件 cancelBtn.setOnClickListener(this); // 注册通知服务 notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE); } @Override public void onClick(View v) { switch (v.getId()){ case R.id.send: sendNotification(); break; case R.id.cancel: notificationManager.cancel(NOTIFICATION_ID); break; } } /** * 发送通知 */ public void sendNotification(){ Intent intent = new Intent(this, MainActivity.class); // 创建一个点击通知的处理 PendingIntent pendingIntent = PendingIntent.getActivities(this, 0, new Intent[]{intent} ,0); Notification.Builder builder = new Notification.Builder(this); // 设置小图标 builder.setSmallIcon(R.drawable.xiatianlong); // 设置通知时状态栏显示的文本 builder.setTicker("通知状态栏的显示文本"); // 设置通知的时间(此处去系统的当前时间) builder.setWhen(System.currentTimeMillis()); // 设置通知的标题 builder.setContentTitle("通知标题"); // 设置通知的内容 builder.setContentText("通知的内容"); // 设置点击跳转 builder.setContentIntent(pendingIntent); // 设置通知音 builder.setDefaults(Notification.DEFAULT_SOUND); Notification notification = builder.build(); notificationManager.notify(NOTIFICATION_ID ,notification); }}
下面来谈谈notification,这个notification一般用在电话,短信,邮件,闹钟铃声,在手机的状态栏上就会出现一个小图标,提示用户处理这个快讯,这时手从上方滑动状态栏就可以展开并处理这个快讯。发现这个功能特别好用,所以我就根据我的理解来谈谈。摘自帮助文档 : notification类表示一个持久的通知,将提交给用户使用NotificationManager。已添加的Notification.Builder,使其更容易构建通知。notification是一种让你的应用程序在没有开启情况下或在后台运行警示用户。它是看不见的程序组件(Broadcast Receiver,Service和不活跃的Activity)警示用户有需要注意的事件发生的最好途径。
先来区分以下状态栏和状态条的区别:
1、状态条就是手机屏幕最上方的一个条形状的区域;
在状态条有好多信息量:比如usb连接图标,手机信号图标,电池电量图标,时间图标等等;
2、状态栏就是手从状态条滑下来的可以伸缩的view;
在状态栏中一般有两类(使用FLAG_标记):
(1)正在进行的程序;
(2)是通知事件;
大概来描述创建一个Notification传送的信息有:
1、一个状态条图标;
2、在拉伸的状态栏窗口中显示带有大标题,小标题,图标的信息,并且有处理该点击事件:比如调用该程序的入口类;
3、闪光,LED,或者震动;
快速创建一个Notification的步骤简单可以分为以下四步:
第一步:通过getSystemService()方法得到NotificationManager对象;
第二步:对Notification的一些属性进行设置比如:内容,图标,标题,相应notification的动作进行处理等等;
第三步:通过NotificationManager对象的notify()方法来执行一个notification的快讯;
第四步:通过NotificationManager对象的cancel()方法来取消一个notificatioin的快讯;
下面对Notification类中的一些常量,字段,方法简单介绍一下:
常量:
DEFAULT_ALL 使用所有默认值,比如声音,震动,闪屏等等
DEFAULT_LIGHTS 使用默认闪光提示
DEFAULT_SOUNDS 使用默认提示声音
DEFAULT_VIBRATE 使用默认手机震动
【说明】:加入手机震动,一定要在manifest.xml中加入权限:
<uses-permission android:name="android.permission.VIBRATE" />
以上的效果常量可以叠加,即通过
mNotifaction.defaults =DEFAULT_SOUND | DEFAULT_VIBRATE ;
或mNotifaction.defaults |=DEFAULT_SOUND (最好在真机上测试,震动效果模拟器上没有)
//设置flag位
FLAG_AUTO_CANCEL 该通知能被状态栏的清除按钮给清除掉
FLAG_NO_CLEAR 该通知能被状态栏的清除按钮给清除掉
FLAG_ONGOING_EVENT 通知放置在正在运行
FLAG_INSISTENT 是否一直进行,比如音乐一直播放,知道用户响应
常用字段:
contentIntent 设置PendingIntent对象,点击时发送该Intent
defaults 添加默认效果
flags 设置flag位,例如FLAG_NO_CLEAR等
icon 设置图标
sound 设置声音
tickerText 显示在状态栏中的文字
when 发送此通知的时间戳
Notification.build构造Notification方法介绍:
void setLatestEventInfo(Context context , CharSequencecontentTitle,CharSequence contentText,PendingIntent contentIntent)
功能: 显示在拉伸状态栏中的Notification属性,点击后将发送PendingIntent对象
参数: context 上下文环境
contentTitle 状态栏中的大标题
contentText 状态栏中的小标题
contentIntent 点击后将发送PendingIntent对象
说明:要是在Notification中加入图标,在状态栏和状态条中显示图标一定要用这个方法,否则报错!
最后说一下NotificationManager类的常用方法:
通过获取系统服务来获取该对象:
NotificationManager mNotificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE) ;
NotificationManager常用方法介绍:
public void cancelAll() 移除所有通知 (只是针对当前Context下的Notification)
public void cancel(int id) 移除标记为id的通知 (只是针对当前Context下的所有Notification)
public void notify(String tag ,int id, Notification notification) 将通知加入状态栏, 标签为tag,标记为id
public void notify(int id, Notification notification) 将通知加入状态栏,,标记为id
首先,我感觉在实现中PendingIntent感觉就是Intent的包装。
它的三个实例化方法:
getActivity(Context, int, Intent, int)
getService(Context, int, Intent, int)
getBroadcast(Context, int, Intent, int)
感觉是保存当前的Activity的Context,然后在外部启动Intent动作。类似于代码Context.startActivity(*, *);
常和Notification和Alarm一起使用。
- Notification的发送与取消
- 使用Notification实现状态通知栏的发送和取消
- 发送,取消notification,打开,关闭闪光灯。
- 笔记9 Notification Notification创建、发送通知及取消通知
- 发送Notification的方法
- iOS开发之旅--本地通知的发送与取消
- Android AlarmManager加入与取消闹钟提醒, Notification不显示的问题
- android Notification的实例与Notification API
- 取消outlook正在发送的邮件
- outlook取消大附件的发送
- 程序发送和取消状态栏的通知
- 如何在正在发送界面取消彩信的发送
- Notification的功能与用法
- delegate 与 notification 的区别
- Notification的功能与用法
- Notification的功能与用法
- Notification的功能与用法
- Notification的功能与用法
- RocketMQ 发送消息疑问
- wamp安装成功,但是localhost还是打不开
- 来点硬件知识,给笔记本加固态硬盘
- leetcode-python 第九周
- Win7+Eclipse+Hadoop2.6.4开发环境搭建
- Notification的发送与取消
- 【华为练习题】 最后一个单词的长度(初级)
- Eclipse快捷键大全(转)
- java一点内容(1)
- typedef用法
- 《Android开发艺术探索》学习笔记之综合知识梳理
- java--html(5)表单
- CUDA(五)用deviceQuery看GPU属性
- 广告轮播图功能------本地获取图片(一)