PendingIntent详解.doc_第1页
PendingIntent详解.doc_第2页
PendingIntent详解.doc_第3页
PendingIntent详解.doc_第4页
PendingIntent详解.doc_第5页
全文预览已结束

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

PendingIntent详解一. 基本用法public final class PendingIntent extends Objectimplements Parcelable PendingIntent是一个特殊的Intent, 是对Intent的包装,它持有context和intent.可以理解为一个延时执行的Intent.它只有在满足特定条件才会执行. 主要的使用的地方和例子:通知Notificatio的发送,短消息SmsManager的发送和警报器AlarmManager的执行等等。PendingIntent就是一个可以在满足一定条件下执行的Intent,它相比于Intent的优势在于自己携带有Context对象,这样他就不必依赖于某个activity才可以存在。PendingIntent一般作为参数传给某个实例,在该实例完成某个操作后自动执行PendingIntent上的Action,也可以通过PendingIntent的send函数手动执行,并可以在send函数中设置OnFinished表示send成功后执行的动作。PendingIntent用于描述Intent及其最终的行为. 你可以通过getActivity(Context context, int requestCode, Intent intent, int flags)系列方法从系统取得一个用于启动一个Activity的PendingIntent对象, 可以通过getService(Context context, int requestCode, Intent intent, int flags)方法从系统取得一个用于启动一个Service的PendingIntent对象 可以通过getBroadcast(Context context, int requestCode, Intent intent, int flags)方法从系统取得一个用于向BroadcastReceiver的Intent广播的PendingIntent对象cancel()函数public voidcancel()Since:API Level 1Cancel a currently active PendingIntent. Only the original application owning an PendingIntent can cancel it.只有PengdingIntent的原应用程序才能调用cancel()来把它从系统中移除掉。 返回的PendingIntent可以递交给别的应用程序,然后继续处理。这里的话你可以稍后才处理PendingIntent中描述的Intent及其最终行为。注意:两个PendingIntent对等是指它们的operation一样, 且其它们的Intent的action, data, categories,components和flags都一样。但是它们的Intent的Extra可以不一样。主要常量l FLAG_CANCEL_CURRENT:如果当前系统中已经存在一个相同的PendingIntent对象,那么就将先将已有的PendingIntent取消,然后重新生成一个PendingIntent对象。l FLAG_NO_CREATE:如果当前系统中不存在相同的PendingIntent对象,系统将不会创建该PendingIntent对象而是直接返回null。l FLAG_ONE_SHOT:该PendingIntent只作用一次。在该PendingIntent对象通过send()方法触发过后,PendingIntent将自动调用cancel()进行销毁,那么如果你再调用send()方法的话,系统将会返回一个SendIntentException。l FLAG_UPDATE_CURRENT:如果系统中有一个和你描述的PendingIntent对等的PendingInent,那么系统将使用该PendingIntent对象,但是会使用新的Intent来更新之前PendingIntent中的Intent对象数据,例如更新Intent中的Extras。二. Intent和PendingIntent的区别l Intent是立即使用的,而PendingIntent可以等到事件发生后触发,PendingIntent可以cancell Intent在程序结束后即终止,而PendingIntent在程序结束后依然有效l PendingIntent自带Context,而Intent需要在某个Context内运行l Intent在原task中运行,PendingIntent在新的task中运行三 PendingIntent举例a. 系统通知栏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);b. 短信系统举例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

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论