深入理解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/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

发表回复

登录后才能评论