Android开发学习笔记:Notification和NotificationManager浅析

简介:

NotificationManager

获取NotificationManager

 String service = Context.NOTIFICATION_SERVICE;

NotificationManager mNotificationManager =(NotificationManager)getSystemService(service);

Notification

 
  1. //实例化Notification  
  2. Notification notification = new Notification(); 

3.设置Notification的属性

 
  1. // 设置显示图标,该图标会在状态栏显示    
  2. int icon = notification.icon = R.drawable.happy;    
  3. // 设置显示提示信息,该信息也在状态栏显示   
  4. String tickerText = "测试Notification";     
  5. // 显示时间    
  6. long when = System.currentTimeMillis();  notification.icon = icon;    
  7. notification.tickerText = tickerText;    
  8. notification.when = when;    
  9.  
  10. //也可以这样设置    
  11. Notification notification_2=new Notification(icon,tickerText,when)  

调用setLatestEventInfo()方法在视图中设置图标和时间。

 
  1. // 实例化Intent  
  2. Intent intent = new Intent(MainActivity.this, MainActivity.class);   
  3. // 获得PendingIntent  
  4. PendingIntent pIntent = PendingIntent.getActivity(MainActivity.this0, intent, 0);   
  5. // 设置事件信息  
  6. notification.setLatestEventInfo(MainActivity.this" Title""Content", pIntent);  

4.发出通知

 
  1. //Notification标示ID  
  2. private static final int ID = 1;  
  3. //发出通知  
  4. mNotificationManager.notify(ID, n); 

   下面是具体的例子,在这个例子里定义了一个MainActivity发出广播通知,定义一个MyReceiver类继承Broadcasts接受通知,当接收完通知之后,启动一个SecondActivity,在SecondActivity类中通过Notification和NotificationManager来可视化显示广播通知。具体的步骤如下:

MainActivity.java

 
  1. package com.android.notification;  
  2.  
  3. import android.app.Activity;  
  4. import android.content.Intent;  
  5. import android.os.Bundle;  
  6. import android.view.View;  
  7. import android.view.View.OnClickListener;  
  8. import android.widget.Button;  
  9.  
  10. public class MainActivity extends Activity {  
  11.     // 声明Button  
  12.     private Button btn;  
  13.     // 定义Broadcast Receiver action  
  14.     private static final String MY_ACTION = "com.android.notification.MY_ACTION";  
  15.     @Override 
  16.     public void onCreate(Bundle savedInstanceState) {  
  17.         super.onCreate(savedInstanceState);  
  18.         // 设置当前布局视图  
  19.         setContentView(R.layout.main);  
  20.         // 实例化Button  
  21.         btn = (Button)findViewById(R.id.Button1);  
  22.         // 添加事件监听器  
  23.         btn.setOnClickListener(listener);  
  24.     }  
  25.     // 创建事件监听器  
  26.     private OnClickListener listener = new OnClickListener() {  
  27.         @Override 
  28.         public void onClick(View v) {  
  29.             // 实例化Intent  
  30.             Intent intent = new Intent();  
  31.             // 设置Intent action属性  
  32.             intent.setAction(MY_ACTION);  
  33.             // 发起广播  
  34.             sendBroadcast(intent);  
  35.         }  
  36.     };  

MyReceiver.java

 
  1. package com.android.notification;  
  2.  
  3. import android.content.BroadcastReceiver;  
  4. import android.content.Context;  
  5. import android.content.Intent;  
  6.  
  7. public class MyReceiver extends BroadcastReceiver{  
  8.     @Override 
  9.     public void onReceive(Context context, Intent intent) {  
  10.         // 实例化Intent  
  11.         Intent i = new Intent();  
  12.         // 在新的任务中启动Activity  
  13.         i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);  
  14.         // 设置Intent启动的组件名称  
  15.         i.setClass(context, SecondActivity.class);  
  16.         // 启动Activity显示通知  
  17.         context.startActivity(i);  
  18.     }  
  19. }  

