Android之PendingIntent(转载/整理)
PendingIntent可以看作是对Intent的包装。供当前App之外的其他App调用。有点“被动”或是“Callback”的意思,但不是严格意义上的“被动”或是“Callback”。总之,当前App不能用它马上启动它所包裹的Intent。而是在外部App执行这个PendingIntent时,间接地、实际地调用里面的Intent。PendingIntent主要持有的信息是它所包装的Intent和当前App的Context。正由于PendingIntent中保存有当前App的Context,使它赋予外部App一种能力,使得外部App可以如同当前App一样的执行PendingIntent里的Intent,就算在执行时当前App已经不存在了,也能通过存在PendingIntent里的Context照样执行Intent。
Intent一般是用作Activity、Sercvice、BroadcastReceiver之间传递数据,而Pendingintent,可用在 Notification,闹钟,计时器,短信上,可以理解为延迟执行的intent,PendingIntent是对Intent一个包装。
PendingIntent与Intent的区别:(1)Intent是立即使用的,而PendingIntent可以等到事件发生后触发,PendingIntent可以cancel;(2)Intent在程序结束后即终止,而PendingIntent在程序结束后依然有效;(3)PendingIntent自带Context,而Intent需要在某个Context内运行;(4)Intent在原task中运行,PendingIntent在新的task中运行.
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,Activity2.class), 0));//即将跳转页面,还没跳转 NotificationManager manager=(NotificationManager)getSystemService(this.NOTIFICATION_SERVICE); manager.notify(0,notice); }
setLatestEventInfo表示设置点击该通知的事件
Android发送短信实例
短信系统举例代码 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); }
转自:http://www.dewen.org/q/2342?sort=active
转自:http://www.cnblogs.com/trinea/archive/2012/11/09/PendingIntent%E4%BB%8B%E7%BB%8D.html
转自:http://blog.csdn.net/zeng622peng/article/details/6180190