Android通知,PendingIntent示例

Welcome to Android Notification Example using android PendingIntent. In this tutorial we’re going to discuss and implement PendingIntent and build Notification in our application.

欢迎使用Android PendingIntent的Android通知示例。 在本教程中,我们将讨论和实现PendingIntent并在应用程序中构建Notification

Android PendingIntent (Android PendingIntent)

Android PendingIntent is an object that wraps up an intent object and it specifies an action to be taken place in future. In other words, PendingIntent lets us pass a future Intent to another application and allow that application to execute that Intent as if it had the same permissions as our application, whether or not our application is still around when the Intent is eventually invoked.

Android PendingIntent是包装intent对象的对象,它指定将来要执行的操作。 换句话说, PendingIntent允许我们将将来的Intent传递给另一个应用程序,并允许该应用程序执行该Intent,就好像它具有与我们的应用程序相同的权限一样,无论最终调用该Intent时我们的应用程序是否还在。

A PendingIntent is generally used in cases were an AlarmManager needs to be executed or for Notification (that we’ll implement later in this tutorial). A PendingIntent provides a means for applications to work, even after their process exits.

PendingIntent通常用于需要执行AlarmManager或用于Notification的情况 (我们将在本教程的稍后部分实现)。 PendingIntent提供了一种使应用程序工作的方法,即使它们退出了进程也是如此。

For security reasons, the base Intent that is supplied to the PendingIntent must have the component name explicitly set to ensure it is ultimately sent there and nowhere else. Each explicit intent is supposed to be handled by a specific app component like Activity, BroadcastReceiver or a Service. Hence PendingIntent uses the following methods to handle the different types of intents:

出于安全原因,提供给PendingIntent的基本Intent必须具有明确设置的组件名称,以确保最终将其发送到该地址。 每个明确的意图都应该由特定的应用程序组件(例如Activity, BroadcastReceiver或Service)处理。 因此,PendingIntent使用以下方法来处理不同类型的意图:

  1. PendingIntent.getActivity() : Retrieve a PendingIntent to start an Activity

    PendingIntent.getActivity() :检索PendingIntent以启动活动
  2. PendingIntent.getBroadcast() : Retrieve a PendingIntent to perform a Broadcast

    PendingIntent.getBroadcast() :检索要执行广播的PendingIntent
  3. PendingIntent.getService() : Retrieve a PendingIntent to start a Service

    PendingIntent.getService() :检索PendingIntent以启动服务

An example implementation of PendingIntent is given below.

下面给出了PendingIntent的示例实现。

Intent intent = new Intent(this, SomeActivity.class);
 
// Creating a pending intent and wrapping our intent
PendingIntent pendingIntent = PendingIntent.getActivity(this, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT);
try {
    // Perform the operation associated with our pendingIntent
    pendingIntent.send();
} catch (PendingIntent.CanceledException e) {
    e.printStackTrace();
}

The operation associated with the pendingIntent is executed using the send() method.

使用send()方法执行与未决Intent相关的操作。

The parameters inside the getActivity() method and there usages are described below :

下面介绍了getActivity()方法内部的参数及其用法:

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值