android.app.PendingIntent
2015-07-26 13:02
585 查看
A description of an Intent and target action to perform with it. Instances of this class are created with
By giving a PendingIntent to another application, you are granting it the right to perform the operation you have specified as if the other application was yourself (with the same permissions and identity). As such, you should be careful about how you build the PendingIntent: almost always, for example, the base Intent you supply should have the component name explicitly(明确的) set to one of your own components, to ensure it is ultimately sent there and nowhere else.
A PendingIntent itself is simply a reference to a token maintained by the system describing the original data used to retrieve it. This means that, even if its owning application's process is killed, the PendingIntent itself will remain usable from other processes that have been given it. If the creating application later re-retrieves the same kind of PendingIntent (same operation, same Intent action, data, categories, and components, and same flags), it will receive a PendingIntent representing the same token if that is still valid, and can thus call
Because of this behavior, it is important to know when two Intents are considered to be the same for purposes of retrieving a PendingIntent. A common mistake people make is to create multiple PendingIntent objects with Intents that only vary in their "extra" contents, expecting to get a different PendingIntent each time. This does not happen. The parts of the Intent that are used for matching are the same ones defined by
There are two typical ways to deal with this.
If you truly need multiple distinct PendingIntent objects active at the same time (such as to use as two notifications that are both shown at the same time), then you will need to ensure there is something that is different about them to associate them with different PendingIntents. This may be any of the Intent attributes considered by
If you only need one PendingIntent active at a time for any of the Intents you will use, then you can alternatively use the flags
intent英文意思是意图,pending表示即将发生或来临的事情。
PendingIntent这个类用于处理即将发生的事情。比如在通知Notification中用于跳转页面,但不是马上跳转。
Intent 是及时启动,intent 随所在的activity 消失而消失。
PendingIntent 可以看作是对intent的包装,通常通过getActivity,getBroadcast ,getService来得到pendingintent的实例,当前activity并不能马上启动它所包含的intent,而是在外部执行 pendingintent时,调用intent的。正由于pendingintent中 保存有当前App的Context,使它赋予外部App一种能力,使得外部App可以如同当前App一样的执行pendingintent里的 Intent, 就算在执行时当前App已经不存在了,也能通过存在pendingintent里的Context照样执行Intent。另外还可以处理intent执行后的操作。常和alermanger 和notificationmanager一起使用。
Intent一般是用作Activity、Sercvice、BroadcastReceiver之间传递数据,而Pendingintent,一般用在 Notification上,可以理解为延迟执行的intent,PendingIntent是对Intent一个包装。
Java代码
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);
}
[java] view plaincopy
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);
}
1. GSM网络中android发送短信示例Java代码
String msg ="你好,美女";
String number = "135****6784";
SmsManager sms = SmsManager.getDefault();
PendingIntent pi = PendingIntent.getBroadcast(SmsActivity.this,0,new Intent(...),0);
sms.sendTextMessage(number, null, msg, pi, null);
Toast.makeText(SmsActivity.this,"发送成功",Toast.LENGHT_LONG).show();
[java] view plaincopy
String msg ="你好,美女";
String number = "135****6784";
SmsManager sms = SmsManager.getDefault();
PendingIntent pi = PendingIntent.getBroadcast(SmsActivity.this,0,new Intent(...),0);
sms.sendTextMessage(number, null, msg, pi, null);
Toast.makeText(SmsActivity.this,"发送成功",Toast.LENGHT_LONG).show();
代码解释
PendingIntent就是一个Intent的描述,我们可以把这个描述交给别的程序,别的程序根据这个描述在后面的别的时间做你安排做的事情 (By giving a PendingIntent to another application, you are granting it the right to perform the operation you have specified as if the other application was yourself,就相当于PendingIntent代表了Intent)。本例中别的程序就是发送短信的程序,短信发送成功后要把intent广播出去 。
函数SmsManager.sendTextMessage(String destinationAddress, String scAddress, String text, PendingIntent sentIntent, PendingIntent deliveryIntent)中参数解释:
1)PendingIntent sentIntent:当短信发出时,成功的话sendIntent会把其内部的描述的intent广播出去,否则产生错误代码并通过android.app.PendingIntent.OnFinished进行回调,这个参数最好不为空,否则会存在资源浪费的潜在问题;
2)PendingIntent deliveryIntent:是当消息已经传递给收信人后所进行的PendingIntent广播。
查看PendingIntent 类可以看到许多的Send函数,就是PendingIntent在进行被赋予的相关的操作。
getActivity,
getActivities,
getBroadcast, and
getService; the returned object can be handed to other applications so that they can perform the action you described on your behalf at a later time.
By giving a PendingIntent to another application, you are granting it the right to perform the operation you have specified as if the other application was yourself (with the same permissions and identity). As such, you should be careful about how you build the PendingIntent: almost always, for example, the base Intent you supply should have the component name explicitly(明确的) set to one of your own components, to ensure it is ultimately sent there and nowhere else.
A PendingIntent itself is simply a reference to a token maintained by the system describing the original data used to retrieve it. This means that, even if its owning application's process is killed, the PendingIntent itself will remain usable from other processes that have been given it. If the creating application later re-retrieves the same kind of PendingIntent (same operation, same Intent action, data, categories, and components, and same flags), it will receive a PendingIntent representing the same token if that is still valid, and can thus call
cancelto remove it.
Because of this behavior, it is important to know when two Intents are considered to be the same for purposes of retrieving a PendingIntent. A common mistake people make is to create multiple PendingIntent objects with Intents that only vary in their "extra" contents, expecting to get a different PendingIntent each time. This does not happen. The parts of the Intent that are used for matching are the same ones defined by
Intent.filterEquals. If you use two Intent objects that are equivalent as per
Intent.filterEquals, then you will get the same PendingIntent for both of them.
There are two typical ways to deal with this.
If you truly need multiple distinct PendingIntent objects active at the same time (such as to use as two notifications that are both shown at the same time), then you will need to ensure there is something that is different about them to associate them with different PendingIntents. This may be any of the Intent attributes considered by
Intent.filterEquals, or different request code integers supplied to
getActivity,
getActivities,
getBroadcast, or
getService.
If you only need one PendingIntent active at a time for any of the Intents you will use, then you can alternatively use the flags
FLAG_CANCEL_CURRENTor
FLAG_UPDATE_CURRENTto either cancel or modify whatever current PendingIntent is associated with the Intent you are supplying.
intent英文意思是意图,pending表示即将发生或来临的事情。
PendingIntent这个类用于处理即将发生的事情。比如在通知Notification中用于跳转页面,但不是马上跳转。
Intent 是及时启动,intent 随所在的activity 消失而消失。
PendingIntent 可以看作是对intent的包装,通常通过getActivity,getBroadcast ,getService来得到pendingintent的实例,当前activity并不能马上启动它所包含的intent,而是在外部执行 pendingintent时,调用intent的。正由于pendingintent中 保存有当前App的Context,使它赋予外部App一种能力,使得外部App可以如同当前App一样的执行pendingintent里的 Intent, 就算在执行时当前App已经不存在了,也能通过存在pendingintent里的Context照样执行Intent。另外还可以处理intent执行后的操作。常和alermanger 和notificationmanager一起使用。
Intent一般是用作Activity、Sercvice、BroadcastReceiver之间传递数据,而Pendingintent,一般用在 Notification上,可以理解为延迟执行的intent,PendingIntent是对Intent一个包装。
Java代码
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);
}
[java] view plaincopy
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);
}
1. GSM网络中android发送短信示例Java代码
String msg ="你好,美女";
String number = "135****6784";
SmsManager sms = SmsManager.getDefault();
PendingIntent pi = PendingIntent.getBroadcast(SmsActivity.this,0,new Intent(...),0);
sms.sendTextMessage(number, null, msg, pi, null);
Toast.makeText(SmsActivity.this,"发送成功",Toast.LENGHT_LONG).show();
[java] view plaincopy
String msg ="你好,美女";
String number = "135****6784";
SmsManager sms = SmsManager.getDefault();
PendingIntent pi = PendingIntent.getBroadcast(SmsActivity.this,0,new Intent(...),0);
sms.sendTextMessage(number, null, msg, pi, null);
Toast.makeText(SmsActivity.this,"发送成功",Toast.LENGHT_LONG).show();
代码解释
PendingIntent就是一个Intent的描述,我们可以把这个描述交给别的程序,别的程序根据这个描述在后面的别的时间做你安排做的事情 (By giving a PendingIntent to another application, you are granting it the right to perform the operation you have specified as if the other application was yourself,就相当于PendingIntent代表了Intent)。本例中别的程序就是发送短信的程序,短信发送成功后要把intent广播出去 。
函数SmsManager.sendTextMessage(String destinationAddress, String scAddress, String text, PendingIntent sentIntent, PendingIntent deliveryIntent)中参数解释:
1)PendingIntent sentIntent:当短信发出时,成功的话sendIntent会把其内部的描述的intent广播出去,否则产生错误代码并通过android.app.PendingIntent.OnFinished进行回调,这个参数最好不为空,否则会存在资源浪费的潜在问题;
2)PendingIntent deliveryIntent:是当消息已经传递给收信人后所进行的PendingIntent广播。
查看PendingIntent 类可以看到许多的Send函数,就是PendingIntent在进行被赋予的相关的操作。
相关文章推荐
- pendingIntent简单理解
- 闹钟的设计(ClockDemo)
- pendingIntent
- PendingIntent
- Android中的PendingIntent使用注意事项
- pendingIntent理解
- Android 中的notify 机制
- Android PendingIntent.getBroadcast intent数据不更新问题
- Intent和PendingIntent的区别
- PendingIntent的理解
- android发短信操作
- AN开发范例实战宝典学习日记二: 短息发送的广播
- android pengingintent
- 手机屏幕上方通知
- Android入门之通知栏
- Android 实现定时闹铃功能
- Android中pendingIntent的深入理解
- Intent和PendingIntent的区别
- Android--Alarm,定时闹钟
- Android--桌面组件AppWidget