Notification 和 NotificationManager 的基本用法

来源:互联网 发布:庭院流水桌面软件 编辑:程序博客网 时间:2024/06/07 01:11
Notification 和 NotificationManager 的基本用法 
转自:http://sunzone.iteye.com/blog/1998123
1. 用途:用来设置通知; 
2. 说明:NotificationManager 为后台运行的服务 , 用来发送通知;Notification 类表示一个持久性的通知 
3. 状态栏和状态条的区别: 
a) 状态条:手机屏幕最上方的一个条形状的区域;状态条有很多的信息量,例如:usb连接图标,手机信号图标,电池电量图标,时间图标等等 
b) 状态栏:手机从状态条滑下来的可以伸缩的view;  状态栏中一般有两类: 
i. 正在进行的程序; 
ii. 通知事件 
4. 一般Notification 传送的信息: 
a) 一个状态条图标 
b) 在拉伸状态栏中显示带有大标题,小标题,图标的信息,并且有处理该点击事件;比如调用该程序的入口类; 
c) 闪光、led 或者震动; 
5. 创建Notification 的步骤: 
a) 获取NotificationManager对象:NotificationManager  nm = getSystemService(Service.NOTIFICATION_SERVICE); 
b) 设置属性:内容,图标,标题,相应的处理动作; 
c) 通过nm.notify(); 方法来执行一个notification快讯; 
d) 通过nm.cance(); 方法取消一个快讯; 
6. Notification 类中的常量、字段、方法介绍: 
a) DEFAULT_ALL  使用所有默认值,声音、震动、闪屏等 
b) DEFAULT_LIGHTS  使用默认灯光提示; 
c) DEFAULT_SOUNDS   使用默认提示音 
d) DEFAULT_VIBRATE 使用默认手机震动 
e) 提示:这些效果常量可以叠加; 
7. 相关权限: 
a) 手机震动:<uses-permission android:name=”android.permission.VIBRATE”/> 
8. 实现:见代码….. 

