PendingIntent的使用

PendingIntent的使用
  * Flags的类型:
    FLAG_ONE_SHOT:得到的pi只能使用一次,第二次使用该pi时报错
    FLAG_NO_CREATE: 当pi不存在时,不创建,返回null
    FLAG_CANCEL_CURRENT: 每次都创建一个新的pi
    FLAG_UPDATE_CURRENT: 不存在时就创建,创建好了以后就一直用它,每次使用时都会更新pi的数据(使用较多)
    
  * 在AlarmManager中的使用
    Intent intent = new Intent("action", null, context, serviceClass);
		PendingIntent pi = PendingIntent.getService(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
		AlarmManager manager = (AlarmManager)probe.getSystemService(Context.ALARM_SERVICE);
		manager.set(AlarmManager.RTC_WAKEUP, milis, pi);

  * 在NotificationManager中的使用
    Intent intent = new Intent();
        intent.setAction("myaction");
		PendingIntent pi = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    	
    NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    Notification n = new Notification();
		n.icon = R.drawable.ic_launcher;
		n.when = System.currentTimeMillis();
		n.setLatestEventInfo(this,"this is title", "this is a message", pi);
		nm.notify(0, n);

  * send()方法是用,调用PendingIntent.send()会启动包装的Intent(如启动service,activity)

  * cancel()方法是为了解除PendingIntent和被包装的Intent之间的关联,此时如果再调用send()方法,则会抛出CanceledException异常


  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值