我阅读了Android文档,但我仍然需要更多的说明。PendingIntent到底是什么?
当前回答
为什么需要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分钟。
这就是挂起意图和常规意图之间的区别。常规意图可以在没有挂起意图的情况下创建,然而,为了创建挂起意图,你需要先设置一个常规意图。
悬而未决的意图
暂挂意图是对常规意图的包装 供另一个应用程序使用。 它使其他应用程序能够执行包含的功能 行动,因为它是你的应用程序与所有的权限 申请已获批准
当你想打开一些应用程序组件,如活动/服务/BroadcastReceiver在以后的时间或指定的时间间隔后,你必须发送PendingIntent在这种情况下。它就像你给其他应用程序一段时间后代表你运行你的应用程序代码的许可条。因此,PendingIntent可以超越进程边界,比如你想要AlarmManager,这是另一个进程中的另一个应用程序,然后AlarmManager在你的应用程序上执行由PendingIntent指定的操作
待定意图是指将在未来某个时间点启动的意图。正常的意图在传递给startActivity(intent)或StartService(intent)时立即启动。
出租车的类比
意图
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,你给外部应用程序执行你的许可。 如:
你的音乐应用程序的通知不能播放/暂停音乐如果你 没有给PendingIntent发送广播,因为你的音乐应用 哪个通知应用程序具有READ_EXTERNAL_STORAGE权限 不喜欢。通知是一个系统服务(所以它是一个外部应用程序)。
推荐文章
- 视图绑定-我如何获得包含布局的绑定?
- 在Android Studio中改变矢量资产的填充颜色
- 在构建中编写注释的语法是什么?gradle文件?
- 如何以编程方式添加按钮色调
- 用Android Studio进行调试永远停留在“等待调试器”状态
- Openssl不被视为内部或外部命令
- 无法执行dex:在Eclipse中超过GC开销限制
- 如何以编程方式将视图添加到视图
- 单击url会打开默认浏览器
- 使用Retrofit刷新OAuth令牌,而不修改所有调用
- 多个dex文件定义了landoid /support/v4/accessibilityservice/AccessibilityServiceInfoCompat
- 如何获得动作栏的高度?
- 从活动外部调用startActivity() ?
- createScaledBitmap的过滤器参数做什么?
- 为什么我在使用adb时访问数据文件夹被拒绝?