1、获得系统服务
notificationmanager=(NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
2、创建Notification
notification=new Notification(R.drawable.keai,"可爱",System.currentTimeMillis());
3、对Notification进行一些设置
notification.flags=Notification.FLAG_AUTO_CANCEL;      //设置下拉点击之后回到应用程序,可以有多个值选择
4、Intent与PendingIntent
Intent   intent=new Intent(this,Z18Activity.class);
pendingintent=PendingIntent.getActivity(this,0,intent,0);
notification.setLatestEventInfo(this,"真可爱","太可爱啦!",pendingintent);
5、可更改提示下拉条中的布局
RemoteViews rv=new RemoteViews(this.getPackageName(),R.layout.main);     //此步骤是修改下拉后看到提示条的布局
notification.contentView=rv;
6、用NotificationManager发送Notification提示信息
notificationmanager.notify(0,notification);        //通过标识发送指定的Notification

上面有些步骤是属于并列的,可能后面的操作将覆盖前面的操作

 

Example1:

Ticker Text:Notification刚出来的时候,在状态栏上滚动的字幕,如果很长,会自动分割滚动
Content Title:Notification展开后的标题
Content Text:Notification展开后的内容



 

 

取得NotificationManage

Java代码  
  1. private NotificationManager mNotificationManager;  
  2. mNotificationManager = (NotificationManager)   
  3.         getSystemService(Context.NOTIFICATION_SERVICE);  

 创建Notification并且显示

Java代码  
  1. //Notification的滚动提示  
  2. String tickerText = "My notification, It's a long text! Hello World desiyo?";  
  3. //Notification的图标,一般不要用彩色的  
  4. int icon = R.drawable.icon_02241_3;  
  5.    
  6. //contentTitle和contentText都是标准的Notification View的内容  
  7. //Notification的内容标题,拖下来后看到的标题  
  8. String contentTitle="My notification";  
  9. //Notification的内容  
  10. String contentText="Hello World!";  
  11.    
  12. //Notification的Intent,即点击后转向的Activity  
  13. Intent notificationIntent = new Intent(thisthis.getClass());  
  14. notificationIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);  
  15. PendingIntent contentIntent = PendingIntent.getActivity(this0,   
  16.         notificationIntent, 0);  
  17.    
  18. //创建Notifcation  
  19. Notification notification = new Notification(icon, tickerText, System.currentTimeMillis());  
  20. //设定Notification出现时的声音,一般不建议自定义  
  21. notification.defaults |= Notification.DEFAULT_SOUND;  
  22. //设定如何振动  
  23. notification.defaults |= Notification.DEFAULT_VIBRATE;  
  24. //指定Flag,Notification.FLAG_AUTO_CANCEL意指点击这个Notification后,立刻取消自身  
  25. //这符合一般的Notification的运作规范  
  26. notification.flags|=Notification.FLAG_AUTO_CANCEL;  
  27. notification.setLatestEventInfo(this, contentTitle, contentText, contentIntent);  
  28.   
  29. //显示这个notification  
  30. mNotificationManager.notify(HELLO_ID, notification);  

 这是最基本的应用,可以说除了找个合适的图标以外,其它都很简单。

使用自定义View的Notification

