位置: 编程技术 - 正文

Android中通知的使用-----Notification详解(android通知消息)

编辑:rootadmin

推荐整理分享Android中通知的使用-----Notification详解(android通知消息),希望有所帮助,仅作参考,欢迎阅读内容。

文章相关热门搜索词:android 通知管理,安卓通知类型,安卓通知类型,android系统的通知,android系统设置通知管理,安卓通知app,安卓通知类型,安卓通知机制,内容如对您有帮助,希望把文章链接给更多的朋友!

Notification —— 通知,是一种让你的应用程序在不使用Activity的情况下警示用户。它是看不见的程序组件(Broadcast Receiver,Service和不活跃的Activity)警示用户有需要注意的事件发生的最好途径。 Notification 是由NotificationManager(系统服务)统一管理的。

一般来说, 一个Notification应该传送的消息包括:

1 、一个状态条图标

2、在拉伸的状态栏窗口中显示额外的信息和启动一个Application的Intent

3、闪灯或LED

4、电话震动

在状态栏(Status Bar)中,通知主要有两类(使用FLAG_标记,后面讲解到):

1、正在运行的事件

2、通知事件

Notification图解如下:

Notification类介绍:

常量:

//表示发送一个Notification的所携带的效果

DEFAULT_ALL 使用默认字段

DEFAULT_LIGHTS 默认闪光

DEFAULT_SOUND 默认声音(uri,指向路径)

DEFAULT_VIRATE 默认震动,后来得知需要添加震动权限VIBRATE: android.permission.VIBRATE

PS:以上的效果常量可以累加,即通过mNotifaction.defaults |=DEFAULT_SOUND (有些效果只能在真机上才有,比如震动)

//设置Flag位

FLAG_AUTO_CANCEL 该通知能被状态栏的清除按钮给清除掉

FLAG_NO_CLEAR 该通知不能被状态栏的清除按钮给清除掉

FLAG_ONGOING_EVENT 通知放置在正在运行

常用字段

contentView 通知在状态栏的显示View(自定义,具体请看下文) ,常与contentIntent配合使用,点击该通知后,

即触发contentIntent

contentIntent 设置PendingIntent对象,点击该通知时发送该Intent

flags 设置flag位,例如FLAG_NO_CLEAR等

defaults 添加效果

tickerText 显示在状态栏中的文字

when 发送此通知的时间戳

icon 设置图标

Android中通知的使用-----Notification详解(android通知消息)

常用方法介绍:

void setLatestEventInfo(Context context , CharSequence contentTitle,CharSequence contentText,PendingIntent contentIntent)

功能: 显示在拉伸状态栏中的Notification属性,点击后将发送PendingIntent对象。

参数: context 上下文环境

contentTitle 状态栏中的大标题

contentText 状态栏中的小标题

contentIntent 点击后将发送PendingIntent对象

另外的就是Notification的几步不同构造方法了,其构造方法的参数含义如上,请参考SDK 。

注意,关于通知(Notification)的显示类型有两种:

第一种:使用默认的形式(效果图如上显示)。具体使用是为Notification对象设置setLatestEventInfo()方法(该方法内部创建

了默认的RemoteViews对象,因此为默认显示),否则程序会报异常 ;

第二种: 使用自定义的View(RemoteViews对象)显示(功能更加自由,强大),具体方法为设置Notification对象的

contentView 属性和contentIntent属性 ,此时不需要设置setLatestEventInfo()方法。具体使用方法如下:

