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一個包裝。
- 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));//即將跳轉頁面,還沒跳轉 //PendingIntent.getActivity(this, 0, new Intent(this,Activity2.class) 獲取 PendingIntent對象
- NotificationManager manager=(NotificationManager)getSystemService(this.NOTIFICATION_SERVICE);
- manager.notify(0,notice); //使用NotificationManager的時候 再去調用PendingIntent對象進行跳轉
- }
1. GSM網絡中android發送短信示例
- String msg ="你好,美女";
- String number = "135****6784";
- SmsManager sms = SmsManager.getDefault();
- PendingIntent pi = PendingIntent.getBroadcast(SmsActivity.this,0,new Intent(...),0);
- //new Intent(...) e.g: PendingIntent pIntent = PendingIntent.getBroadcast(this, 0, new //// Intent("com.homer.pendingintent.pendingbroadcast"), 0);
- sms.sendTextMessage(number, null, msg, pi, null);
- Toast.makeText(SmsActivity.this,"發送成功",Toast.LENGHT_LONG).show();
完整例子,可以參考: http://download.csdn.net/detail/sunboy_2050/4917350#comment
代碼解釋
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在進行被賦予的相關的操作。
-------------------------------------------------------------------------------------------------------
本文翻譯整理自:http://developer.android.com/reference/android/app/PendingIntent.html
public static PendingIntent getActivity (Context context, int requestCode, Intent intent, int flags)
Retrieve a PendingIntent that will start a new activity, like calling Context.startActivity(Intent)
.
Note that the activity will be started outside of the context of an existing activity, so you must use the Intent.FLAG_ACTIVITY_NEW_TASK
launch
flag in the Intent.
因爲對於Context的startActivity方法,如果不是在其子類(Activity)中調用,那麼必須對Intent加上FLAG_ACTIVITY_NEW_TASK。
Parameters
context | The Context in which this PendingIntent should start the activity. |
---|---|
requestCode | Private request code for the sender (currently not used). |
intent | Intent of the activity to be launched. |
flags | May be FLAG_ONE_SHOT , FLAG_NO_CREATE , FLAG_CANCEL_CURRENT , FLAG_UPDATE_CURRENT ,
or any of the flags as supported byIntent.fillIn() to
control which unspecified parts of the intent that can be supplied when the actual send happens.當我們使用Intent.fillIn()方法時,表示其Intent的某個數據項可以被send方法的Inent參數進行覆蓋重寫。 |
FLAG_NO_CREATE
has
been supplied.public static PendingIntent getActivities (Context context, int requestCode, Intent[] intents, int flags)
Like getActivity(Context,
int, Intent, int)
, but allows an array of Intents to be supplied. The first Intent in the array is taken as the primary key for the PendingIntent, like the single Intent given to getActivity(Context,
int, Intent, int)
. Upon sending the resulting PendingIntent, all of the Intents are started in the same way as they would be by passing them to startActivities(Intent[])
.
The first intent in the array will be started outside of the context of an existing activity, so you must use the Intent.FLAG_ACTIVITY_NEW_TASK
launch
flag in the Intent. (Activities after the first in the array are started in the context of the previous activity in the array, so FLAG_ACTIVITY_NEW_TASK is not needed nor desired for them.)
The last intent in the array represents the key for the PendingIntent. In other words, it is the significant element for matching (as done with the single intent given togetActivity(Context,
int, Intent, int)
, its content will be the subject of replacement by send(Context,
int, Intent)
andFLAG_UPDATE_CURRENT
,
etc. This is because it is the most specific of the supplied intents, and the UI the user actually sees when the intents are started.
Parameters
context | The Context in which this PendingIntent should start the activity. |
---|---|
requestCode | Private request code for the sender (currently not used). |
intents | Array of Intents of the activities to be launched. |
flags | May be FLAG_ONE_SHOT , FLAG_NO_CREATE , FLAG_CANCEL_CURRENT , FLAG_UPDATE_CURRENT ,
or any of the flags as supported byIntent.fillIn() to
control which unspecified parts of the intent that can be supplied when the actual send happens.當我們使用Inent.fillIn()所支持的flags時,表示其Intent的數據項可以被send方法的Intent參數覆蓋重寫。 |
Returns
- Returns an existing or new PendingIntent matching the given parameters. May return null only if
FLAG_NO_CREATE
has been supplied.
public static PendingIntent getService (Context context, int requestCode, Intent intent, int flags)
Retrieve a PendingIntent that will start a service, like calling Context.startService()
.
The start arguments given to the service will come from the extras of the Intent.
Parameters
context | The Context in which this PendingIntent should start the service. |
---|---|
requestCode | Private request code for the sender (currently not used). |
intent | An Intent describing the service to be started. |
flags | May be FLAG_ONE_SHOT , FLAG_NO_CREATE , FLAG_CANCEL_CURRENT , FLAG_UPDATE_CURRENT ,
or any of the flags as supported byIntent.fillIn() to
control which unspecified parts of the intent that can be supplied when the actual send happens.當我們使用Intent.fillIn()方法的flags時,它表示其Intent的數據項可以被send方法的Intent參數覆蓋重寫。 |
Returns
- Returns an existing or new PendingIntent matching the given parameters. May return null only if
FLAG_NO_CREATE
has been supplied.
public static PendingIntent getBroadcast (Context context, int requestCode, Intent intent, int flags)
Retrieve a PendingIntent that will perform a broadcast, like calling Context.sendBroadcast()
.
Parameters
context | The Context in which this PendingIntent should perform the broadcast. |
---|---|
requestCode | Private request code for the sender (currently not used). |
intent | The Intent to be broadcast. |
flags | May be FLAG_ONE_SHOT , FLAG_NO_CREATE , FLAG_CANCEL_CURRENT , FLAG_UPDATE_CURRENT ,
or any of the flags as supported byIntent.fillIn() to
control which unspecified parts of the intent that can be supplied when the actual send happens.當我們使用Intent.fillIn()方法的flags時,它表示其Intent的數據項可以被send方法的Intent參數覆蓋重寫。 |
Returns
- Returns an existing or new PendingIntent matching the given parameters. May return null only if
FLAG_NO_CREATE
has been supplied.
public void send ()
Perform the operation associated with this PendingIntent.
Throws
PendingIntent.CanceledException | Throws CanceledException if the PendingIntent is no longer allowing more intents to be sent through it. |
---|
public void send (Context context, int code, Intent intent)
Perform the operation associated with this PendingIntent, allowing the caller to specify information about the Intent to use.
Parameters
context | The Context of the caller. 該參數是因爲intent參數才需要提供的,所用如果你的intent參數不爲null的話,該參數也不能爲null. |
---|---|
code | Result code to supply back to the PendingIntent's target. |
intent | Additional Intent data. See Intent.fillIn() for
information on how this is applied to the original Intent. |
Throws
PendingIntent.CanceledException | Throws CanceledException if the PendingIntent is no longer allowing more intents to be sent through it. |
---|
public void send (int code, PendingIntent.OnFinished onFinished, Handler handler)
Perform the operation associated with this PendingIntent, allowing the caller to be notified when the send has completed.
Parameters
code | Result code to supply back to the PendingIntent's target. |
---|---|
onFinished | The object to call back on when the send has completed, or null for no callback. 通過該參數,我們可以設置在Intent發送成功後的回調函數。 |
handler | Handler identifying the thread on which the callback should happen. If null, the callback will happen from the thread pool of the process. 用於說明onFinished參數指定的回調函數,最終在哪個Handler中進行調用。 |
Throws
PendingIntent.CanceledException | Throws CanceledException if the PendingIntent is no longer allowing more intents to be sent through it. |
---|
public void send (Context context, int code, Intent intent, PendingIntent.OnFinished onFinished, Handler handler)
Perform the operation associated with this PendingIntent, allowing the caller to specify information about the Intent to use and be notified when the send has completed.
For the intent parameter, a PendingIntent often has restrictions on which fields can be supplied here, based on how the PendingIntent was retrieved ingetActivity(Context,
int, Intent, int)
, getBroadcast(Context,
int, Intent, int)
, or getService(Context,
int, Intent, int)
.
Parameters
context | The Context of the caller. This may be null if intent is also null. 該參數是因爲intent參數才需要提供的,一般是當前的context,如果你的intent參數不爲null的話,該函數也不能爲null. |
---|---|
code | Result code to supply back to the PendingIntent's target. |
intent | Additional Intent data. See Intent.fillIn() for
information on how this is applied to the original Intent. Use null to not modify the original Intent. |
onFinished | The object to call back on when the send has completed, or null for no callback. 通過該參數,我們可以指定Intent發送成功後的回調函數。 |
handler | Handler identifying the thread on which the callback should happen. If null, the callback will happen from the thread pool of the process. 該參數說明onFinished參數指定的回調函數將在哪個Handler中進行調用。 |
Throws
PendingIntent.CanceledException | Throws CanceledException if the PendingIntent is no longer allowing more intents to be sent through it. |
---|
public void cancel ()
Cancel a currently active PendingIntent. Only the original application owning an PendingIntent can cancel it.
只有PengdingIntent的原應用程序才能調用cancel()來把它從系統中移除掉。
public String getTargetPackage ()
Return the package name of the application that created this PendingIntent, that is the identity under which you will actually be sending the Intent. The returned string is supplied by the system, so that an application can not spoof its package.
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
pendingIntent簡單理解
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的執行等等。
Android的狀態欄通知(Notification)
如果需要查看消息,可以拖動狀態欄到屏幕下方即可查看消息。
步驟:
1 獲取通知管理器NotificationManager,它也是一個系統服務
2 建立通知Notification notification = new Notification(icon, null, when);
3 爲新通知設置參數(比如聲音,震動,燈光閃爍)
4 把新通知添加到通知管理器
發送消息的代碼如下:
//獲取通知管理器
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE)
int icon = android.R.drawable.stat_notify_chat;
long when = System.currentTimeMillis();//通知發生的時間爲系統當前時間
//新建一個通知,指定其圖標和標題
Notification notification = new Notification(icon, null, when);//第一個參數爲圖標,第二個參數爲短暫提示標題,第三個爲通知時間
notification.defaults = Notification.DEFAULT_SOUND;//發出默認聲音
notification.flags |= Notification.FLAG_AUTO_CANCEL;//點擊通知後自動清除通知
Intent openintent = new Intent(this, OtherActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, openintent, 0);//當點擊消息時就會向系統發送openintent意圖
notification.setLatestEventInfo(this, “標題”, “我是內容", contentIntent);
mNotificationManager.notify(0, notification);//第一個參數爲自定義的通知唯一標識
重點是setLatestEventInfo( )方法的最後一個參數!!!!它是一個PendingIntent!!!!!!!!!
這裏使用到了PendingIntent(pend本意是待定,不確定的意思)
PendingIntent可以看作是對Intent的包裝。PendingIntent主要持有的信息是它所包裝的Intent和當前Application的Context。正由於PendingIntent中保存有當前Application的Context,使它賦予帶他程序一種執行的Intent的能力,就算在執行時當前Application已經不存在了,也能通過存在PendingIntent裏的Context照樣執行Intent。
PendingIntent的一個很好的例子:
SmsManager的用於發送短信的方法:
sendTextMessage(destinationAddress, scAddress, text, sentIntent, deliveryIntent);
第一個參數:destinationAddress 對方手機號碼
第二個參數:scAddress 短信中心號碼 一般設置爲空
第三個參數:text 短信內容
第四個參數:sentIntent判斷短信是否發送成功,如果你沒有SIM卡,或者網絡中斷,則可以通過這個itent來判斷。注意強調的是“發送”的動作是否成功。那麼至於對於對方是否收到,另當別論
第五個參數:deliveryIntent當短信發送到收件人時,會收到這個deliveryIntent。即強調了“發送”後的結果
就是說是在"短信發送成功"和"對方收到此短信"纔會激活 sentIntent和deliveryIntent這兩個Intent。這也相當於是延遲執行了Intent
上面兩個例子可以理解,PendingIntent就是一個可以在滿足一定條件下執行的Intent,它相比於Intent的優勢在於自己攜帶有Context對象,這樣他就不必依賴於某個activity纔可以存在。