用户工具

站点工具


开发:android:pendingintent

PendingIntent简介

根据字面意思就知道是延迟的Intent,主要用来在某个事件完成后执行特定的Action。PendingIntent包含了Intent及Context,所以就算Intent所属程序结束,PendingIntent依然有效,可以在其他程序中使用。

PendingIntent一般作为参数传给某个实例,在该实例完成某个操作后自动执行PendingIntent上的Action,也可以通过PendingIntent的send函数手动执行,并可以在send函数中设置OnFinished表示send成功后执行的动作。

由于PendingIntent中,保存了当前App的context,使它赋予外部App一种能力,使得外部App可以如同当前App一样执行PendingIntent里的Intent,就算执行时当前的App已经不存在了也能通过存在PendingIntent的Context照样执行Intent。另外还可以处理Intent执行后的操作经常和AlarmManager 和NotificationManager一起使用。

和Intent区别

  1. Intent是立即使用的,而PendingIntent可以等到事件发生后触发,PendingIntent可以cancel
  2. Intent在程序结束后即终止,而PendingIntent在程序结束后依然有效
  3. PendingIntent自带Context,而Intent需要在某个Context内运行
  4. Intent在原task中运行,PendingIntent在新的task中运行

三个静态方法

  1. 通过getActivity(Context context, int requestCode, Intent intent, int flags)系列方法从系统取得一个用于启动一个Activity的PendingIntent对象,
  2. 通过getService(Context context, int requestCode, Intent intent, int flags)方法从系统取得一个用于启动一个Service的PendingIntent对象
  3. 通过getBroadcast(Context context, int requestCode, Intent intent, int flags)方法从系统取得一个用于向BroadcastReceiver的Intent广播的PendingIntent对象

示例

通知栏

NotificationManager nm = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
int icon = android.R.drawable.stat_notify_chat;
long when = System.currentTimeMillis() + 2000;
Notification n = new Notification(icon, "通知栏demo提醒", when);
n.defaults = Notification.DEFAULT_SOUND;
n.flags |= Notification.FLAG_AUTO_CANCEL;
Intent openintent = new Intent(this, DemoList.class);
PendingIntent pi = PendingIntent.getActivity(this, 0, openintent, PendingIntent.FLAG_CANCEL_CURRENT);
n.setLatestEventInfo(this, "通知栏demo提醒title", "通知栏demo提醒text", pi);
nm.notify(0, n);

短信

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);
}

链接

开发/android/pendingintent.txt · 最后更改: 2015-01-12 16:32 由 danding

粤ICP备16007019号-3