SecondActivity.java

 
  1. package com.android.notification;  
  2.  
  3. import android.app.Activity;  
  4. import android.app.Notification;  
  5. import android.app.NotificationManager;  
  6. import android.app.PendingIntent;  
  7. import android.content.Intent;  
  8. import android.os.Bundle;  
  9. import android.view.View;  
  10. import android.view.View.OnClickListener;  
  11. import android.widget.Button;  
  12.  
  13. public class SecondActivity extends Activity {  
  14.     // 声明按钮  
  15.     private Button cancelBtn;  
  16.     // 声明Notification  
  17.     private Notification notification ;  
  18.     // 声明NotificationManager  
  19.     private NotificationManager mNotification;  
  20.     // Notification标示ID  
  21.     private static final int ID = 1;  
  22.     @Override 
  23.     public void onCreate(Bundle savedInstanceState) {  
  24.         super.onCreate(savedInstanceState);  
  25.         setContentView(R.layout.second);  
  26.         // 实例化按钮  
  27.         cancelBtn = (Button)findViewById(R.id.cancelButton2);  
  28.         // 获得NotificationManager实例  
  29.         String service = NOTIFICATION_SERVICE;  
  30.         mNotification = (NotificationManager)getSystemService(service);  
  31.           
  32.         // 实例化Notification  
  33.         notification = new Notification();  
  34.         // 设置显示图标,该图标会在状态栏显示  
  35.         int icon = notification.icon = android.R.drawable.stat_notify_chat;   
  36.         // 设置显示提示信息,该信息也会在状态栏显示  
  37.         String tickerText = "Test Notification";   
  38.         // 显示时间  
  39.         long when = System.currentTimeMillis();  
  40.         notification.icon = icon;  
  41.         notification.tickerText = tickerText;  
  42.         notification.when = when;  
  43.           
  44.         // 实例化Intent  
  45.         Intent intent = new Intent(this, MainActivity.class);   
  46.         // 获得PendingIntent  
  47.         PendingIntent pi = PendingIntent.getActivity(this0, intent, 0);   
  48.         // 设置事件信息  
  49.         notification.setLatestEventInfo(this"消息""Hello Android", pi);   
  50.         // 发出通知  
  51.         mNotification.notify(ID, notification);  
  52.           
  53.         // 为按钮添加监听器  
  54.         cancelBtn.setOnClickListener(cancelListener);  
  55.     }  
  56.       
  57.     // 取消通知监听器  
  58.      private OnClickListener cancelListener = new OnClickListener() {  
  59.         @Override 
  60.         public void onClick(View v) {  
  61.             // 取消通知  
  62.             mNotification.cancel(ID);  
  63.         }  
  64.     };  

main.xml

 
  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  3.     android:orientation="vertical" 
  4.     android:layout_width="fill_parent" 
  5.     android:layout_height="fill_parent" 
  6.     > 
  7.     <Button   
  8.         android:text="发出广播通知"   
  9.         android:id="@+id/Button1"   
  10.         android:layout_width="wrap_content"   
  11.         android:layout_height="wrap_content" 
  12.         /> 
  13. </LinearLayout> 

second.xml

 
  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  3.     android:orientation="vertical" 
  4.     android:layout_width="fill_parent" 
  5.     android:layout_height="fill_parent" 
  6.     > 
  7.     <TextView   
  8.         android:text="显示通知界面"   
  9.         android:id="@+id/TextView1"   
  10.         android:layout_width="wrap_content"   
  11.         android:layout_height="wrap_content" 
  12.         />    
  13.     <Button   
  14.         android:text="取消通知"   
  15.         android:id="@+id/cancelButton2"   
  16.         android:layout_width="wrap_content"   
  17.         android:layout_height="wrap_content" 
  18.         />        
  19. </LinearLayout> 

在AndroidManifest.xml文件中16~21加入对receiver,SecondActivity的声明

 
  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android" 
  3.       package="com.android.notification" 
  4.       android:versionCode="1" 
  5.       android:versionName="1.0"> 
  6.     <uses-sdk android:minSdkVersion="10" /> 
  7.  
  8.     <application android:icon="@drawable/icon" android:label="@string/app_name"> 
  9.         <activity android:name=".MainActivity" 
  10.                   android:label="@string/app_name"> 
  11.             <intent-filter> 
  12.                 <action android:name="android.intent.action.MAIN" /> 
  13.                 <category android:name="android.intent.category.LAUNCHER" /> 
  14.             </intent-filter> 
  15.         </activity> 
  16.         <receiver android:name="MyReceiver"> 
  17.             <intent-filter> 
  18.                 <action android:name="com.android.notification.MY_ACTION"/> 
  19.             </intent-filter> 
  20.         </receiver>       
  21.         <activity android:name="SecondActivity"/>          
  22.     </application> 
  23. </manifest> 

效果图:

  

 Notification丰富的提示方式:

声音提醒

·使用默认声音

notification.defaults |= Notification.DEFAULT_SOUND;

·使用自定义声音

notification.sound = Uri.parse("file:///sdcard/notification/ringer.mp3");

·注:如果定义了默认声音,那么自定义声音将被覆盖

振动提醒

·使用默认振动

notification.defaults |= Notification.DEFAULT_VIBRATE;

·使用自定义振动

long[] vibrate = {0,100,200,300}; 
notification.vibrate = vibrate;

·注:如果定义了默认振动,那么自定义振动将被覆盖

灯光闪烁提醒

·使用默认闪烁

notification.defaults |= Notification.DEFAULT_LIGHTS;

·使用自定义闪烁

notification.ledARGB = 0xff00ff00; // LED灯的颜色,绿灯
notification.ledOnMS = 300; // LED灯显示的毫秒数,300毫秒
notification.ledOffMS = 1000; // LED灯关闭的毫秒数,1000毫秒
notification.flags |= Notification.FLAG_SHOW_LIGHTS; // 必须加上这个标志

更多特性

可以通过 Notification 的相关字段或标志(flags)为提醒设置更多的特性。

·FLAG_AUTO_CANCEL 标志:当提醒被用户点击之后会自动被取消(cancel);

·FLAG_INSISTENT 标志:在用户响应之前会一直重复提醒音;

·FLAG_ONGOING_EVENT 标志:Add this to the flags field to group the notification under

the "Ongoing" title in the Notifications window.

·FLAG_NO_CLEAR 标志:当在提醒栏中点击“清除提醒”按钮时,该提醒将不会被清除;

·number 字段:This value indicates the current number of events represented by the notification.

The appropriate number is overlaid on top of the status bar icon. If you intend to use this

field, then you must start with "1" when the Notification is first created. (If you change

the value from zero to anything greater during an update, the number is not shown.)

·iconLevel 字段:This value indicates the current level of a LevelListDrawable that is used for the notification icon. You can animate the icon in the status bar by changing this value to correlate with the drawable's defined in a LevelListDrawable.

·contentView 字段:To define your own layout for the expanded message, instantiate a

RemoteViews object and pass it to the contentView field of your Notification. Pass the

PendingIntent to the contentIntent field.



本文转自 lingdududu 51CTO博客,原文链接: 

http://blog.51cto.com/liangruijun/657502
相关文章
|
20天前
|
Java Android开发
Android 开发获取通知栏权限时会出现两个应用图标
Android 开发获取通知栏权限时会出现两个应用图标
12 0
|
1月前
|
XML 缓存 Android开发
Android开发,使用kotlin学习多媒体功能(详细)
Android开发,使用kotlin学习多媒体功能(详细)
94 0
|
2月前
|
Android开发
安卓SO层开发 -- 编译指定平台的SO文件
安卓SO层开发 -- 编译指定平台的SO文件
30 0
|
1月前
|
设计模式 人工智能 开发工具
安卓应用开发:构建未来移动体验
【2月更文挑战第17天】 随着智能手机的普及和移动互联网技术的不断进步,安卓应用开发已成为一个热门领域。本文将深入探讨安卓平台的应用开发流程、关键技术以及未来发展趋势。通过分析安卓系统的架构、开发工具和框架,本文旨在为开发者提供全面的技术指导,帮助他们构建高效、创新的移动应用,以满足不断变化的市场需求。
18 1
|
3月前
|
Android开发 开发者 iOS开发
APP开发后如何上架,上架Android应用市场前要准备什么
移动应用程序(APP)的开发已经成为现代企业和开发者的常见实践。然而,开发一个成功的APP只是第一步,将其上架到应用商店让用户下载和使用是实现其潜力的关键一步。
|
1月前
|
机器学习/深度学习 调度 Android开发
安卓应用开发:打造高效通知管理系统
【2月更文挑战第14天】 在移动操作系统中,通知管理是影响用户体验的关键因素之一。本文将探讨如何在安卓平台上构建一个高效的通知管理系统,包括服务、频道和通知的优化策略。我们将讨论最新的安卓开发工具和技术,以及如何通过这些工具提高通知的可见性和用户互动性,同时确保不会对用户造成干扰。
33 1
|
11天前
|
XML 开发工具 Android开发
构建高效的安卓应用:使用Jetpack Compose优化UI开发
【4月更文挑战第7天】 随着Android开发不断进化,开发者面临着提高应用性能与简化UI构建流程的双重挑战。本文将探讨如何使用Jetpack Compose这一现代UI工具包来优化安卓应用的开发流程,并提升用户界面的流畅性与一致性。通过介绍Jetpack Compose的核心概念、与传统方法的区别以及实际集成步骤,我们旨在提供一种高效且可靠的解决方案,以帮助开发者构建响应迅速且用户体验优良的安卓应用。
|
20天前
|
Android开发
Android开发小技巧:怎样在 textview 前面加上一个小图标。
Android开发小技巧:怎样在 textview 前面加上一个小图标。
10 0
|
20天前
|
Android开发
Android 开发 pickerview 自定义选择器
Android 开发 pickerview 自定义选择器
12 0
|
27天前
|
Java Android开发
Android开发系列全套课程
本系列课程面向有java基础,想进入企业从事android开发的计算机专业者。学习搭配实战案例,高效掌握岗位知识。
17 1