[java] view plaincopyNotification noti = new Notification(icon, title, when &#; ); noti.flags = Notification.FLAG_INSISTENT; // 1、创建一个自定义的消息布局 notification.xml // 2、在程序代码中使用RemoteViews的方法来定义image和text。然后把RemoteViews对象传到contentView字段 RemoteViews remoteView = new RemoteViews(this.getPackageName(),R.layout.notification); remoteView.setImageViewResource(R.id.image, R.drawable.icon); remoteView.setTextViewText(R.id.text , "Hello,this message is in a custom expanded view"); noti.contentView = remoteView; // 3、为Notification的contentIntent字段定义一个Intent(注意,使用自定义View不需要setLatestEventInfo()方法) //这儿点击后简答启动Settings模块 PendingIntent contentIntent = PendingIntent.getActivity (MainActivity.this, 0,new Intent("android.settings.SETTINGS"), 0); noti.contentIntent = contentIntent;

本文采用的RemoteViews资源文件如下:/layout/notification.xml

[java] view plaincopy<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android=" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent"> <ImageView android:id="@&#;id/image" android:layout_width="wrap_content" android:layout_height="fill_parent" /> <TextView android:id="@&#;id/text" android:layout_width="wrap_content" android:layout_toRightOf="@&#;id/image" android:layout_height="wrap_content" android:textColor="#" /> <ProgressBar android:id="@&#;id/progress_horizontal" style="?android:attr/progressBarStyleHorizontal" android:layout_below="@&#;id/text" android:layout_toRightOf="@&#;id/image" android:layout_width="fill_parent" android:layout_height="wrap_content" android:max="" android:progress="" android:secondaryProgress="" /> </RelativeLayout>

效果图如下:

前面我们说过,NotificationManager是所有Notification的大管家,它的主要职责是加入/移除Notification。

NotificationManager类

通过获取系统服务来获取该对象:

NotificationManager mNotificationManager = (NotificationManager)getSystemServic(Context.NOTIFICATION_SERVICE) ;

常用方法:

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

Demo如下:

说明: 示例Demo演示了创建两种不同类型的Notification , 实现起来也是很简单的。其实说到两种不同类型的使用

方式 ,其实内部原理是差不多的。

PS : 自定义Notification复用了文章之前的布局文件,请知晓。

[java] view plaincopypackage com.feixun.qin; import android.app.Activity; import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.net.Uri; import android.os.Bundle; import android.provider.MediaStore.Audio; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.RemoteViews; import android.widget.RemoteViews.RemoteView; public class MainActivity extends Activity { private Button sendNotiBt; private int count = 0; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); sendNotiBt = (Button) findViewById(R.id.sendNotiBt); sendNotiBt.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub showDefaultNotification(); } }); } //自定义显示的通知 ,创建RemoteView对象 private void showCustomizeNotification() { CharSequence title = "i am new"; int icon = R.drawable.icon; long when = System.currentTimeMillis(); Notification noti = new Notification(icon, title, when &#; ); noti.flags = Notification.FLAG_INSISTENT; // 1、创建一个自定义的消息布局 view.xml // 2、在程序代码中使用RemoteViews的方法来定义image和text。然后把RemoteViews对象传到contentView字段 RemoteViews remoteView = new RemoteViews(this.getPackageName(),R.layout.notification); remoteView.setImageViewResource(R.id.image, R.drawable.icon); remoteView.setTextViewText(R.id.text , "通知类型为:自定义View"); noti.contentView = remoteView; // 3、为Notification的contentIntent字段定义一个Intent(注意,使用自定义View不需要setLatestEventInfo()方法) //这儿点击后简单启动Settings模块 PendingIntent contentIntent = PendingIntent.getActivity (MainActivity.this, 0,new Intent("android.settings.SETTINGS"), 0); noti.contentIntent = contentIntent; NotificationManager mnotiManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); mnotiManager.notify(0, noti); } // 默认显示的的Notification private void showDefaultNotification() { // 定义Notication的各种属性 CharSequence title = "i am new"; int icon = R.drawable.icon; long when = System.currentTimeMillis(); Notification noti = new Notification(icon, title, when &#; ); noti.flags = Notification.FLAG_INSISTENT; // 创建一个通知 Notification mNotification = new Notification(); // 设置属性&#; mNotification.icon = R.drawable.icon; mNotification.tickerText = "NotificationTest"; mNotification.when = System.currentTimeMillis(); // 立即发生此通知 // 带参数的构造函数,属性&#;如上 // Notification mNotification = = new Notification(R.drawable.icon,"NotificationTest", System.currentTimeMillis())); // 添加声音效果 mNotification.defaults |= Notification.DEFAULT_SOUND; // 添加震动,后来得知需要添加震动权限 : Virbate Permission //mNotification.defaults |= Notification.DEFAULT_VIBRATE ; //添加状态标志 //FLAG_AUTO_CANCEL 该通知能被状态栏的清除按钮给清除掉 //FLAG_NO_CLEAR 该通知能被状态栏的清除按钮给清除掉 //FLAG_ONGOING_EVENT 通知放置在正在运行 //FLAG_INSISTENT 通知的音乐效果一直播放 mNotification.flags = Notification.FLAG_INSISTENT ; //将该通知显示为默认View PendingIntent contentIntent = PendingIntent.getActivity (MainActivity.this, 0,new Intent("android.settings.SETTINGS"), 0); mNotification.setLatestEventInfo(MainActivity.this, "通知类型:默认View", "一般般哟。。。。",contentIntent); // 设置setLatestEventInfo方法,如果不设置会App报错异常 NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); //注册此通知 // 如果该NOTIFICATION_ID的通知已存在,会显示最新通知的相关信息 ,比如tickerText 等 mNotificationManager.notify(2, mNotification); } private void removeNotification() { NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); // 取消的只是当前Context的Notification mNotificationManager.cancel(2); } }

