一、 Pending Intent 概述
PendingIntent在Android開發中是比較重要的一個概念,經常用於啟動一個活動、啟動服務、發送廣播以及創建定時器等操作。PendingIntent實際上是Intent的包裝類,它存儲了需要發送的Intent以及待執行的操作,但是並不會立即觸發執行。相反,PendingIntent會在某個時間點執行任務,這個時間點通常是我們指定的一種條件觸發。總而言之,PendingIntent實現了非同步的延時操作。
二、PendingIntent的構造方法
PendingIntent是由PendingIntent.getActivities(), PendingIntent.getActivity(), PendingIntent.getService(), PendingIntent.getBroadcast()四類靜態方法構造得到,而且這四個方法的參數都十分相似。具體使用根據需要傳遞參數即可。
// 以PendingIntent.getService()為例,第二個參數是request code,第三個參數是一個Intent對象 Intent intent = new Intent(context, MyService.class); PendingIntent operation = PendingIntent.getService(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
三、PendingIntent的flag參數
Pending Intent提供了一些選項以控制Intent在發送時的某些行為。 例如,FLAG_UPDATE_CURRENT標誌允許在PendingIntent中包含更新內容,從而允許可以被更新的正在掛起的Intent的目標組件接收更新。它避免了不必要的故障,因為掛起的Intent可能會過時或已被取消。具體的flag有:
PendingIntent.FLAG_NO_CREATE
PendingIntent.FLAG_ONE_SHOT
PendingIntent.FLAG_CANCEL_CURRENT
PendingIntent.FLAG_UPDATE_CURRENT
//以PendingIntent.FLAG_UPDATE_CURRENT為例 Intent intent = new Intent(context, MyActivity.class); PendingIntent operation = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
四、如何使用PendingIntent實現定時任務
使用AlarmManager和PendingIntent配合,可以實現定時任務。AlarmManager是一種全局的定時器,它可以在一段時間後執行某項任務。
// 以1分鐘後啟動服務為例 Intent intent = new Intent(context, MyService.class); PendingIntent operation = PendingIntent.getService(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 1000 * 60, operation);
五、PendingIntent在通知欄中的使用
通知是Android應用中重要的交互方式。當應用程序處於後台或屏幕關閉時,通知是實現信息提醒的主要方式,而且PendingIntent可以有效地幫助實現這一功能。
NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); NotificationCompat.Builder builder = new NotificationCompat.Builder(context); builder.setSmallIcon(R.drawable.icon).setContentTitle("Notification Title").setContentText("Notification Content") .setAutoCancel(true); Intent intent = new Intent(context, MainActivity.class); PendingIntent pendingIntent = PendingIntent.getActivity(context, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT); builder.setContentIntent(pendingIntent); Notification notification = builder.build(); manager.notify(0, notification);
六、PendingIntent存儲在SQLiteDatabase中
在某些應用場景下,需要把PendingIntent存儲到資料庫中,如:定時任務、鬧鐘提醒等。
ContentValues values = new ContentValues(); values.put("requestCode", requestCode); values.put("intent", intent.toUri(0)); db.insert("pending_intent_table", null, values);
在查詢資料庫時,我們需要從資料庫中讀出數據作為Intent參數,因為我們存儲的是PendingIntent的相關參數而非PendingIntent對象本身。
String uri = cursor.getString(cursor.getColumnIndex("intent")); Intent intent = Intent.parseUri(uri, 0); PendingIntent pi = PendingIntent.getBroadcast(context, requestCode, intent, PendingIntent.FLAG_UPDATE_CURRENT);
七、總結
本文詳細講述了Pending Intent的概念、構造方法、flag參數、定時任務、通知欄中的使用以及存儲在SQLite資料庫中,當我們使用Android進行開發時,PendingIntent是非常重要的一個概念,相信開發者通過這篇文章可以更好地掌握PendingIntent的用法。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/309242.html