我阅读了Android文档,但我仍然需要更多的说明。PendingIntent到底是什么?
当前回答
一个PendingIntent是一个令牌,你给另一个应用程序(例如通知管理器,警报管理器或其他第三方应用程序),它允许这个其他应用程序使用你的应用程序的权限来执行预定义的代码段。 要通过挂起意图执行广播,请通过PendingIntent. getbroadcast()获取一个PendingIntent。要通过挂起intent来执行一个activity,你需要通过PendingIntent.getActivity()来接收这个activity。
其他回答
In an easy language, 1. A description of an Intent and Target action to perform. First you have to create an intent and then you have to pass an specific java class which you want to execute, to the Intent. 2. You can call those java class which is your class action class by PendingIntent.getActivity, PendingIntent.getActivities(Context, int, Intent[], int), PendingIntent.getBroadcast(Context, int, Intent, int), and PendingIntent.getService(Context, int, Intent, int); Here you see that Intent which is comes from the step 1 3. You should keep in mind that...By giving a PendingIntent to another application, you are granting it the right to perform the operation you have specified.
这是我读了很长时间后学到的。
为什么需要PendingIntent ?我在想
为什么接收应用程序本身不能创建Intent或 为什么我们不能使用一个简单的意图达到同样的目的。
例如:Intent bluetoothIntent= new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
如果我将bluetoothIntent发送到另一个没有android。permission权限的应用程序。接收应用程序不能通过startActivity(bluetoothIntent)启用蓝牙。
使用PendingIntent可以克服这个限制。对于PendingIntent,接收应用程序不需要android.permission。BLUETOOTH_ADMIN用于开启蓝牙。源。
什么是意图?
An Intent is a specific command in Android that allows you to send a command to the Android OS to do something specific. Think of it as an action that needs to take place. There are many actions that can be done such as sending an email, or attaching a photo to an email, or even launching an application. The logical workflow of creating an intent is usually as follows: a. Create the Intent b. Add Intent options -> Ex. what type of intent we are sending to the OS or any attributes associated with that intent, such as a text string or something being passed along with the intent c. RUN the Intent
现实生活中的例子:假设我早上醒来,我“打算”去洗手间。我首先要考虑去洗手间,但这并不能让我真正去洗手间。然后我必须告诉我的大脑先起床,然后去洗手间,然后放松,然后去洗手,然后去擦手。一旦我知道我要去哪里,我发送命令开始,我的身体采取行动。
什么是未决意图?
从现实生活中的例子继续,假设我想洗澡,但我想在刷牙和吃早餐后洗澡。所以我知道我至少要等30-40分钟才能洗澡。我脑子里还在想,我需要准备好衣服,然后上楼回到浴室,然后脱衣服,然后洗澡。然而,这要等到30-40分钟后才会发生。我现在有洗澡的意向。等待30-40分钟。
这就是挂起意图和常规意图之间的区别。常规意图可以在没有挂起意图的情况下创建,然而,为了创建挂起意图,你需要先设置一个常规意图。
一个PendingIntent是一个令牌,你给另一个应用程序(例如通知管理器,警报管理器或其他第三方应用程序),它允许这个其他应用程序使用你的应用程序的权限来执行预定义的代码段。 要通过挂起意图执行广播,请通过PendingIntent. getbroadcast()获取一个PendingIntent。要通过挂起intent来执行一个activity,你需要通过PendingIntent.getActivity()来接收这个activity。
我在通知中遇到了PendingIntents。这里有一个简单的解释:
我们想要提供一个Intent到Notification,在这种情况下,我们想要打开一个执行相机捕捉功能的Activity。这里,如果我们简单地传递Intent, NotificationManager没有这个权限,尽管我的应用程序在Manifest中声明了这个权限;因此,该动作不会工作,因为NotificationManager没有这样做的权限。
但是,如果你使用PendingIntent,这里的权限,我的应用程序将被使用而不是NotificationManager。因此,即使NotificationManager没有相机权限,而我的应用程序有,它仍然会打开活动并执行应用程序。
注意:Pending intent需要常规intent先被设置。
推荐文章
- 改变开关的“开”色
- 以编程方式将EditText的输入类型从PASSWORD更改为NORMAL,反之亦然
- 如何在隐藏和查看密码之间切换
- 在Android上调整一个大的位图文件到缩放输出文件
- 如何更改Android版本和代码版本号?
- Android Studio突然无法解析符号
- 应用程序重新启动而不是恢复
- 如何设置整个应用程序在纵向模式?
- Android中文本的阴影效果?
- 以编程方式设置TextView的布局权重
- Android -如何覆盖“后退”按钮,所以它不完成()我的活动?
- 如何从通知点击发送参数到一个活动?
- 导航目标xxx对于这个NavController是未知的
- 使用ConstraintLayout均匀间距的视图
- 文件google-services错误。模块根文件夹中缺少Json。谷歌服务插件没有它就不能正常工作。