同Toast一样,我们也可以自已指定1个View来作为Notification展开后的显示内容,比如说在Android Market中下载的时候,Notification中会显示当前下载的进度,那么我们也来模拟1个这样的效果吧。

 
首先给出View的定义文件:notification_view_sample.xml
Java代码  
  1. <?xml version="1.0" encoding="utf-8"?>  
  2.         xmlns:android="http://schemas.android.com/apk/res/android"  
  3.         android:layout_width="fill_parent"  
  4.         android:layout_height="fill_parent"  
  5.         android:padding="3dp"  
  6. >  
  7.         "@+id/notificationImage"  
  8.                 android:layout_width="wrap_content" android:layout_height="wrap_content"  
  9.                 android:src="@android:drawable/stat_sys_download"  
  10.         />  
  11.         "@+id/notificationTitle"  
  12.                 android:layout_width="wrap_content" android:layout_height="wrap_content"  
  13.                 android:layout_toRightOf="@id/notificationImage"  
  14.                 android:layout_alignParentRight="true"  
  15.                 android:paddingLeft="6dp"  
  16.                 android:textColor="#FF000000"  
  17.         />  
  18.         "@+id/notificationPercent"  
  19.                 android:layout_width="wrap_content" android:layout_height="wrap_content"  
  20.                 android:layout_below="@id/notificationImage"  
  21.                 android:paddingTop="2dp"  
  22.                 android:textColor="#FF000000"  
  23.         />  
  24.         "@+id/notificationProgress"  
  25.                 android:layout_width="wrap_content" android:layout_height="wrap_content"  
  26.                 android:layout_below="@id/notificationTitle"  
  27.                 android:layout_alignLeft="@id/notificationTitle"  
  28.                 android:layout_alignParentRight="true"  
  29.                 android:layout_alignTop="@id/notificationPercent"  
  30.                 android:paddingLeft="6dp"  
  31.                 android:paddingRight="3dp"  
  32.                 android:paddingTop="2dp"  
  33.                 style="?android:attr/progressBarStyleHorizontal"  
  34.         />  
  35.   
  Java代码  
  1. //Notification的滚动提示  
  2. String tickerText1 = "Custom view for download notification";  
  3. //Notification的图标,一般不要用彩色的  
  4. int icon1 = android.R.drawable.stat_sys_download;  
  5.    
  6. //Notification的Intent,即点击后转向的Activity  
  7. Intent notificationIntent1 = new Intent(thisthis.getClass());  
  8. notificationIntent1.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);  
  9. PendingIntent contentIntent1 = PendingIntent.getActivity(this0, notificationIntent1, 0);  
  10.    
  11. //创建Notifcation  
  12. Notification notification1 = new Notification(icon1, tickerText1, System.currentTimeMillis());  
  13. //设定Notification出现时的声音,一般不建议自定义  
  14. notification1.defaults |= Notification.DEFAULT_SOUND;  
  15. //设定是否振动  
  16. notification1.defaults |= Notification.DEFAULT_VIBRATE;  
  17. //notification.number=numbers++;  
  18. //指定Flag,Notification.FLAG_AUTO_CANCEL意指点击这个Notification后,立刻取消自身  
  19. //这符合一般的Notification的运作规范  
  20. notification1.flags|=Notification.FLAG_ONGOING_EVENT;  
  21.    
  22. //创建RemoteViews用在Notification中  
  23. RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.notification_view_sample);  
  24. contentView.setTextViewText(R.id.notificationTitle, "Download:Facebook for android");  
  25. contentView.setTextViewText(R.id.notificationPercent, "35%");  
  26. contentView.setProgressBar(R.id.notificationProgress, 10035false);  
  27.    
  28. notification1.contentView = contentView;  
  29. notification1.contentIntent=contentIntent1;  
  30.    
  31. //显示这个notification  
  32. mNotificationManager.notify(CUSTOM_VIEW_ID, notification1);  

 注意以上代码中使用的是RemoteViews,而不是普通的View,另外使用的是PendingIntent而不是普通的Intent,这都说明了Notification是1个“远程”的东西,其中能够使用的控件是受限制的,比如说TableLayout就不能使用。看下效果图,是不是和Market中的界面很接近呢?

 

由于在使用自定义的view时不必使用setLastEventInfo()方法,所以你必须为Notification的contentIntent域定义一个Intent,如下所示:

Java代码  
  1. RemoteViews contentView = new RemoteViews(getPackageName(),R.layout.notification_view);  
  2. contentView.setTextViewText(R.id.notificationTitle, "下载文件:测试文件");  
  3. contentView.setTextViewText(R.id.notificationPercent, "55%");  
  4. contentView.setProgressBar(R.id.notificationProgress, 10055false);  
  5.   
  6. notice.contentView = contentView;  
  7. notice.contentIntent = contentIntent;  
 

 注意:当为notification创建自定义的布局时,你必须确保它能够再不同的设备方向以及分辨率下都正常工作。这对于所有的布局工作都是十分重要的,所以,不要定义过于复杂的布局,而且要在多种情况下进行测试。

 

