Android添加通知到顶部任务栏


Android添加通知到顶部任务栏

  1. public class NotificationtestActivity extends Activity {  
  2.     private static final int ID = 1213;  
  3.     private static final String KEY_COUNT="keyCount";  
  4.     private int count;  
  5.     @Override  
  6.     public void onCreate(Bundle savedInstanceState) {  
  7.         super.onCreate(savedInstanceState);  
  8.         setContentView(R.layout.main);  
  9.         Intent intent=this.getIntent();  
  10.         count=intent.getIntExtra(KEY_COUNT,0);          
  11.          
  12.         this.setTitle("这是第"+Integer.toString(count)+"个");  
  13.           
  14.         Button btn=(Button) this.findViewById(R.id.button1);  
  15.         btn.setOnClickListener(new View.OnClickListener() {  
  16.               
  17.             @Override  
  18.             public void onClick(View v) {  
  19.                 AddNotification();  
  20.                 NotificationtestActivity.this.finish();  
  21.             }  
  22.         });  
  23.     }  
  24.     /** 
  25.     * 添加顶部通知 
  26.     * @author liuzhao 
  27.     */  
  28.     public void AddNotification(){  
  29.         count++;  
  30.         //添加通知到顶部任务栏   
  31.         //获得NotificationManager实例   
  32.         String service = NOTIFICATION_SERVICE;  
  33.         NotificationManager nm = (NotificationManager)getSystemService(service);  
  34.         //实例化Notification   
  35.         Notification n = new Notification();  
  36.         //设置显示图标   
  37.         int icon = R.drawable.icon;  
  38.         //设置提示信息   
  39.         String tickerText ="我的程序";  
  40.         //显示时间   
  41.         long when = System.currentTimeMillis();  
  42.            
  43.         n.icon = icon;  
  44.         n.tickerText = tickerText;  
  45.         n.when = when;  
  46.         //显示在“正在进行中”   
  47.         //  n.flags = Notification.FLAG_ONGOING_EVENT;   
  48.         n.flags|=Notification.FLAG_AUTO_CANCEL; //自动终止   
  49.         //实例化Intent   
  50.         Intent it = new Intent(this,NotificationtestActivity.class);  
  51.         it.putExtra(KEY_COUNT, count);  
  52.         /********************* 
  53.          *获得PendingIntent   
  54.          *FLAG_CANCEL_CURRENT: 
  55.          *      如果当前系统中已经存在一个相同的PendingIntent对象, 
  56.          *      那么就将先将已有的PendingIntent取消,然后重新生成一个PendingIntent对象。  
  57.          *FLAG_NO_CREATE: 
  58.          *      如果当前系统中不存在相同的PendingIntent对象, 
  59.          *      系统将不会创建该PendingIntent对象而是直接返回null。  
  60.          *FLAG_ONE_SHOT: 
  61.          *      该PendingIntent只作用一次, 
  62.          *      如果该PendingIntent对象已经触发过一次, 
  63.          *      那么下次再获取该PendingIntent并且再触发时, 
  64.          *      系统将会返回一个SendIntentException,在使用这个标志的时候一定要注意哦。  
  65.          *FLAG_UPDATE_CURRENT: 
  66.          *      如果系统中已存在该PendingIntent对象, 
  67.          *      那么系统将保留该PendingIntent对象, 
  68.          *      但是会使用新的Intent来更新之前PendingIntent中的Intent对象数据, 
  69.          *      例如更新Intent中的Extras。这个非常有用, 
  70.          *      例如之前提到的,我们需要在每次更新之后更新Intent中的Extras数据, 
  71.          *      达到在不同时机传递给MainActivity不同的参数,实现不同的效果。  
  72.          *********************/  
  73.            
  74.         PendingIntent pi = PendingIntent.getActivity(this0, it, PendingIntent.FLAG_UPDATE_CURRENT);  
  75.           
  76.         //设置事件信息,显示在拉开的里面   
  77.         n.setLatestEventInfo(NotificationtestActivity.this,"我的软件"+Integer.toString(count), "我的软件正在运行……", pi);  
  78.        
  79.         //发出通知   
  80.         nm.notify(ID,n);  
  81.     }  
  82. }  

 

相关内容