PendingIntent和Intent

pendingIntent字面意义:等待的,未决定的Intent。
要得到一个pendingIntent对象,使用方法类的静态方法 getActivity(Context, int, Intent, int),getBroadcast(Context, int, Intent, int),
getService(Context, int, Intent, int)  分别对应着Intent的3个行为,跳转到一个activity组件、打开一个广播组件和打开一个服务组件。
参数有4个,比较重要的事第三个和第一个,其次是第四个和第二个。可以看到,要得到这个对象,必须传入一个Intent作为参数,必须有context作为参数。
pendingIntent是一种特殊的Intent。主要的区别在于Intent的执行立刻的,而pendingIntent的执行不是立刻的。pendingIntent执行的操作实质上是参数传进来的Intent的操作,但是使用pendingIntent的目的在于它所包含的Intent的操作的执行是需要满足某些条件的。

主要的使用的地方和例子:通知Notificatio的发送,短消息SmsManager的发送和警报器AlarmManager的执行等等。


使用alarm机制定时启动service,并让service显示Notification的例子

首先创建待定意图

[java]  view plain  copy
  1. mAlarmSender = PendingIntent.getService(AlarmService.this,0new Intent(AlarmService.this, AlarmService_Service.class), 0);  
注册alarm,30秒启动一次

[java]  view plain  copy
  1. AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);  
  2. am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,firstTime, 30*1000, mAlarmSender);  

AlarmService_Service是接收intent的service


service首先显示Notification

[java]  view plain  copy
  1. mNM = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);  
  2. showNotification();  
  3.   
  4. private void showNotification() {  
  5.         CharSequence text = getText(R.string.alarm_service_started);  
  6.         Notification notification = new Notification(R.drawable.stat_sample, text,  
  7.                 System.currentTimeMillis());  
  8.         PendingIntent contentIntent = PendingIntent.getActivity(this0,  
  9.                 new Intent(this, AlarmService.class), 0);  
  10.         notification.setLatestEventInfo(this, getText(R.string.alarm_service_label),  
  11.                        text, contentIntent);  
  12.         mNM.notify(R.string.alarm_service_started, notification);  
  13. }  


Notification的第1个参数,用来表示通知发出时,状态栏显示的的图标

Notification的第2个参数,用来表示通知发出时,状态栏显示的滚动文字

Notification的第3个参数,用来表示通知的时间

notification.setLatestEventInfo的参数2,里面设置的文字对应Sample Alarm Service这句话

notification.setLatestEventInfo的参数3,里面设置的文字对应The alarm service has started running这句话


[java]  view plain  copy
  1. mNM.notify(R.string.alarm_service_started, notification);  
notify的第一个参数表示Notification的唯一ID,cancel通知的时候要用到这个ID


service退出的时候,就会将通知消除

[java]  view plain  copy
  1. public void onDestroy() {  
  2.     // Cancel the notification -- we use the same ID that we had used to start it  
  3.     mNM.cancel(R.string.alarm_service_started);  
  4.   
  5.     // Tell the user we stopped.  
  6.     Toast.makeText(this, R.string.alarm_service_finished, Toast.LENGTH_SHORT).show();  
  7. }  

点击通知的时候,会执行传入的待定意图,启动画面

[java]  view plain  copy
  1.         PendingIntent contentIntent = PendingIntent.getActivity(this0,  
  2.                 new Intent(this, AlarmService.class), 0);  
  3.         notification.setLatestEventInfo(this, getText(R.string.alarm_service_label),  
  4.                        text, contentIntent);  

注意:PendingIntent.getService是用来启动service的待定意图,PendingIntent.getActivity是用来启动Activity的待定意图

service接着启动一个线程,用来暂停15秒

[java]  view plain  copy
  1. Thread thr = new Thread(null, mTask, "AlarmService_Service");  
  2. thr.start();  
在线程中,等待15秒后,会停止service
[java]  view plain  copy
  1. long endTime = System.currentTimeMillis() + 15*1000;  
  2. while (System.currentTimeMillis() < endTime) {  
  3.     synchronized (mBinder) {  
  4.         try {  
  5.             mBinder.wait(endTime - System.currentTimeMillis());  
  6.         } catch (Exception e) {  
  7.         }  
  8.     }  
  9. }  
  10.   
  11. // Done with our work...  stop the service!  
  12. AlarmService_Service.this.stopSelf();  

最后分析下Binder运用。按照我的理解,Binder就像是service提供的一个远程调用的插座。

其他进程可以通过这个插座连接到service上,并调用Binder提供的一些方法。ServiceConnection就像是其他进程的插头,用来取得service提供的Binder。

[java]  view plain  copy
  1. private ServiceConnection mConnection = new ServiceConnection(){  
  2.     public void onServiceConnected(ComponentName className, IBinder service) {  
  3.     // Called when the connection is made.  
  4.     serviceBinder = ((MyService.MyBinder)service).getService();  
  5.     }  
  6.    
  7.     public void onServiceDisconnected(ComponentName className) {  
  8.     // Received when the service unexpectedly disconnects.  
  9.     serviceBinder = null;  
  10.     }  
  11. };  
绑定service,需要指定目的service以及ServiceConnection
[java]  view plain  copy
  1. Intent bindIntent = new Intent(MyActivity.this, MyService.class);  
  2. bindService(bindIntent, mConnection, Context.BIND_AUTO_CREATE);  
当然,本例并没有其他进程连接过来,只是利用binder对象作为同步lock的对象而已

因为没有人调用binder的notify,所以它只能老老实实的等待wait函数超时了


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值