更好的控制Notification动画图标怎么做?
和selector类似,定义1个XML文件放在drawable下,下面是之前用到的stat_sys_download的定义:

Java代码  
  1.         xmlns:android="http://schemas.android.com/apk/res/android"  
  2.         android:oneshot="false">  
  3.     "@drawable/stat_sys_download_anim0" android:duration="200" />  
  4.     "@drawable/stat_sys_download_anim1" android:duration="200" />  
  5.     "@drawable/stat_sys_download_anim2" android:duration="200" />  
  6.     "@drawable/stat_sys_download_anim3" android:duration="200" />  
  7.     "@drawable/stat_sys_download_anim4" android:duration="200" />  
  8.     "@drawable/stat_sys_download_anim5" android:duration="200" />  
  9.   

 如何更新Notification?

注意到前面的代码中用到的CUSTOM_VIEW_ID,这是Notification的ID,如果2次弹出的Notification的ID相同,那么Notification就只会更新而不会再次滚动提醒。之前给出的ProgressBar是不会动的,利用这个方法就可以让它动起来(或者也可以直接调用RemoteView的set方法来直接更新?未试验)
如何自定义提示的声音和振动

//自定义提示音notification.sound = Uri.parse("file:///sdcard/notification/ringer.mp3");

//自定义振动方式long[] vibrate = {0,100,200,300};notification.vibrate = vibrate;
请注意:如果使用了DEFAULT_SOUND或DEFAULT_VIBRATE,则自定义的提示音和振动无效。
在类似于短消息的应用中如何提示数量?
使用Notification的number属性,默认为0,如果是1或更大的数字,则会在图标上覆盖显示这个数字。
notification.number=notificationNumber;
Flag的使用
notification有1个flag属性,除了DEFAULT_SOUND之外,还有几个很有用的属性。
FLAG_AUTO_CANCEL:自动清除Notification,前面的例子中有用到
FLAG_INSISTENT:提示音一直不停,直至用户响应(很吵吧!)
FLAG_ONGOING_EVENT:表示这是1个正在进行的任务,不可以清除,第2个例子中有用到
FLAG_NO_CLEAR:不可以清除

实例来源:

http://www.apkbus.com/forum.php?mod=viewthread&tid=20486

参考:http://www.apkbus.com/forum.php?mod=viewthread&tid=19156

 

Example2:

Java代码  
  1. Button btn4 = (Button) this.findViewById(R.id.btn4);  
  2.         btn4.setText("发出一个通知(Notification)");  
  3.         btn4.setOnClickListener(new Button.OnClickListener() {  
  4.             public void onClick(View v) {  
  5.                 // 实例化通知管理器  
  6.                 NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);  
  7.   
  8.                 // 指定单击通知后所打开的详细的通知页面(单击通知后打开 NotificationView)  
  9.                 PendingIntent contentIntent = PendingIntent.getActivity(  
  10.                         Main.this0new Intent(Main.this,    NotificationView.class), 0);  
  11.   
  12.                 // 实例化一个通知,并指定其图标和标题(在提示栏上显示)  
  13.                 Notification n = new Notification(R.drawable.icon01, "我是滚动的通知信息我是滚动的通知信息我是滚动的通知信息", System.currentTimeMillis());  
  14.                   
  15.                 // 设置通知的发送人和通知的详细内容(打开提示栏后在通知列表中显示)  
  16.                 n.setLatestEventInfo(Main.this"通知发送人""我是详细的通知信息我是详细的通知信息我是详细的通知信息", contentIntent);  
  17.   
  18.                 // 100 毫秒延迟后,震动 250 毫秒,暂停 100 毫秒后,再震动 500 毫秒  
  19.                 n.vibrate = new long[] { 100250100500 };  
  20.                   
  21.                 // 发出通知(其中第一个参数为通知标识符)  
  22.                 nm.notify(0, n);  
  23.             }  
  24.         });    
  Java代码  
  1. // 单击通知列表的某个通知后,所打开的详细的通知页  
  2. public class NotificationView extends Activity {  
  3.     protected void onCreate(Bundle savedInstanceState) {  
  4.         super.onCreate(savedInstanceState);  
  5.         setContentView(R.layout.view);  
  6.   
  7.         TextView txtMsg = (TextView)this.findViewById(R.id.txtMsg);  
  8.         txtMsg.setText("点通知之后所链接到的 Activity");  
  9.           
  10.         NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);  
  11.         // 取消显示在通知列表中的指定通知(参数为通知标识符)  
  12.         nm.cancel(0);  
  13.           
  14.         // 需要关闭此 Activity 的话就 finish 它既可  
  15.         // this.finish();  
  16.     }  
  17. }  

 ...