Java代码  收藏代码
  1. package com.example.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.app.Service;  
  8. import android.content.Intent;  
  9. import android.net.Uri;  
  10. import android.os.Bundle;  
  11. import android.provider.MediaStore.Audio;  
  12. import android.util.Log;  
  13. import android.view.View;  
  14. import android.view.View.OnClickListener;  
  15. import android.widget.Button;  
  16.   
  17. public class MainActivity extends Activity {  
  18.   
  19.     private Button send, cancel;  
  20.     private Notification notification; // 通知  
  21.     private NotificationManager notificationManager; // 通知系统服务  
  22.   
  23.     /** 
  24.      * 获取布局文件中控件的对象 
  25.      */  
  26.     public void init() {  
  27.         Log.i("msg""init()...调用");  
  28.         send = (Button) findViewById(R.id.send);  
  29.         cancel = (Button) findViewById(R.id.cancel);  
  30.     }  
  31.   
  32.     // 程序入口  
  33.     protected void onCreate(Bundle savedInstanceState) {  
  34.         super.onCreate(savedInstanceState);  
  35.         setContentView(R.layout.activity_main);  
  36.   
  37.         init(); // 初始化  
  38.   
  39.         // 设置监听  
  40.         send.setOnClickListener(listener);  
  41.         cancel.setOnClickListener(listener);  
  42.   
  43.     }  
  44.   
  45.     /** 
  46.      * 发出通知 -- 
  47.      */  
  48.     public void sendNotification() {  
  49.         // 获取对象  
  50.         notificationManager = (NotificationManager) this  
  51.                 .getSystemService(Service.NOTIFICATION_SERVICE);  
  52.         notification = new Notification();  
  53.         notification.icon = R.drawable.ic_launcher; // 设置图标,公用图标  
  54.         notification.tickerText = "状态条标题,提示标题";  
  55.         notification.when = System.currentTimeMillis(); // 当前时间 ,通知时间  
  56.           
  57.         // 提示音  
  58.         notification.defaults = Notification.DEFAULT_SOUND;  
  59.         notification.sound = Uri.parse("file:///sdcard/notification/ringer.mp3"); // 播放指定位置音乐  
  60.         notification.sound = Uri.withAppendedPath(Audio.Media.INTERNAL_CONTENT_URI, "6"); // 系统音乐  
  61.           
  62.         // 手机震动 -- 权限: <uses-permission android:name="android.permission.VIBRATE"/>  
  63.         notification.defaults = Notification.DEFAULT_VIBRATE;  
  64.         long[] vibrate = {0,100,200,300};  
  65.         notification.vibrate = vibrate;  
  66.           
  67.         // LED 灯闪烁  
  68.         notification.defaults = Notification.DEFAULT_LIGHTS;  
  69.         notification.ledARGB=0xff00ff00;  
  70.         notification.ledOffMS = 1000;  
  71.         notification.ledOnMS = 300// 闪光时间,毫秒  
  72.       
  73.         /* 
  74.          * 设置Flag的值:说明 
  75.          * FLAG_AUTO_CANCEL : 通知能被状态按钮清除掉 
  76.          * FLAG_NO_CLEAR : 点击清除按钮,不清除 
  77.          * FLAG_ONGOING_EVENT:  该通知放置在正在运行组中 
  78.          * FLAG_INSISTENT : 是否一直进行,比如播放音乐,直到用户响应 
  79.          */  
  80.         notification.flags = Notification.FLAG_ONGOING_EVENT;  
  81.           
  82.   
  83.   
  84.         Intent intent = new Intent(MainActivity.this, MainActivity.class);  
  85.         PendingIntent pi = PendingIntent.getActivity(this0, intent,  
  86.                 PendingIntent.FLAG_UPDATE_CURRENT);  
  87.   
  88.         /* 
  89.          * 功能:显示在拉伸状态栏中的Notification属性,点击后将发送PendingIntent对象  
  90.          * 1:上下文环境 
  91.          * 2:状态栏中的大标题 
  92.          * 3:状态栏中的小标题; 
  93.          * 4:点击后发送的PendingIntent对象 
  94.          */  
  95.         notification.setLatestEventInfo(this"状态栏标题""状态栏内容", pi);  
  96.         notificationManager.notify(1, notification);  
  97.     }  
  98.   
  99.     /** 
  100.      * 取消通知 
  101.      */  
  102.     public void cancelNotification() {  
  103.         notificationManager.cancel(1);  
  104.     }  
  105.   
  106.     // 按钮点击监听器  
  107.     private OnClickListener listener = new View.OnClickListener() {  
  108.   
  109.         public void onClick(View v) {  
  110.   
  111.             if (v.getId() == R.id.send) { // 发出通知  
  112.                 sendNotification();  
  113.                 return;  
  114.             }  
  115.             if (v.getId() == R.id.cancel) { // 取消通知  
  116.                 cancelNotification();  
  117.                 return;  
  118.             }  
  119.   
  120.         }  
  121.     };  
  122. }  

下面是布局文件: 
Java代码  收藏代码
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     android:layout_width="fill_parent"  
  3.     android:layout_height="fill_parent"  
  4.     android:orientation="vertical" >  
  5.   
  6.     <Button  
  7.         android:id="@+id/send"  
  8.         android:layout_width="fill_parent"  
  9.         android:layout_height="wrap_content"  
  10.         android:text="发送通知" />  
  11.   
  12.     <Button  
  13.         android:id="@+id/cancel"  
  14.         android:layout_width="fill_parent"  
  15.         android:layout_height="wrap_content"  
  16.         android:text="取消通知" />  
  17.   
  18.   
  19. </LinearLayout>  
  • notification.rar (893.2 KB)
  • 下载次数: 1
0 0
原创粉丝点击