关于通知栏的使用:

Notification及NotificationManager的使用详解 

 


相关类:

import android.app.NotificationManager;
import android.app.PendingIntent;

import android.app.Notification;

1、使用标准的布局来显示通知信息

[java] view plain copy
  1. Intent intent = new Intent(MainActivity.this, Other.class);  
  2.                 PendingIntent pi = PendingIntent.getActivity(MainActivity.this,  
  3.                         0, intent, 0);  
  4.   
  5.                 Notification notify = new Notification();  
  6.                 notify.icon = R.drawable.ic_launcher; // 通知狼的两个图标  
  7.                 notify.tickerText = "启动其他activity的通知"// 通知栏浮动的时候显示的信息  
  8.                 notify.when = System.currentTimeMillis();   //设置时间  
  9.                 notify.defaults = Notification.DEFAULT_SOUND; //默认的声音  
  10.                 notify.defaults = Notification.DEFAULT_ALL;  
  11.                 notify.setLatestEventInfo(MainActivity.this"普通通知 ""点击查看",pi);//使用标准的布局来显示通知信息  
  12.   
  13.                 NotificationManager no = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);  
  14.                 no.notify(NOTIFICATION_ID, notify);  

2、使用RemoteViews自定义布局 [java] view plain copy
  1. Intent intent = new Intent(MainActivity.this,Other.class);  
  2.                   //intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);                                  
  3.                   PendingIntent pi = PendingIntent.getBroadcast(MainActivity.this0, intent, 0);  
  4.          
  5.                   Notification notification = new Notification();  
  6.                   notification.tickerText="fadf";  
  7.                   notification.icon=R.drawable.ic_action_search;  
  8.                     
  9.                   RemoteViews  contentview = new RemoteViews(getPackageName(),R.layout.status_bar_balloon);  
  10.                   //这种是你自定义一个布局进行显示的。  
  11.                   contentview.setTextViewText(R.id.text, "你好吧");  
  12.                   contentview.setImageViewResource(R.id.icon, R.drawable.ic_action_search);  
  13.                   //在视图中添加图片和文本进行显示。  
  14.                   notification.contentView = contentview ;  
  15.                     
  16.                   NotificationManager  ni = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);  
  17.                   ni.notify(NOTIFICATION_ID, notification);        

3、带参数的Notification构造方法实现通知

[java] view plain copy
  1. Intent intent = new Intent(MainActivity.this,Other.class);  
  2.                 //intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);                                  
  3.                 PendingIntent pi = PendingIntent.getBroadcast(MainActivity.this0, intent, 0);  
  4.                                
  5.                 Notification notification = new Notification(R.drawable.ic_action_search,"fadf",SystemClock.currentThreadTimeMillis());  
  6.                  //这一种里面的构造方法,可以理解为使用默认的布局进行现实的,显示图标,滚动图标的显示内容,以及显示时间。  
  7.                  notification.setLatestEventInfo(MainActivity.this,"你好""还不错", pi); //在通知栏中现实的内容  
  8.          
  9.                 NotificationManager  ni = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);  
  10.                 ni.notify(NOTIFICATION_ID, notification); 

更多相关文章

  1. android自定义通知栏并通过广播实现监听
  2. android之自定义ViewGroup和自动换行的布局的实现
  3. 自定义Android标题栏TitleBar布局
  4. Android入门:HTML布局中Android程序与JAVASCRIPT的交互
  5. Android学习笔记(1) Android 布局管理器 之 LinearLayout
  6. android上滑某个布局固定悬浮
  7. Android中CoordinatorLayout(协调布局)的使用
  8. Android 5.0新控件 AppBarLayout | 嵌套布局 介绍及使用详情

随机推荐

  1. android中四个典型动画设置
  2. android binder 面试
  3. Android [Camera 源码] 外接 USB 摄像头(
  4. Android Studio -- Error retrieving par
  5. 【Gradle】自定义Android Gradle工程
  6. Android之有序广播
  7. Android android 6.0权限校验及版本兼容
  8. android内存管理(二)
  9. Android中知识点整理
  10. android解锁触发广播