Example3:

 

Java代码  
  1. public class NotificationActivity extends Activity implements OnClickListener {  
  2.    
  3.     @Override  
  4.     public void onCreate(Bundle savedInstanceState) {  
  5.         super.onCreate(savedInstanceState);  
  6.         setContentView(R.layout.notification);  
  7.         findViewById(R.id.notificationBtn).setOnClickListener(this);  
  8.     }  
  9.    
  10.     @Override  
  11.     public void onClick(View arg0) {  
  12.         // TODO Auto-generated method stub  
  13.         addNotification();  
  14.     }  
  15.    
  16.     private void addNotification() {  
  17.         NotificationManager manager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);  
  18.         Notification notification = new Notification();  
  19.         notification.icon = R.drawable.icon;  
  20.         notification.tickerText = "我在这里";  
  21.         notification.defaults = Notification.DEFAULT_SOUND;  
  22.         notification.audioStreamType = android.media.AudioManager.ADJUST_LOWER;  
  23.            
  24.         Intent intent = new Intent(this, Notification2Activity.class);  
  25.            
  26.         PendingIntent pendingIntent = PendingIntent.getActivity(this0, intent, PendingIntent.FLAG_ONE_SHOT);  
  27.         notification.setLatestEventInfo(this"短信通知""亲爱的,晚上8点老地方见哦~", pendingIntent);  
  28.         manager.notify(R.drawable.icon, notification);  
  29.     }  
  30. }  
  Java代码  
  1. public class Notification2Activity extends Activity implements OnClickListener {  
  2.    
  3.     @Override  
  4.     protected void onCreate(Bundle savedInstanceState) {  
  5.         super.onCreate(savedInstanceState);  
  6.         setContentView(R.layout.notification2);  
  7.         findViewById(R.id.cancleBtn).setOnClickListener(this);  
  8.     }  
  9.    
  10.     @Override  
  11.     public void onClick(View v) {  
  12.         // TODO Auto-generated method stub  
  13.         cancleNotification();  
  14.     }  
  15.    
  16.     private void cancleNotification() {  
  17.         // TODO Auto-generated method stub  
  18.         NotificationManager manager =  (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);  
  19.         manager.cancel(R.drawable.icon);  
  20.         Toast.makeText(this"Notification cancled", Toast.LENGTH_SHORT).show();  
  21.     }  
  22.        
  23. }  
 

都是很简单的例子,帮助理解!

更多相关文章

  1. Android(安卓)Wi-Fi Disable Roaming in wpa_supplicant
  2. 学到一个android小应用-霓虹灯闪烁效果界面
  3. Android代码规范
  4. Android如何使用Notification进行通知
  5. Android(安卓)之 下拉框(Spinner)的使用
  6. Android中Activity的Intent大全
  7. Android蓝牙通信代码
  8. android 打开通知栏
  9. Android(安卓)在Service里面启动Activity

随机推荐

  1. Android 科大讯飞 在线和离线语音听写
  2. android声音播放之SoundPool的应用,让你的
  3. android listview多视图嵌套多视图
  4. Android高可拓展框架Conductor源码解析(2
  5. Android动画精讲一:从setTranslationX谈属
  6. 安卓中MVC模式的深度思索和实践(二)
  7. Android 通知(Notification)的基本用法
  8. 使用Kotlin开发Android项目-Kibo(二)
  9. 背景图像颜色检测与Android油漆。
  10. IDEA简介和快捷键设置