PendingIntent的使用

   参看原文:http://blog.csdn.net/zeng622peng/article/details/6180190

                    http://blog.csdn.net/yuzhiboyi/article/details/8484771

  先看例子

<span style="font-size:18px;">private void showNotify(){
    	Notification notice=new Notification();
    	notice.icon=R.drawable.icon;
    	notice.tickerText="您有一条新的信息";
    	notice.defaults=Notification.DEFAULT_SOUND;
    	notice.when=10L;
    	// 100 毫秒延迟后,震动 250 毫秒,暂停 100 毫秒后,再震动 500 毫秒  
    	//notice.vibrate = new long[] { 100, 250, 100, 500 };出错?
    	//notice.setLatestEventInfo(this, "通知", "开会啦", PendingIntent.getActivity(this, 0, null, 0));
        notice.setLatestEventInfo(this, "通知", "开会啦", PendingIntent.getActivity(this, 0, new Intent(this,otherAct        ivity.class,null);<span style="color:#cc0000;">//即将跳转页面,还没跳转,这时Intent已经交给了NotigicationManager处理了。</span></span><pre name="code" class="java"><span style="font-size: 18px;">       </span><span style="font-family: Arial, Helvetica, sans-serif;">//获取系统通知服务</span>
NotificationManager manager=(NotificationManager)getSystemService(this.NOTIFICATION_SERVICE); manager.notify(0,notice); }

       例子中Intent什么时候开始被处理呢?应该在manager.notify(0,notice)中被处理,此时manager通过调用传进来的notice中的PendingIntent的send()方法。PendingIntent.send(){ 

方法中可以做这样处理,源程序Context.startActivity(Intent);  }

    什么时候调用就要看NotificationManager了。

    发送短信的例子

private final static String SEND_ACTION      = "send";
private final static String DELIVERED_ACTION = "delivered";

private void sendSms(String receiver, String text) {
    SmsManager s = SmsManager.getDefault();
    PendingIntent sentPI = PendingIntent.getBroadcast(this, 0, new Intent(SEND_ACTION),
                                                      PendingIntent.FLAG_CANCEL_CURRENT);
    PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0, new Intent(DELIVERED_ACTION),
                                                           PendingIntent.FLAG_CANCEL_CURRENT);
    // 发送完成
    registerReceiver(new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            switch (getResultCode()) {
                case Activity.RESULT_OK:
                    Toast.makeText(getBaseContext(), "Send Success!", Toast.LENGTH_SHORT).show();
                    break;
                case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                    Toast.makeText(getBaseContext(), "Send Failed because generic failure cause.",
                                   Toast.LENGTH_SHORT).show();
                    break;
                case SmsManager.RESULT_ERROR_NO_SERVICE:
                    Toast.makeText(getBaseContext(), "Send Failed because service is currently unavailable.",
                                   Toast.LENGTH_SHORT).show();
                    break;
                case SmsManager.RESULT_ERROR_NULL_PDU:
                    Toast.makeText(getBaseContext(), "Send Failed because no pdu provided.", Toast.LENGTH_SHORT).show();
                    break;
                case SmsManager.RESULT_ERROR_RADIO_OFF:
                    Toast.makeText(getBaseContext(), "Send Failed because radio was explicitly turned off.",
                                   Toast.LENGTH_SHORT).show();
                    break;
                default:
                    Toast.makeText(getBaseContext(), "Send Failed.", Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    }, new IntentFilter(SEND_ACTION));

    // 对方接受完成
    registerReceiver(new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            switch (getResultCode()) {
                case Activity.RESULT_OK:
                    Toast.makeText(getBaseContext(), "Delivered Success!", Toast.LENGTH_SHORT).show();
                    break;
                default:
                    Toast.makeText(getBaseContext(), "Delivered Failed!", Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    }, new IntentFilter(DELIVERED_ACTION));

    // 发送短信,sentPI和deliveredPI将分别在短信发送成功和对方接受成功时被广播
    s.sendTextMessage(receiver, null, text, sentPI, deliveredPI);
}




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值