android添加通知到顶部任务栏

public class NotificationtestActivity extends Activity {
    private static final int ID = 1213;
    private static final String KEY_COUNT="keyCount";
    private int count;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Intent intent=this.getIntent();
        count=intent.getIntExtra(KEY_COUNT,0);        
       
        this.setTitle("这是第"+Integer.toString(count)+"个");
        
        Button btn=(Button) this.findViewById(R.id.button1);
        btn.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				AddNotification();
				NotificationtestActivity.this.finish();
			}
		});
    }
    /**
    * 添加顶部通知
    * @author liuzhao
    */
    public void AddNotification(){
    	count++;
		//添加通知到顶部任务栏
		//获得NotificationManager实例
		String service = NOTIFICATION_SERVICE;
		NotificationManager nm = (NotificationManager)getSystemService(service);
		//实例化Notification
		Notification n = new Notification();
		//设置显示图标
		int icon = R.drawable.icon;
		//设置提示信息
		String tickerText ="我的程序";
		//显示时间
		long when = System.currentTimeMillis();
		 
		n.icon = icon;
		n.tickerText = tickerText;
		n.when = when;
		//显示在“正在进行中”
		//  n.flags = Notification.FLAG_ONGOING_EVENT;
		n.flags|=Notification.FLAG_AUTO_CANCEL; //自动终止
		//实例化Intent
		Intent it = new Intent(this,NotificationtestActivity.class);
		it.putExtra(KEY_COUNT, count);
		/*********************
		 *获得PendingIntent  
		 *FLAG_CANCEL_CURRENT:
		 *		如果当前系统中已经存在一个相同的PendingIntent对象,
		 *		那么就将先将已有的PendingIntent取消,然后重新生成一个PendingIntent对象。 
		 *FLAG_NO_CREATE:
		 *		如果当前系统中不存在相同的PendingIntent对象,
		 *		系统将不会创建该PendingIntent对象而是直接返回null。 
		 *FLAG_ONE_SHOT:
		 *		该PendingIntent只作用一次,
		 *		如果该PendingIntent对象已经触发过一次,
		 *		那么下次再获取该PendingIntent并且再触发时,
		 *		系统将会返回一个SendIntentException,在使用这个标志的时候一定要注意哦。 
		 *FLAG_UPDATE_CURRENT:
		 *		如果系统中已存在该PendingIntent对象,
		 *		那么系统将保留该PendingIntent对象,
		 *		但是会使用新的Intent来更新之前PendingIntent中的Intent对象数据,
		 *		例如更新Intent中的Extras。这个非常有用,
		 *		例如之前提到的,我们需要在每次更新之后更新Intent中的Extras数据,
		 *		达到在不同时机传递给MainActivity不同的参数,实现不同的效果。 
		 *********************/
		 
		PendingIntent pi = PendingIntent.getActivity(this, 0, it, PendingIntent.FLAG_UPDATE_CURRENT);
		
		//设置事件信息,显示在拉开的里面
		n.setLatestEventInfo(NotificationtestActivity.this,"我的软件"+Integer.toString(count), "我的软件正在运行……", pi);
	 
		//发出通知
		nm.notify(ID,n);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值