深入理解PendingIntent的用法

一、 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

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
小藍的頭像小藍
上一篇 2025-01-04 19:30
下一篇 2025-01-04 19:30

相關推薦

  • 深入解析Vue3 defineExpose

    Vue 3在開發過程中引入了新的API `defineExpose`。在以前的版本中,我們經常使用 `$attrs` 和` $listeners` 實現父組件與子組件之間的通信,但…

    編程 2025-04-25
  • 深入理解byte轉int

    一、位元組與比特 在討論byte轉int之前,我們需要了解位元組和比特的概念。位元組是計算機存儲單位的一種,通常表示8個比特(bit),即1位元組=8比特。比特是計算機中最小的數據單位,是…

    編程 2025-04-25
  • 深入理解Flutter StreamBuilder

    一、什麼是Flutter StreamBuilder? Flutter StreamBuilder是Flutter框架中的一個內置小部件,它可以監測數據流(Stream)中數據的變…

    編程 2025-04-25
  • 深入探討OpenCV版本

    OpenCV是一個用於計算機視覺應用程序的開源庫。它是由英特爾公司創建的,現已由Willow Garage管理。OpenCV旨在提供一個易於使用的計算機視覺和機器學習基礎架構,以實…

    編程 2025-04-25
  • 深入了解scala-maven-plugin

    一、簡介 Scala-maven-plugin 是一個創造和管理 Scala 項目的maven插件,它可以自動生成基本項目結構、依賴配置、Scala文件等。使用它可以使我們專註於代…

    編程 2025-04-25
  • 深入了解LaTeX的腳註(latexfootnote)

    一、基本介紹 LaTeX作為一種排版軟體,具有各種各樣的功能,其中腳註(footnote)是一個十分重要的功能之一。在LaTeX中,腳註是用命令latexfootnote來實現的。…

    編程 2025-04-25
  • 深入剖析MapStruct未生成實現類問題

    一、MapStruct簡介 MapStruct是一個Java bean映射器,它通過註解和代碼生成來在Java bean之間轉換成本類代碼,實現類型安全,簡單而不失靈活。 作為一個…

    編程 2025-04-25
  • 深入理解Python字元串r

    一、r字元串的基本概念 r字元串(raw字元串)是指在Python中,以字母r為前綴的字元串。r字元串中的反斜杠(\)不會被轉義,而是被當作普通字元處理,這使得r字元串可以非常方便…

    編程 2025-04-25
  • 深入了解Python包

    一、包的概念 Python中一個程序就是一個模塊,而一個模塊可以引入另一個模塊,這樣就形成了包。包就是有多個模塊組成的一個大模塊,也可以看做是一個文件夾。包可以有效地組織代碼和數據…

    編程 2025-04-25
  • 深入探討馮諾依曼原理

    一、原理概述 馮諾依曼原理,又稱「存儲程序控制原理」,是指計算機的程序和數據都存儲在同一個存儲器中,並且通過一個統一的匯流排來傳輸數據。這個原理的提出,是計算機科學發展中的重大進展,…

    編程 2025-04-25

發表回復

登錄後才能評論