我阅读了Android文档,但我仍然需要更多的说明。PendingIntent到底是什么?
当前回答
出租车的类比
意图
intent通常用于启动Services。例如:
Intent intent = new Intent(CurrentClass.this, ServiceClass.class);
startService(intent);
这就像你叫出租车的时候:
Myself = CurrentClass
Taxi Driver = ServiceClass
悬而未决的意图
你需要使用像这样的东西:
Intent intent = new Intent(CurrentClass.this, ServiceClass.class);
PendingIntent pi = PendingIntent.getService(parameter, parameter, intent, parameter);
getDataFromThirdParty(parameter, parameter, pi, parameter);
现在这个第三方将代表您启动服务。 现实生活中的一个类比是Uber或Lyft,它们都是出租车公司。
你向优步/来福车发出打车请求。然后他们会代表你打电话给他们的司机。
因此:
Uber/Lyft ------ ThirdParty which receives PendingIntent
Myself --------- Class calling PendingIntent
Taxi Driver ---- ServiceClass
其他回答
为什么需要PendingIntent ?我在想
为什么接收应用程序本身不能创建Intent或 为什么我们不能使用一个简单的意图达到同样的目的。
例如:Intent bluetoothIntent= new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
如果我将bluetoothIntent发送到另一个没有android。permission权限的应用程序。接收应用程序不能通过startActivity(bluetoothIntent)启用蓝牙。
使用PendingIntent可以克服这个限制。对于PendingIntent,接收应用程序不需要android.permission。BLUETOOTH_ADMIN用于开启蓝牙。源。
在我的例子中,上述答案和谷歌的官方文档都没有帮助我抓住PendingIntent类的概念。
然后我发现了这个视频,谷歌I/O 2013, Beyond Blue Dot会议。在这个视频中,前谷歌员工Jaikumar Ganesh解释了PendingIntent是什么,这让我对它有了一个大的了解。
以下是上述视频的转录(从15:24)。
待定意图是什么? 它是你的应用进程会给定位进程的一个令牌,定位进程会在感兴趣的事件发生时用它来唤醒你的应用。这基本上意味着你的应用在后台不需要一直在运行。当有趣的事情发生时,我们会叫醒你。这样可以节省很多电池。
通过下面的代码片段(包含在会话的幻灯片中),这种解释变得更加清晰。
PendingIntent mIntent = PendingIntent.getService(...);
mLocationClient.requestLocationUpdates(locationRequest, mIntent);
public void onHandleIntent(Intent intent) {
String action = intent.getAction();
if (ACTION_LOCATION.equals(action)) {
Location location = intent.getParcelableExtra(...)
}
}
待定意图是指将在未来某个时间点启动的意图。正常的意图在传递给startActivity(intent)或StartService(intent)时立即启动。
PendingIntent基本上是一个包装另一个Intent对象的对象。然后它可以传递给一个外部应用程序,在那里你授予该应用程序执行操作的权利,即,执行意图,就像从你自己的应用程序的进程执行一样(相同的权限和身份)。出于安全原因,您应该始终将显式意图传递给PendingIntent,而不是隐式意图。
PendingIntent aPendingIntent = PendingIntent.getService(Context, 0, aIntent,
PendingIntent.FLAG_CANCEL_CURRENT);
Pending Intent指定将来要执行的操作。它允许你将未来的Intent传递给另一个应用程序,并允许该应用程序执行该Intent,就像它拥有与你的应用程序相同的权限一样,无论你的应用程序是否仍然存在,当Intent最终被调用时。
它是一个令牌,你给一个外部应用程序,它允许外部应用程序使用你的应用程序的权限来执行预定义的代码段。
如果你给了一个外部应用程序一个Intent,并且这个应用程序发送/广播你给的Intent,他们会用他们自己的权限执行这个Intent。但是如果你给外部应用一个你用自己的权限创建的Pending Intent,该应用将使用你的应用权限执行包含的Intent。
要通过挂起意图执行广播,请通过PendingIntent. getbroadcast()获取一个PendingIntent。要通过挂起intent来执行一个activity,你需要通过PendingIntent.getActivity()来接收这个activity。
这是一个你想要执行的Intent动作,但是在稍后的时间。就当是把一个意图冷冻起来。需要它的原因是必须从应用程序中的有效上下文中创建和启动Intent,但在某些情况下,当你想要运行操作时,Intent是不可用的,因为你在技术上处于应用程序的上下文之外(两个常见的例子是从Notification或BroadcastReceiver启动Activity。
通过创建一个PendingIntent你想要用它来启动,比如说,当你有上下文的时候(从另一个Activity或Service内部),你可以把这个对象传递给外部的东西,以便它代表你启动应用程序的一部分。
A PendingIntent provides a means for applications to work, even after their process exits. Its important to note that even after the application that created the PendingIntent has been killed, that Intent can still run. A description of an Intent and target action to perform with it. Instances of this class are created with getActivity(Context, int, Intent, int), getBroadcast(Context, int, Intent, int), getService (Context, int, Intent, int); the returned object can be handed to other applications so that they can perform the action you described on your behalf at a later time.
通过将PendingIntent授予另一个应用程序,您授予它执行您所指定的操作的权利,就好像另一个应用程序就是您自己一样(具有相同的权限和标识)。因此,你应该注意如何构建PendingIntent:通常,例如,你提供的base Intent会显式地将组件名称设置为你自己的一个组件,以确保它最终被发送到那里而不是其他地方。
A PendingIntent itself is simply a reference to a token maintained by the system describing the original data used to retrieve it. This means that, even if its owning application’s process is killed, the PendingIntent itself will remain usable from other processes that have been given it. If the creating application later re-retrieves the same kind of PendingIntent (same operation, same Intent action, data, categories, and components, and same flags), it will receive a PendingIntent representing the same token if that is still valid, and can thus call cancel() to remove it.
推荐文章
- 从资产中读取文件
- 在不活动的地方调用getLayoutInflater()
- 设置Android布局元素的背景颜色
- 错误:'keytool'不能被识别为内部或外部命令、可操作程序或批处理文件
- 在应用程序本身中更改Locale
- apk (.apk)和应用程序包(.aab)的区别
- 如何设置超时在改造库?
- Android - SPAN_EXCLUSIVE_EXCLUSIVE跨度不能为零长度
- TextView的字体大小在Android应用程序改变字体大小从本机设置
- 如何模拟Android杀死我的进程
- 禁用EditText闪烁光标
- Android Eclipse -无法找到*.apk
- 设置TextView文本从html格式的字符串资源在XML
- 如何允许所有网络连接类型HTTP和HTTPS在Android(9)馅饼?
- Android加载JS包失败