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

   Notification和NotificationManager操作相对比较简单,一般获取系统级的服务NotificationManager,然后实例化Notification,设置它的属性,通过NotificationManager发出通知就可以了。基本步骤如下:

1.获取NotificationManager

 String service = Context.NOTIFICATION_SERVICE;

NotificationManager mNotificationManager =(NotificationManager)getSystemService(service);

2.实例化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.

你可能感兴趣的:(android,移动开发,notification,休闲)