简单粗暴的图片压缩,可以压到kb以内 publicstaticBitmaprevitionImageSize(Stringpath)throwsIOException{BufferedInputStreamin=newBufferedInputStream(newFileInputStream(newFile(path)));BitmapFactory.Optionsoptions=newBitmapFactory.Options()

Android gpio口在sys_comfig中的参数意义 ;Port:端口组内序号功能分配内部电阻状态驱动能力输出电平状态;第一个尖括号:功能分配,0为输入,1为输出,2是可做IO口,6是中断口,其余不确定;第

在Android中实现截图功能 在实际应用中,经常需要对图片进行处理,包括压缩、截图等等,其实android系统提供了一个可以截图的activity,我们只需调用它就行了,下面示例完成

标签: android通知消息

本文链接地址:https://www.jiuchutong.com/biancheng/370942.html 转载请保留说明!

上一篇:Android使用volley发送带参数的post请求(Android使用活动需要去什么进行注册)

下一篇:简单粗暴的图片压缩,可以压到100kb以内(简单 粗暴)

  • 申报个税后发生扣工资
  • 建筑业购买材料计入什么科目
  • 印花税购销合同包括哪些
  • 土地使用税是新增税吗
  • 个独企业缴纳个人经营所得税额的会计分录
  • 主营业务收入和销售收入
  • 国家规定先打五联还是麻腮风
  • 手撕票几个点
  • 其他应付款是不是长期负债
  • 认缴注册资本的风险
  • 异地存货管理
  • 收到退款如何做账
  • 进项转出再差额征收待抵扣
  • 以前多计提了应付职工薪酬怎么平账?
  • 医院哪些收入需要交企业所得税
  • 国税实名认证一般是公司里谁去认证的
  • 增值税申报金额含税吗
  • 政府机构可以开对外营业的餐饮吗
  • 税报完了可以撤销吗
  • 房地产企业销售现房是否预缴增值税
  • 计提工资与发放工资
  • 通讯费补贴要交税吗
  • 行政事业单位绿化工程完工应结转资产吗
  • 税务稽查可以不罚款的情形
  • win7系统没有桌面图标怎么办
  • 金融负债包括哪几项
  • 劳务费和合同如何分配
  • 购买商城购买化肥
  • 开机后网络连接很慢
  • 会计分录有哪几种形式
  • 其他应付款结转什么科目
  • 如何做商品批发
  • batchsize1
  • 纽格尔官网
  • 企业所得税扣除限额标准
  • 前后端分离与不分离
  • 出售其他债权投资差额计入
  • 车辆购置税发票在哪里打印
  • 职工教育经费的计提比例是多少
  • 申请退税怎么算
  • 一觉醒后浑身酸痛
  • 什么是CHATGPT
  • 预付采购材料款60000元
  • 职工福利费属于短期薪酬吗
  • wordpress怎么用
  • 什么叫坏账准备
  • 幼儿园免征增值税账务处理
  • 小微企业所得税税率2.5% 10% 25%
  • 接受银行承兑汇票需要开通吗
  • 个体工商户还没开业也要报税吗
  • 企业促销费用项目有哪些
  • 核定征收企业有没有自然人电子税务
  • 小规模防伪税控服务费多少钱
  • 现金折扣与商业折扣的计算
  • 结转是什么意思啊
  • 为什么说资产负债表是静态报表
  • 退回上年度企业所得税
  • 开票金额大于付款金额 多余部分为返点
  • 存出投资款会计处理
  • 无形资产减值准备是什么科目
  • wlms.exe是什么
  • linux补丁工具
  • win8怎么修改电脑密码修改
  • 微软输入法拼音
  • win8.1系统怎么升级到win10
  • glsl fract
  • 解决胃胀气最快方法
  • sed 处理多行
  • windows2008 iis
  • css框架结构
  • vue.js作用
  • python调用cuda执行加法
  • unity rp
  • js教程 chm
  • python周期性分析
  • vue卡片式风格
  • 善意取得的条件和效力
  • 汽车公司户转个人户
  • 浙江省焊工操作证查询
  • 豫事办登录时密码叫重置什么原因
  • 免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

    鄂ICP备2023003026号

    网站地图: 企业信息 工商信息 财税知识 网络常识 编程技术

    友情链接: 武汉网站建设