Notification_使用详解(很全)1
- 格式:doc
- 大小:62.00 KB
- 文档页数:9
android studio notice用法
在Android Studio中,通知(Notification)是一种常用的提醒用户的方式,可以在App运行过程中主动向用户推送消息。
Notification类描述了消息通知的组成内容,包括消息图标、消息标题、消息内容等基本元素,以及附加文本、进度条、计时器等额外元素。
实际推送工作还需要由通知管理器NotificationManager执行。
以下是一个简单的使用Notification的示例:
1. 创建一个Notification对象,并设置其各种属性,如标题、内容、图标等。
2. 创建一个PendingIntent对象,该对象指定了当用户点击通知时应该打开的Activity或执行的操作。
3. 使用NotificationManager来发送Notification。
需要注意的是,由于Android系统版本的更新,Notification的使用方式也在不断变化。
因此,在编写代码时需要考虑到不同版本的兼容性。
同时,对于Android 及以上版本,由于引入了新的通知渠道(Notification Channels)的概念,因此需要在创建通知前先创建一个通知渠道。
通知渠
道用于定义通知的属性和行为,例如声音、振动和优先级等。
此外,为了确保通知能够正常显示,还需要在文件中添加必要的权限和声明。
例如,如果要在后台播放音乐或发送网络请求时显示通知,需要在Manifest中添加相应的权限。
总之,在Android Studio中正确使用Notification需要考虑到多个方面,包括API的调用、权限的声明以及不同版本的兼容性等。
AndroidStudioNotification(状态栏通知)详解1.设计⽂档部分解读1)Notification的基本布局上⾯的组成元素依次是:Icon/Photo:⼤图标Title/Name:标题Message:内容信息Timestamp:通知时间,默认是系统发出通知的时间,也可以通过setWhen()来设置Secondary Icon:⼩图标内容⽂字,在⼩图标的左⼿边的⼀个⽂字2)扩展布局在 Jelly Bean 中你可以为通知提供更多事件的细节。
你可以通过扩展布局显⽰消息的前⼏⾏或者图⽚的预览。
这样⽤户可以看多更多的内容 - 有时甚⾄可以看到整个消息。
⽤户可以通过 pinch-zoom 或者双⼿指滑动来打开扩展布局。
Android 为单条消息提供了两种扩展布局 (⽂字和图像) 供你开发应⽤时使⽤。
关于其他⼀些设计的东西,就不⼀⼀提及了,有兴趣的⾃⾏查看上⾯提供的API⽂档,知道下这个Notification在4.x以上的版本可以多种多样就好!我们更多的时候关注的是如何写代码使⽤这个东西,下⾯我们就来学习下Notification的⽤法!2.Notification的基本使⽤流程状态通知栏主要涉及到2个类:Notification 和NotificationManagerNotification:通知信息类,它⾥⾯对应了通知栏的各个属性NotificationManager:是状态栏通知的管理类,负责发通知、清除通知等操作。
使⽤的基本流程:Step 1. 获得NotificationManager对象: NotificationManager mNManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);Step 2. 创建⼀个通知栏的Builder构造类: Notification.Builder mBuilder = new Notification.Builder(this);Step 3. 对Builder进⾏相关的设置,⽐如标题,内容,图标,动作等!Step 4.调⽤Builder的build()⽅法为notification赋值Step 5.调⽤NotificationManager的notify()⽅法发送通知!PS:另外我们还可以调⽤NotificationManager的cancel()⽅法取消通知3.设置相关的⼀些⽅法:Notification.Builder mBuilder = new Notification.Builder(this);后再调⽤下述的相关的⽅法进⾏设置:(官⽅API⽂档:) 常⽤的⽅法如下:setContentTitle(CharSequence):设置标题setContentText(CharSequence):设置内容setSubText(CharSequence):设置内容下⾯⼀⼩⾏的⽂字setTicker(CharSequence):设置收到通知时在顶部显⽰的⽂字信息setWhen(long):设置通知时间,⼀般设置的是收到通知时的System.currentTimeMillis()setSmallIcon(int):设置右下⾓的⼩图标,在接收到通知的时候顶部也会显⽰这个⼩图标setLargeIcon(Bitmap):设置左边的⼤图标setAutoCancel(boolean):⽤户点击Notification点击⾯板后是否让通知取消(默认不取消)setDefaults(int):向通知添加声⾳、闪灯和振动效果的最简单、使⽤默认(defaults)属性,可以组合多个属性,Notification.DEFAULT_VIBRATE(添加默认震动提醒);Notification.DEFAULT_SOUND(添加默认声⾳提醒);Notification.DEFAULT_LIGHTS(添加默认三⾊灯提醒)Notification.DEFAULT_ALL(添加默认以上3种全部提醒)setVibrate(long[]):设置振动⽅式,⽐如:setVibrate(new long[] {0,300,500,700});延迟0ms,然后振动300ms,在延迟500ms,接着再振动700ms,关于Vibrate⽤法后⾯会讲解!setLights(int argb, int onMs, int offMs):设置三⾊灯,参数依次是:灯光颜⾊,亮持续时间,暗的时间,不是所有颜⾊都可以,这跟设备有关,有些⼿机还不带三⾊灯;另外,还需要为Notification设置flags为Notification.FLAG_SHOW_LIGHTS 才⽀持三⾊灯提醒!setSound(Uri):设置接收到通知时的铃声,可以⽤系统的,也可以⾃⼰设置,例⼦如下:.setDefaults(Notification.DEFAULT_SOUND) //获取默认铃声.setSound(Uri.parse("file:///sdcard/xx/xx.mp3")) //获取⾃定义铃声.setSound(Uri.withAppendedPath(Audio.Media.INTERNAL_CONTENT_URI, "5")) //获取Android多媒体库内的铃声setOngoing(boolean):设置为ture,表⽰它为⼀个正在进⾏的通知。
notification用法Notification用法Notification是Android系统中的一种提醒方式,它可以在屏幕上显示一条消息,提示用户有新的事件发生。
在Android应用程序中,Notification通常用于提醒用户有新的消息、通知、任务等等。
本文将详细介绍Notification的使用方法。
一、创建Notification实例要创建一个Notification实例,需要使用Notification.Builder类。
以下是创建一个基本的Notification实例的示例代码:```// 创建一个基本的Notification实例NotificationCompat.Builder builder = new NotificationCompat.Builder(this).setSmallIcon(R.drawable.notification_icon).setContentTitle("My notification").setContentText("Hello World!");```其中,setSmallIcon()方法用于设置通知栏中小图标的资源ID;setContentTitle()方法用于设置通知栏中标题文本;setContentText()方法用于设置通知栏中内容文本。
二、设置PendingIntent当用户点击通知时,我们需要响应这个事件。
为此,我们需要为通知设置一个PendingIntent。
PendingIntent是一个延迟执行的Intent 对象,它允许我们在未来某个时间点执行某个操作。
以下是为通知设置PendingIntent的示例代码:```// 创建一个打开Activity的PendingIntentIntent intent = new Intent(this, MainActivity.class); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);// 将PendingIntent设置到Builder中builder.setContentIntent(pendingIntent);```其中,getActivity()方法用于创建启动Activity的Intent对象;getBroadcast()方法用于创建发送广播的Intent对象;getService()方法用于创建启动Service的Intent对象。
Notification 使用详解(很全Java & android 2011-01-25 14:23:21当用户有没有接到的电话的时候,Android顶部状态栏里就会出现一个小图标。
提示用户有没有处理的快讯,当拖动状态栏时,可以查看这些快讯。
Android给我们提供了NotificationManager来管理这个状态栏。
可以很轻松的完成。
如果要添加一个Notification,可以按照以下几个步骤1:获取NotificationManager:NotificationManagerm_NotificationManager=(NotificationManagerthis.getSystemService(NOTIFICATI ON_SER VICE;2:定义一个Notification:Notification m_Notification=new Notification(;3:设置Notification的各种属性://设置通知在状态栏显示的图标m_Notification.icon=R.drawable.icon;//当我们点击通知时显示的内容m_Notification.tickerText="Button1 通知内容.....";通知时发出的默认声音m_Notification.defaults=Notification.DEFAULT_SOUND;//设置通知显示的参数Intent m_Intent=new Intent(NotificationDemo.this,DesActivity.class; PendingIntent m_PendingIntent=PendingIntent.getActivity(NotificationDemo.this, 0,m_Intent, 0;m_Notification.setLatestEventInfo(NotificationDemo.this, "Button1", "Button1通知",m_PendingIntent ;//这个可以理解为开始执行这个通知m_NotificationManager.notify(0,m_Notification;4:既然可以增加同样我们也可以删除。
Notice 和 Notification 的区别与运用一、notice 和 notification 的定义1. notice:notice 是一个英语单词,可以作名词或动词使用。
作名词时,表示“注意,注意到,通知,布告”等意思;作动词时,表示“注意到,察觉,通知”等意思。
2. notification:notification 也是一个英语单词,通常作名词使用,表示“通知,通告,通报”等意思。
二、notice 和 notification 的用法比较1. notice 一般用作名词时,表示的是“通知、布告”等形式的消息,比如公告、告示、通告等。
2. notification 通常用作名词,表示“通知”或“通告”,重点强调的是一种正式的冠方形式的通知。
三、notice 和 notification 的具体场景应用1. notice 的应用场景(1) 在学校、办公室、公共场所等地方,通常会有各种通知或者告示,这些通知和告示就是 notice。
比如学校的校园通告、公司的公告、商场的通知等,都属于 notice 的范畴。
(2) 在日常生活中,如果有人想要传达某种重要的信息,也可以使用notice 这个词汇。
你可以对他人说:“I want to give you a notice about our meeting tomorrow.”2. notification 的应用场景(1) 在一些正式的场合,比如政府部门、公司组织等,通常会使用notification 这个词汇来表示正式的冠方通知。
政府发布的公告、公司发出的通知、法院传达的法律通知等,都属于 notification 的范畴。
(2) 在技术领域,比如软件、手机应用等,notification 通常用来表示“通知”功能,用于提醒用户收到了某些消息或者事件。
比如手机上的消息通知、软件中的系统通知等,都是 notification 的应用。
四、notice 和 notification 的语法搭配1. 在句子中,notice 和 notification 都可以作为名词使用,可以和动词 give, receive, send, issue 等搭配使用。
android用户界面之Notification教程实例汇总用户界面是android开发中最基础的功能,也是与用户交互最直接的方式。
通过学习安卓开发这段时间,很容易找到关于android的各种UI常见元素的教程与实例。
比如说:WebView,Edittext,Button ,ListView,TextView, checkbox,layout, surfaceview,menu,progressBar,seekBar,Notification 等等。
现在特地总结了Android用户界面之Notification 的相关信息,分三个方面进行说明的,希望可以对学习 安卓开发 的朋友们有些帮助,也希望能不断补充新的学习内容。
一、Notification基础教程1.Notification实时显示系统内存信息/android-51690-1-1.html2.Android 通知Notification/android-51694-1-1.html3.Notification 使用详解(很全)/android-51688-1-1.html4.Android Push Notification实现信息推送使用/android-48363-1-1.html5.android 消息提醒(Toast,Notification)/android-12970-1-1.html6.第十九讲:Android Notification的使用入门/android-725-1-1.html7.Android Notification与Toast/android-6187-1-1.html8.Andriod:Toast、Notification、Alarm, Android中的通知实现 /android-4776-1-1.html9.NotificationManager and Notification学习笔记/android-267-1-1.html10Android 浅谈Notification/android-16289-1-1.html11.android开发(11) 消息栏通知(Notification)/android-5202-1-1.html12.解读Notification/android-19156-1-1.html二、Notification简单实例1.如何制作Notification程序/android-51689-1-1.html 2.实现自定义布局的Notification/android-19155-1-1.html 3.android之Notification的使用/android-17099-1-1.html 4.Android 给地震监视器添加Notification/android-16948-1-1.html 5.Android 高级Notification技巧/android-16949-1-1.html 6.在Android中使用Notification进行提示/android-20486-1-1.html三、Notification实例源码1.使用免费服务轻松实现push notification/android-51693-1-1.html 2.如何修改状态栏notification/android-5967-1-1.html。
1.创建一个Notification的对象Notification(icon, tickerText, when)2.setLatestEventInfo(context,ContentTitle,contentText,contentIntent)第一个参数上下文环境第二个参数标题第三个参数内容第四个参数要跳转到的Activity其中的Intent 是PendingIntent类型的3.创建PendingIntent的对象通过PendingIntent contentIntent=PendingIntent.getActivity (context, requestCode, intent, flags);第一个参数上下文环境第二个参数请求号第三个参数intent对象要跳转到的Activity 第四个参数标志位4.激活Notification要通过NotificationManager对象调用notify(id, notification)方法,因此Notification的激活需要使用NotificationManager对象创建NotificationManager对象getSystemService(Context.NOTIFICATION_SERVICE); default属性为Notification设置震动、声音、灯光,取值是Notification中的常量以DEFAULT 开头,如果只想设置其中的两个可以用位或| 符号进行分割;flag属性设置Notification能否被清除,取值是Notification中的常量以FLAG 开头设置震动要加权限uses-permission android:name="android.permission.VIBRATE"一个应用对用着一个ActivityManager的对象,同时对应着一个NotificationManager的对象,管理时只通过ID区分,其他的不能标示NotificationManager可以让我们的Notification消失,cancle(ID),cancleall()。
Android中Notification通知⽤法详解Notification的作⽤通知(Notification)是Android系统中⽐较有特⾊的⼀个功能。
当某个应⽤程序希望向⽤户发出⼀些提⽰信息,⽽该应⽤程序⼜不在前台运⾏时,就可以⽤通知来实现⽤法⾸先我们需要⼀个NotificationManager来对通知进⾏管理,可以调⽤getSystemService()⽅法得到,⽅法接收⼀个字符串参数⽤于确定获取系统的哪个服务,这⾥我们传⼊NOTIFICATION_SERVICE。
NotificationManager manager=(NotificationManager) getSystemService(NOTIFICATION_SERVICE);接下来⽤⼀个Builder构造器来创建Notification对象Intent intent=new Intent(this,NotificationActivity.class);//⽤intent表现出我们要启动Notification的意图PendingIntent pi=PendingIntent.getActivity(this,0,intent,0);//将Intent对象传⼊PendingIntent对象的getActivity⽅法中NotificationManager manager=(NotificationManager) getSystemService(NOTIFICATION_SERVICE);Notification notification=new NotificationCompat.Builder(this).setContentTitle("This is content title")//设置通知栏中的标题.setContentText("hello world!")//设置通知栏中的内容.setWhen(System.currentTimeMillis())//设置通知出现的时间,此时为事件响应后⽴马出现通知.setSmallIcon(R.mipmap.ic_launcher)//设置通知出现在⼿机顶部的⼩图标.setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher))//设置通知栏中的⼤图标.setContentIntent(pi)//将PendingIntent对象传⼊该⽅法中,表明点击通知后进⼊到NotificationActivity.class页⾯.setAutoCancel(true)//点击通知后,通知⾃动消失.setDefaults(NotificationCompat.DEFAULT_ALL)//默认选项,根据⼿机当前的环境来决定通知发出时播放的铃声,震动,以及闪光灯.setPriority(NotificationCompat.PRIORITY_MAX)//设置通知的权重.build();manager.notify(1,notification);//⽤于显⽰通知,第⼀个参数为id,每个通知的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-permissionandroid: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对象,点击时发送该Intentdefaults 添加默认效果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 voidcancelAll() 移除所有通知 (只是针对当前Context下的Notification)public void cancel(intid) 移除标记为id的通知(只是针对当前Context下的所有Notification)public void notify(String tag ,int id, Notification notification) 将通知加入状态栏, 标签为tag,标记为idpublic void notify(int id, Notificationnotification) 将通知加入状态栏,,标记为id下面看一下demo的效果图:图(1)图(2)图(3)图(4)图(5)源码奉上:在NotificationApp工程里面:一、在.notification.daming包下面NotificationMainActivity.java中的代码:package .notification.daming;import android.app.Activity;import android.app.Notification;import android.app.NotificationManager;import android.app.PendingIntent;import android.content.Intent;import android.content.SharedPreferences;import android.media.RingtoneManager;import .Uri;import android.os.Bundle;import android.preference.PreferenceManager;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;public class NotificationMainActivity extends Activity implements OnClickListener {private Button setNotificationSoundBtn = null;private Button showNotificatioBtn = null;private Button cancelNotificationBtn = null;private Intent mIntent = null;private PendingIntent mPendingIntent = null;private Notification mNotification = null;private NotificationManager mNotificationManager = null;private static final int NOTIFICATION_STATE = 1;private static final int RINGTONE_PICKED = 2;private Uri notifiSounds = null;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(yout.main);mNotificationManager =(NotificationManager)getSystemService(NOTIFICATION_SERVICE);setNotificationSoundBtn = (Button)findViewById(R.id.button0);setNotificationSoundBtn.setOnClickListener(this);showNotificatioBtn = (Button)findViewById(R.id.button1);showNotificatioBtn.setOnClickListener(this);cancelNotificationBtn = (Button)findViewById(R.id.button2);cancelNotificationBtn.setOnClickListener(this);mIntent = new Intent(this, ToNotificationActivity.class);mPendingIntent = PendingIntent.getActivity(this, 0, mIntent, 0);mNotification = new Notification();}public void onClick(View v) {// TODO Auto-generated method stubswitch(v.getId()){case R.id.button0:SharedPreferences sharedPreferences =PreferenceManager.getDefaultSharedPreferences(this);Intent intent = newIntent(RingtoneManager.ACTION_RINGTONE_PICKER);// Allow user to pick 'Default'intent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_DEFAULT, true);intent.putExtra(RingtoneManager.EXTRA_RINGTONE_DEFAULT_URI, RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));// Show only ringtonesintent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE, RingtoneManager.TYPE_NOTIFICATION);// Don't show 'Silent'intent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_SILENT, false);String notifi_sound =sharedPreferences.getString("notification_sounds", null);if(notifi_sound != null){intent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_UR I, Uri.parse(notifi_sound));}// Launch!startActivityForResult(intent, RINGTONE_PICKED);break;case R.id.button1:mNotification.icon = R.drawable.daming;mNotification.tickerText = "大明ZeroSon Notification";mNotification.sound = notifiSounds;mNotification.defaults = Notification.DEFAULT_VIBRATE | Notification.DEFAULT_LIGHTS;mNotification.flags = Notification.FLAG_INSISTENT ;mNotification.setLatestEventInfo(this, "大明Notification", "This is Daming`s Notification Test!", mPendingIntent);mNotificationManager.notify(NOTIFICATION_STATE, mNotification);break;case R.id.button2:mNotificationManager.cancel(NOTIFICATION_STATE);break;default:break;}}@Overrideprotected void onResume() {super.onResume();}@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) {SharedPreferences sharedPreferences =PreferenceManager.getDefaultSharedPreferences(this);SharedPreferences.Editor editor = sharedPreferences.edit();if (resultCode != RESULT_OK) {return;}switch (requestCode) {case RINGTONE_PICKED: {notifiSounds =data.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);editor.putString("notification_sounds",notifiSounds.toString());mit();break;}default: break;}}}package .notification.daming;import android.app.Activity;import android.app.Notification;import android.app.NotificationManager;import android.app.PendingIntent;import android.content.Intent;import android.content.SharedPreferences;import android.media.RingtoneManager;import .Uri;import android.os.Bundle;import android.preference.PreferenceManager;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;public class NotificationMainActivity extends Activity implements OnClickListener {private Button setNotificationSoundBtn = null;private Button showNotificatioBtn = null;private Button cancelNotificationBtn = null;private Intent mIntent = null;private PendingIntent mPendingIntent = null;private Notification mNotification = null;private NotificationManager mNotificationManager = null;private static final int NOTIFICATION_STATE = 1;private static final int RINGTONE_PICKED = 2;private Uri notifiSounds = null;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(yout.main);mNotificationManager =(NotificationManager)getSystemService(NOTIFICATION_SERVICE);setNotificationSoundBtn = (Button)findViewById(R.id.button0);setNotificationSoundBtn.setOnClickListener(this);showNotificatioBtn = (Button)findViewById(R.id.button1);showNotificatioBtn.setOnClickListener(this);cancelNotificationBtn = (Button)findViewById(R.id.button2);cancelNotificationBtn.setOnClickListener(this);mIntent = new Intent(this, ToNotificationActivity.class);mPendingIntent = PendingIntent.getActivity(this, 0, mIntent, 0);mNotification = new Notification();}public void onClick(View v) {// TODO Auto-generated method stubswitch(v.getId()){case R.id.button0:SharedPreferences sharedPreferences =PreferenceManager.getDefaultSharedPreferences(this);Intent intent = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER);// Allow user to pick 'Default'intent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_DEFAULT, true); intent.putExtra(RingtoneManager.EXTRA_RINGTONE_DEFAULT_URI, RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));// Show only ringtonesintent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE, RingtoneManager.TYPE_NOTIFICATION);// Don't show 'Silent'intent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_SILENT, false); String notifi_sound =sharedPreferences.getString("notification_sounds", null);if(notifi_sound != null){intent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI,Uri.parse(notifi_sound));}// Launch!startActivityForResult(intent, RINGTONE_PICKED);break;case R.id.button1:mNotification.icon = R.drawable.daming;mNotification.tickerText = "大明ZeroSon Notification";mNotification.sound = notifiSounds;mNotification.defaults = Notification.DEFAULT_VIBRATE |Notification.DEFAULT_LIGHTS;mNotification.flags = Notification.FLAG_INSISTENT ;mNotification.setLatestEventInfo(this, "大明Notification", "This is Daming`s Notification Test!", mPendingIntent);mNotificationManager.notify(NOTIFICATION_STATE, mNotification);break;case R.id.button2:mNotificationManager.cancel(NOTIFICATION_STATE);break;default:break;}}@Overrideprotected void onResume() {super.onResume();}protected void onActivityResult(int requestCode, int resultCode, Intent data) { SharedPreferences sharedPreferences =PreferenceManager.getDefaultSharedPreferences(this);SharedPreferences.Editor editor = sharedPreferences.edit();if (resultCode != RESULT_OK) {return;}switch (requestCode) {case RINGTONE_PICKED: {notifiSounds =data.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);editor.putString("notification_sounds", notifiSounds.toString());mit();break;}default: break;}}}二、在.notification.daming包下面ToNotificationActivity.java中的代码:package .notification.daming;import android.app.Activity;import android.content.SharedPreferences;import android.media.Ringtone;import android.media.RingtoneManager;import .Uri;import android.os.Bundle;import android.preference.PreferenceManager;import android.widget.TextView;public class ToNotificationActivity extends Activity{private TextView textview = null;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(yout.main1);SharedPreferences sharedPreferences =PreferenceManager.getDefaultSharedPreferences(this);textview = (TextView)findViewById(R.id.textview);String notificationsound =sharedPreferences.getString("notification_sounds", null);if(notificationsound == null){textview.setText("默认Notification声音");} else{Ringtone ringtone= RingtoneManager.getRingtone(ToNotificationActivity.this,Uri.parse(notificationsound));String title = ringtone.getTitle(ToNotificationActivity.this);textview.setText(title);}}}package .notification.daming;import android.app.Activity;import android.content.SharedPreferences;import android.media.Ringtone;import android.media.RingtoneManager;import .Uri;import android.os.Bundle;import android.preference.PreferenceManager;import android.widget.TextView;public class ToNotificationActivity extends Activity{private TextView textview = null;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(yout.main1);SharedPreferences sharedPreferences =PreferenceManager.getDefaultSharedPreferences(this);textview = (TextView)findViewById(R.id.textview);String notificationsound = sharedPreferences.getString("notification_sounds", null);if(notificationsound == null){textview.setText("默认Notification声音");} else{Ringtone ringtone = RingtoneManager.getRingtone(ToNotificationActivity.this, Uri.parse(notificationsound));String title = ringtone.getTitle(ToNotificationActivity.this);textview.setText(title);}}}三、在layout下main.xml布局文件的代码<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="/apk/res/android"android:orientation="vertical"android:layout_width="fill_parent"android:layout_height="fill_parent"><TextViewandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:text="@string/hello"android:gravity="center"android:layout_marginBottom="10dip"/><Buttonandroid:id="@+id/button0"android:layout_width="fill_parent"android:layout_height="wrap_content"android:text="设置Notification的sounds"android:layout_marginBottom="10dip"/><Buttonandroid:id="@+id/button1"android:layout_width="fill_parent"android:layout_height="wrap_content"android:text="发送Notification"android:layout_marginBottom="10dip"/><Buttonandroid:id="@+id/button2"android:layout_width="fill_parent"android:layout_height="wrap_content"android:text="取消Notification"android:layout_marginBottom="10dip"/></LinearLayout><?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="/apk/res/android" android:orientation="vertical"android:layout_width="fill_parent"android:layout_height="fill_parent"><TextViewandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:text="@string/hello"android:gravity="center"android:layout_marginBottom="10dip"/><Buttonandroid:id="@+id/button0"android:layout_width="fill_parent"android:layout_height="wrap_content"android:text="设置Notification的sounds"android:layout_marginBottom="10dip"/><Buttonandroid:id="@+id/button1"android:layout_width="fill_parent"android:layout_height="wrap_content"android:text="发送Notification"android:layout_marginBottom="10dip"/><Buttonandroid:id="@+id/button2"android:layout_width="fill_parent"android:layout_height="wrap_content"android:text="取消Notification"android:layout_marginBottom="10dip"/></LinearLayout>四、在layout下main1.xml布局文件的代码<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="/apk/res/android" android:orientation="vertical"android:layout_width="fill_parent"android:layout_height="fill_parent"><TextViewandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:gravity="center"android:textSize="10pt"android:text="大明原创"android:layout_marginTop="10dip"android:layout_marginBottom="10dip"/><TextViewandroid:id="@+id/textview"android:layout_width="fill_parent"android:layout_height="wrap_content"android:gravity="center"android:textSize="10pt"android:layout_marginTop="10dip"android:layout_marginBottom="10dip"/></LinearLayout><?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="/apk/res/android" android:orientation="vertical"android:layout_width="fill_parent"android:layout_height="fill_parent"><TextViewandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:gravity="center"android:textSize="10pt"android:text="大明原创"android:layout_marginTop="10dip"android:layout_marginBottom="10dip"/><TextViewandroid:id="@+id/textview"android:layout_width="fill_parent"android:layout_height="wrap_content"android:gravity="center"android:textSize="10pt"android:layout_marginTop="10dip"android:layout_marginBottom="10dip"/></LinearLayout>五、manifest.xml中的代码:<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="/apk/res/android"package=".notification.daming"android:versionCode="1"android:versionName="1.0"><uses-sdk android:minSdkVersion="8" /><uses-permission android:name="android.permission.VIBRATE" /><application android:icon="@drawable/icon"android:label="@string/app_name"><activity android:name=".NotificationMainActivity"android:label="@string/app_name"><intent-filter><action android:name="android.intent.action.MAIN" /><category android:name="UNCHER" /> </intent-filter></activity><activity android:name=".ToNotificationActivity"></activity></application></manifest>。
Notification 使用详解当用户有没有接到的电话的时候,Android顶部状态栏里就会出现一个小图标。
提示用户有没有处理的快讯,当拖动状态栏时,可以查看这些快讯。
Android给我们提供了NotificationManage r来管理这个状态栏。
可以很轻松的完成。
如果要添加一个Notification,可以按照以下几个步骤1:获取NotificationManager:NotificationManagerm_NotificationManager=(NotificationManager)this.getSystemService(NOTIFI CATION_SERVICE);2:定义一个Notification:Notification m_Notification=new Notification();3:设置Notification的各种属性://设置通知在状态栏显示的图标m_Notification.icon=R.drawable.icon;//当我们点击通知时显示的内容m_Notification.tickerText="Button1 通知内容.....";//通知时发出的默认声音m_Notification.defaults=Notification.DEFAULT_SOUND;//设置通知显示的参数Intent m_Intent=new Intent(NotificationDemo.this,DesActivity.class); PendingIntentm_PendingIntent=PendingIntent.getActivity(NotificationDemo.this, 0,m_Intent, 0);m_Notification.setLatestEventInfo(NotificationDemo.this, "Button1", "Button1通知",m_PendingIntent );//这个可以理解为开始执行这个通知m_NotificationManager.notify(0,m_Notification);4:既然可以增加同样我们也可以删除。
当然是只是删除你自己增加的。
m_NotificationManager.cancel(0);这里的0是一个ID号码,和notify第一个参数0一样。
这也就完成了,添加删除工作。
这里我们还是一个Demo来掩饰我们的操作。
1:新建一个工程NotificationDemo。
2:添加一个Activity:DesActivity,注意需要在Manifest.xml中声明3:NotificationDemo中的Laytout文件很简单就是定义一个Button.其代码文件如下:1.package com.rocky.studio.ch4221;2.import android.app.Activity;3.import android.app.Notification;4.import android.app.NotificationManager;5.import android.app.PendingIntent;6.import android.content.Intent;7.import android.os.Bundle;8.import android.view.View;9.import android.widget.Button;10.import android.widget.TextView;11.public class NotificationDemo extends Activity {12.13.Button m_Button1;14.TextView m_txtView;15.16.NotificationManager m_NotificationManager;17.Notification m_Notification;18.19.Intent m_Intent;20.PendingIntent m_PendingIntent;21.22./** Called when the activity is first created. */23.@Override24.public void onCreate(Bundle savedInstanceState) {25. super.onCreate(savedInstanceState);26. setContentView(yout.main);27.28. m_NotificationManager=(NotificationManager)this.getSystemService(NOTIFICATION_SERVICE);29.30.31. m_Button1=(Button)this.findViewById(R.id.Button01);32.33.34. //点击通知时转移内容35. m_Intent=new Intent(NotificationDemo.this,DesActivity.class);36.37. m_PendingIntent=PendingIntent.getActivity(NotificationDemo.this, 0,m_Intent, 0);38.39. m_Notification=new Notification();40.41. m_Button1.setOnClickListener(new Button.OnClickListener(){42. public void onClick(View v) {43.// TODO Auto-generated method stub44.45.//设置通知在状态栏显示的图标46.m_Notification.icon=R.drawable.icon;47.48.//当我们点击通知时显示的内容49.m_Notification.tickerText="Button1 通知内容.....";50.51.//通知时发出的默认声音52.m_Notification.defaults=Notification.DEFAULT_SOUND;53.54.//设置通知显示的参数55.m_Notification.setLatestEventInfo(NotificationDemo.this, "Button1","Button1通知",m_PendingIntent );56.57.//这个可以理解为开始执行这个通知58.m_NotificationManager.notify(0,m_Notification);59.60. }});61.62.}63.64.65.}复制代码4:修改DesActivity 的源文件,代码如下:它做的事情就是取消之前添加的Notification1.package com.rocky.studio.ch4221;2.import android.app.Activity;3.import android.app.NotificationManager;4.import android.os.Bundle;5.public class DesActivity extends Activity {6.7.NotificationManager m_NotificationManager;8.9.@Override10.protected void onCreate(Bundle savedInstanceState) {11. // TODO Auto-generated method stub12. super.onCreate(savedInstanceState);13. this.setContentView(yout.main2);14.15. //启动后删除之前我们定义的16. m_NotificationManager=(NotificationManager)this.getSystemService(NOTIFICATION_SERVICE);17. m_NotificationManager.cancel(0);18.}19.20.}复制代码代码也很简单。
可以查看Notification ,NotificationMananger 这两个类来学习前后左右。
下面是一篇文章,对Notification ,NotificationManager这两个类有详细的说明介绍,特借鉴一下。
NoticificationManager很容易可以放在状态栏,也很容易实现从statusbar进入程序中,NoticificationManager中通过intent执行此程序的activity就可以了NoticificationManager状态栏操作NotificationManager(通知管理器):NotificationManager负责通知用户事件的发生.NotificationManager有三个公共方法:1. cancel(int id) 取消以前显示的一个通知.假如是一个短暂的通知,试图将隐藏,假如是一个持久的通知,将从状态条中移走.2. cancelAll() 取消以前显示的所有通知.3. notify(int id, Notification notification) 把通知持久的发送到状态条上.//初始化NotificationManager:NotificationManager nm =(NotificationManager)getSystemService(NOTIFICATION_SERVICE);Notification代表着一个通知.Notification的属性:audioStreamType 当声音响起时,所用的音频流的类型contentIntent 当通知条目被点击,就执行这个被设置的Intent.contentView 当通知被显示在状态条上的时候,同时这个被设置的视图被显示.defaults 指定哪个值要被设置成默认的.deleteIntent 当用户点击"Clear All Notifications"按钮区删除所有的通知的时候,这个被设置的Intent被执行.icon 状态条所用的图片.iconLevel 假如状态条的图片有几个级别,就设置这里.ledARGB LED灯的颜色.ledOffMS LED关闭时的闪光时间(以毫秒计算)ledOnMS LED开始时的闪光时间(以毫秒计算)number 这个通知代表事件的号码sound 通知的声音tickerText 通知被显示在状态条时,所显示的信息vibrate 振动模式.when 通知的时间戳.将Notification发送到状态条上:Notification notification = new Notification();Notification的设置过程……..nm.notify(0, notification); //发送到状态条上创建和触发Notification创建和配置新的Notification需要经历三步。