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使用以下方法来处理不同类型的意图:
PendingIntent.getActivity()
: Retrieve a PendingIntent to start an ActivityPendingIntent.getActivity()
:检索PendingIntent以启动活动PendingIntent.getBroadcast()
: Retrieve a PendingIntent to perform a BroadcastPendingIntent.getBroadcast()
:检索要执行广播的PendingIntentPendingIntent.getService()
: Retrieve a PendingIntent to start a ServicePendingIntent.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()方法内部的参数及其用法: