天天看點

PendingIntent和Intent的差別

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一個包裝。

PendingIntent這個類封裝了一個Intent(意圖),它表示即将發送的動作的類型。 這個類的方法主要有兩類,一類是getXXX,表示要激活的意圖的類型,如activity,service或者是BroadcastReceiver等。另一類是send,表示立即發送這個意圖。 

下面是常用的API:  

view source print ?

1.

public

static

PendingIntent getActivity (Context context, 

int

requestCode, Intent intent, 

int

flags, Bundle options)

2.

public

static

PendingIntent getBroadcast (Context context, 

int

requestCode, Intent intent, 

int

flags)

3.

public

static

PendingIntent getService (Context context, 

int

requestCode, Intent intent, 

int

flags)

4.

public

void

send ()

示例:  activity不貼了。  1.PendingIntent單獨使用.   下面是activity中的一個button的點選事件所調用的方法.這個方法用于發送一個廣播 view source print ?

01.

05.

public

void

pendingIntentFunc()

06.

{

07.

Intent intent = 

new

Intent(

"com.xxx"

);

08.

intent.putExtra(

"info"

,

"我是info"

);

09.

//其實就跟調用sendBroadcast方法一樣

10.

PendingIntent pi = PendingIntent.getBroadcast(

this

, intent, PendingIntent.FLAG_UPDATE_CURRENT);

11.

try

12.

{

13.

pi.send();

//執行PendingIntent中的意圖

14.

catch

(CanceledException e)

15.

{

16.

e.printStackTrace();

17.

}

18.

}

定義一個BroadcastReceiver用于接收廣播: view source print ?

01.

package

com.example.receiver;

02.

import

android.content.BroadcastReceiver;

03.

import

android.content.Context;

04.

import

android.content.Intent;

05.

import

android.util.Log;

06.

import

android.widget.Toast;

07.

public

class

MyReceiver 

extends

BroadcastReceiver

08.

{

09.

private

static

final

String TAG = 

"MyReceiver"

;

10.

@Override

11.

public

void

onReceive(Context context, Intent intent)

12.

{

13.

Log.i(TAG,

"成功收到廣播..."

);

14.

Toast.makeText(context,

"info:"

+intent.getStringExtra(

"info"

),

).show();

15.

}

16.

}

清單檔案中配置receiver:

view source print ?

1.

<receiver android:name=

"com.example.receiver.MyReceiver"

>

2.

<intent-filter >

3.

<action android:name=

"com.xxx"

/>

4.

</intent-filter>

5.

</receiver>

當點選按鈕時,将會發出廣播,而且還能接收到intent傳來的資訊。 logcat列印如下日志: 

PendingIntent和Intent的差別

2.PendingIntent配合Notification使用   下面也是activity中的一個按鈕的點選事件觸發所執行的函數 view source print ?

01.

04.

public

void

sendNotification2()

05.

{

06.

NotificationManager manager = (NotificationManager) 

this

.getSystemService(Context.NOTIFICATION_SERVICE);

07.

//        notification的初始化

08.

Notification notification = 

new

Notification();

09.

notification.icon = R.drawable.ic_launcher;

10.

notification.when = System.currentTimeMillis();

11.

notification.tickerText = 

"又來一條新通知"

;

12.

notification.flags = notification.FLAG_AUTO_CANCEL;

13.

//        包裝一個PendingIntent,當使用者點選通知觸發一個意圖

14.

Intent intent = 

new

Intent(

this

,MyService.

class

);

15.

//        點選通知将啟動服務,相當于調用了startService方法

16.

PendingIntent contentIntent = PendingIntent.getService(

this

,intent, PendingIntent.FLAG_UPDATE_CURRENT);

17.

notification.setLatestEventInfo(

this

,

"标題"

,

"點我激活一個服務"

, contentIntent);

18.

//        激活通知

19.

manager.notify(

2

,notification);

//第一個參數代表的是通知的id

20.

}

定義一個服務:

view source print ?

01.

package

com.example.service;

02.

03.

import

android.app.Service;

04.

import

android.content.Intent;

05.

import

android.os.IBinder;

06.

import

android.util.Log;

07.

08.

public

class

MyService 

extends

Service

09.

{

10.

private

static

final

String TAG = 

"MyService"

;

11.

12.

@Override

13.

public

IBinder onBind(Intent intent)

14.

{

15.

return

null

;

16.

}

17.

18.

@Override

19.

public

void

onCreate()

20.

{

21.

Log.i(TAG,

"服務成功啟動..."

);

22.

}

23.

}

清單檔案中配置服務:

view source print ?

1.

<service android:name=

"com.example.service.MyService"

></service>

當點選工作列通知即開啟服務:

PendingIntent和Intent的差別

logcat成功列印日志,服務啟動成功:  

PendingIntent和Intent的差別

3.PendingIntent配合SmsManger使用  下面使用SmsManager發送一條短信。 view source print ?

1.

public

void

sendSMS()

2.

{

3.

SmsManager sm = SmsManager.getDefault();

4.

PendingIntent sentIntent = PendingIntent.getBroadcast(

this

,

new

Intent(

"com.xxx"

),PendingIntent.FLAG_UPDATE_CURRENT);

5.

sm.sendTextMessage(

"5556"

null

"hello world"

, sentIntent, 

null

);

6.

}

PendingIntent作為senTextMessage的參數被傳遞進來,作為短信發送成功的回執,此時PendingIntent會發送一個廣播。還是以上面例子的廣播接收者接收廣播,可以看到logcat列印了日志:

PendingIntent和Intent的差別

另外5556也收到了短信:

PendingIntent和Intent的差別

上面基本把PendingIntent的使用場景都介紹了,api也使用了一些,其他api大家可以查文檔。