Intent是一個意圖,一個描述了想要啓動一個Activity、Broadcast或是Service的意圖。它主要持有的信息是它想要啓動的組件(Activity、Broadcast或是Service),在開發操做中,須要經過 startActivity , startService 或sendBroadcast 方法來啓動這個意圖執行某些操做!!component
PendingIntent能夠認爲是對Intent的包裝,實際上就是,供當前App或以外的其餘App調用,而常見的是供外部App使用,外部App執行這個 PendingIntent時,間接地調用裏面的Intent,即外部App延時執行PendingIntent中描述的Intent及其最終行爲,PendingIntent主要持有的信息是它所包裝的Intent和當前App Context,即便當前App已經不存在了,也能經過存在於PendingIntent裏的 Context來執行Intent。當你把PendingIntent遞交給別的程序進行處理時,PendingIntent仍然擁有PendingIntent原程序所擁有的權限,當你從系統取得一個PendingIntent時,必定要很是當心才行,好比,一般,若是Intent目的地是你本身的component(Activity/Service/BroadcastReceiver)的話,你最好採用在Intent中顯示指定目的component名字的方式,以確保Intent最終能發到目的,不然Intent最後可能不知道發到哪裏了。對象
能夠這樣理解:當你想在Aactivity中啓動另外一個Bactivity,那麼你能夠選擇兩種狀況[當即啓動或延時啓動]:
1.經過intent配置須要啓動的Bactivity,而後調用startActivity()方法,讓他當即執行啓動操做,跳轉過去
2.另外一種狀況是,你雖然想啓動另外一個Bactivity,但是你並不想立刻跳轉到Bactivity頁面,你想靜等5分鐘以後再跳轉到Bactivity,那麼你能夠經過PendingIntent來實現[固然實現方式有不少啦,這裏僅是想說明PendingIntent與intent的區別],PendingIntent能夠包裝第1步中的intent,而後經過AlarmManager這個定時器,定製5分鐘以後啓PendingIntent,實現這種延時操做,若是你仍是聽着似懂非懂,一頭霧水,我表示頗有壓力了,我該怎麼說你才能清楚呢,理論終究是抽象的,後見將會經過一個程序說明一下,程序中是啓動一個BroadcastReceiver,其實原理都是同樣的!!進程
如何得到一個PendingIntent呢?其實很簡單:
1.你能夠經過getActivity(Context context, int requestCode, Intent intent, int flags)系列方法從系統開發
取得一個用於啓動一個Activity的PendingIntent對象
2.能夠經過getService(Context context, int requestCode, Intent intent, int flags)方法從系統取得一個get
用於啓動一個Service的PendingIntent對象
3.能夠經過getBroadcast(Context context, int requestCode, Intent intent, int flags)方法從系統取得一it
個用於向BroadcastReceiver的發送廣播的PendingIntent對象ast
PendingIntent幾個常量:原理
1.FLAG_CANCEL_CURRENT :若是AlarmManager管理的PendingIntent已經存在,那麼將會取消當前的配置
PendingIntent,從而建立一個新的PendingIntent
2.FLAG_UPDATE_CURRENT:若是AlarmManager管理的PendingIntent已經存在,可讓新的Intent更新以前權限
PendingIntent中的Intent對象數據,例如更新Intent中的Extras,另外,咱們也能夠在PendingIntent的原進程
中調用PendingIntent的cancel ()把其從系統中移除掉
3.FLAG_NO_CREATE :若是AlarmManager管理的PendingIntent已經存在,那麼將不進行任何操做,直接返回已經
存在的PendingIntent,若是PendingIntent不存在了,那麼返回null