Android開發之PendingIntent的使用


PendingIntent,待確定的意圖,等待的意圖

官網鏈接:http://developer.android.com/reference/android/app/PendingIntent.html

 

官網關於該類的繼承關系,PendingIntent繼承自Object。因為該類為final,所以沒有子類,無法被繼承。

要想得到一個PendindIntent對象,需要使用方法類的靜態方法 getActivity(Context, int, Intent, int)getActivities(Context, int, Intent[], int)getBroadcast(Context, int, Intent, int)getService(Context, int, Intent, int)

這幾個靜態方法分別對應着Intent的行為,跳轉到Activity,Activities,Broadcast,Service。

這4個靜態方法的參數都一樣,其中第一個和第三個參數比較的重要,其次是第二個和第四個。第一個參數傳入當前的context,第三個參數傳入intent.

PendingIntent是一個特殊的Intent,主要區別是intent是立馬執行,PendingIntent是待確定的Intent。PendingIntent的操作實際上是傳入的intent的操作。

使用pendingIntent的目的主要是用於所包含的intent執行是否滿足某些條件。

主要的地方:Notification,SmsManager,AlarmManager等

1.Notification例子:

 1 NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
 2 mBuilder.setSmallIcon(R.drawable.notification_icon)
 3 mBuilder.setContentTitle("My notification")
 4 mBuilder.setContentText("Hello World!");
 5 Intent resultIntent = new Intent(this, ResultActivity.class);
 6 PendingIntent resultPendingIntent = PendingIntent.getActivity(this,0,resultIntent,PendingIntent.FLAG_UPDATE_CURRENT);
 7 mBuilder.setContentIntent(resultPendingIntent);
 8 int mNotificationId = 001;
 9 // Gets an instance of the NotificationManager service
10 NotificationManager mNotifyMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
11 // Builds the notification and issues it.
12 mNotifyMgr.notify(mNotificationId, mBuilder.build());

2.SmsManager例子:

1 SmsManager smsManage = SmsManager.getDefault();
2 Intent intent=new Intent("SEND_SMS_ACTION");
3 PendingIntent pendingIntent=PendingIntent.getBroadcast(getApplicationContext(), 0, intent, 0);
4 smsManage.sendTextMessage("13xxxxxxxxx", null, "這是一條短信", pendingIntent, null);

主要常量
FLAG_CANCEL_CURRENT:如果當前系統中已經存在一個相同的PendingIntent對象,那么就將先將已有的PendingIntent取消,然后重新生成一個PendingIntent對象。
FLAG_NO_CREATE:如果當前系統中不存在相同的PendingIntent對象,系統將不會創建該PendingIntent對象而是直接返回null。
FLAG_ONE_SHOT:該PendingIntent只作用一次。在該PendingIntent對象通過send()方法觸發過后,PendingIntent將自動調用cancel()進行銷毀,那么如果你再調用send()方法的話,系統將會返回一個SendIntentException。
FLAG_UPDATE_CURRENT:如果系統中有一個和你描述的PendingIntent對等的PendingInent,那么系統將使用該PendingIntent對象,但是會使用新的Intent來更新之前PendingIntent中的Intent對象數據,例如更新Intent中的Extras。

 

請參考:http://blog.csdn.net/hudashi/article/details/7060837

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM