了解 Android 中的粘性意图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26038839/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 09:57:14  来源:igfitidea点击:

Knowing about Sticky intent in Android

android

提问by Shetty Suresh Babu.

In android there are 3 kinds of Intents,

在android中有3种意图,

  1. Intent,
  2. Sticky Intent,
  3. Pending intent.
  1. 意图,
  2. 粘性意图,
  3. 待定意图。

so What is sticky intent?

那么什么是粘性意图?

回答by Arun Antoney

Intent- is a message passing mechanism between components of Android, except for Content Provider. You can use Intent to start any component.

Sticky Intent- Sticks with Android, for future broadcast listeners. For example if BATTERY_LOW event occurs then that Intent will stick with Android so that any future requests for BATTERY_LOW, will return the Intent.

Pending Intent- If you want some one to perform any Intent operation at future point of time on behalf of you, then we will use Pending Intent.

Intent- 是 Android 组件之间的消息传递机制,Content Provider 除外。您可以使用 Intent 启动任何组件。

Sticky Intent- 坚持使用 Android,供未来的广播听众使用。例如,如果 BATTERY_LOW 事件发生,那么该 Intent 将与 Android 保持一致,以便将来对 BATTERY_LOW 的任何请求都将返回 Intent。

Pending Intent- 如果您希望某个人在未来某个时间点代表您执行任何 Intent 操作,那么我们将使用 Pending Intent。

回答by Umair

An intent that is used with sticky broadcast, is called as sticky intent. This intent will stick with android system for future broadcast receiver requests.

与粘性广播一起使用的意图称为粘性意图。对于未来的广播接收器请求,此意图将与 android 系统保持一致。

OR

或者

sendStickyBroadcast()performs a sendBroadcast(Intent)known as sticky, i.e. the Intent you are sending stays around after the broadcast is complete, so that others can quickly retrieve that data through the return value of registerReceiver(BroadcastReceiver, IntentFilter). In all other ways, this behaves the same as sendBroadcast(Intent). One example of a sticky broadcast sent via the operating system is ACTION_BATTERY_CHANGED. When you call registerReceiver()for that action -- even with a null BroadcastReceiver -- you get the Intent that was last broadcast for that action. Hence, you can use this to find the state of the battery without necessarily registering for all future state changes in the battery.

sendStickyBroadcast()执行一个sendBroadcast(Intent)称为粘性的,即您发送的 Intent 在广播完成后保留,以便其他人可以通过 的返回值快速检索该数据registerReceiver(BroadcastReceiver, IntentFilter)。在所有其他方面,这与sendBroadcast(Intent). 通过操作系统发送的粘性广播的一个例子是ACTION_BATTERY_CHANGED. 当您调用registerReceiver()该操作时——即使使用空的 BroadcastReceiver——您将获得该操作上次广播的 Intent。因此,您可以使用它来查找电池的状态,而不必注册电池中所有未来的状态变化。

回答by Nitendra Thakur

Intent: Intent is an asynchronous message which is use to communicate between the components in android , except Content Provider. for example you can start activity by startActivity(Intent intent);

Sticky Intent: sticky intents are associated with the android system for the future broadcast events.

Pending Intent: Those intent which you want to trigger at some time in future when you application is not alive.

Intent:Intent 是一个异步消息,用于在 android 中的组件之间进行通信,Content Provider 除外。例如,您可以通过 startActivity(Intent intent); 开始活动;

粘性意图:粘性意图与 Android 系统相关联,用于未来的广播事件。

Pending Intent:当您的应用程序不活动时,您希望在将来某个时间触发的那些意图。

回答by DeepakPanwar

Pending Intent:Pending Intent is actually an object which wraps an Intent to do some future work by another app.

Pending Intent:Pending Intent 实际上是一个对象,它包装了一个 Intent,以便由另一个应用程序完成一些未来的工作。

It lets us pass a future Intent to another application and allows 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.

它让我们将未来的 Intent 传递给另一个应用程序,并允许该应用程序执行该 Intent,就好像它与我们的应用程序具有相同的权限一样,无论最终调用 Intent 时我们的应用程序是否仍然存在。

A PendingIntent is generally used in cases were an AlarmManager needs to be executed or for Notifications. A PendingIntent provides a mean for applications to work, even after their process exits.

PendingIntent 通常用于需要执行 AlarmManager 或用于通知的情况。PendingIntent 为应用程序提供了一种工作方式,即使在它们的进程退出之后也是如此。

PendingIntent uses the following methods to handle the different types of intents:

PendingIntent 使用以下方法来处理不同类型的意图:

PendingIntent.getActivity() : Retrieve a PendingIntent to start an Activity
PendingIntent.getBroadcast() : Retrieve a PendingIntent to perform a Broadcast
PendingIntent.getService() : Retrieve a PendingIntent to start a Service

Example :

例子 :

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();
}

Intent:Intent is basically a message passing mechanism between different components of Android, except for Content Provider. You can use intent to start any component in Android.

Intent:Intent 基本上是 Android 不同组件之间的消息传递机制,Content Provider 除外。您可以使用 Intent 启动 Android 中的任何组件。

Sticky Intent:These are the Intents which sticks with Android for future broadcast listener.

Sticky Intent:这些是与 Android 一起用于未来广播侦听器的 Intent。

Sticky Intent is also a type of Intent which allows communication between a function and a service sendStickyBroadcast(), performs a sendBroadcast(Intent) known as sticky, the Intent you are sending stays around after the broadcast is complete, so that others can quickly retrieve that data through the return value of registerReceiver(BroadcastReceiver, IntentFilter). In all other ways, this behaves the same as sendBroadcast(Intent).

Sticky Intent 也是一种 Intent 类型,它允许函数和服务 sendStickyBroadcast() 之间进行通信,执行称为粘性的 sendBroadcast(Intent),您发送Intent 在广播完成后仍然存在,以便其他人可以快速检索该数据通过 registerReceiver(BroadcastReceiver, IntentFilter) 的返回值。在所有其他方面,这与 sendBroadcast(Intent) 的行为相同。

One example of a sticky broadcast sent via the operating system is ACTION_BATTERY_CHANGED. When you call registerReceiver() for that action — even with a null BroadcastReceiver — you get the Intent that was last Broadcast for that action. Hence, you can use this to find the state of the battery without necessarily registering for all future state changes in the battery.

通过操作系统发送的粘性广播的一个例子是 ACTION_BATTERY_CHANGED。当你为那个动作调用 registerReceiver() 时——即使是空的 BroadcastReceiver——你会得到那个动作的最后一个 Broadcast 的 Intent。因此,您可以使用它来查找电池的状态,而不必注册电池中所有未来的状态变化。

回答by Chunsheng Wei

An intent that is used with sticky broadcast, is called as sticky intent. This intent will stick with android system for future broadcast receiver requests.

与粘性广播一起使用的意图称为粘性意图。对于未来的广播接收器请求,此意图将与 android 系统保持一致。

回答by Sharanjeet Kaur

Sticky Intent allows a communication between function and a service sendStickyBroadcast() performs a sendBroadcast(Intent) know as sticky, the Intent you are sending stays around after the broadcast is complete so that others can quickly retrieve that data through the return value of registerReceiver(BroadcastReceiver, IntentFilter). In all other ways, this works the same as sendBroadcast(Intent).

Sticky Intent 允许函数和服务之间的通信 sendStickyBroadcast() 执行 sendBroadcast(Intent) 称为粘性,您发送的 Intent 在广播完成后仍然存在,以便其他人可以通过 registerReceiver() 的返回值快速检索该数据广播接收器、意图过滤器)。在所有其他方面,这与 sendBroadcast(Intent) 的工作方式相同。