如何持久保存另一个应用程序提供的PendingIntent [英] How to persistently save PendingIntent provided by another application

查看:230
本文介绍了如何持久保存另一个应用程序提供的PendingIntent的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我要实现一个将服务公开给其他应用(例如Google Play服务..)的应用.

let's say I want to implement an app which exposes services to other apps (like Google Play Services..).

潜在的应用程序会注册到与我的服务相关的特殊事件,并在适当的时间得到通知.

potential apps would register to my special events associated with my services, and would be notified at the right time.

我正在考虑完全像Google使用Google Play服务那样实现这一点:

I was thinking to implement this exactly like Google did with the Google Play services:

由于Android进程间通信,其他应用程序可以绑定到我的应用程序Service,这样-可以传递给我的应用程序PendingIntent回调",以便我在适当的时间对其执行.

thanks to Android Inter-Process Communication, other apps could bind to my app Service, and by that - pass to my app PendingIntent "callback" that I could execute for them at the right time.

现在,我要解决这个问题:

now, I'll get to the problem:

  • 我的应用程序进程当前正在运行(在后台),并且保留对其他应用程序提供的PendingIntent的引用.

现在,由于某种原因(系统决策/明确地由用户),我的进程已停止.

now, from some reason (System decisions/ user explicitly) my process been stopped.

我的流程在某个时候回溯,然后回到做这件事."

my process cumming back in some point, and come back to "do it's thing.."

-我丢失了对以前提供给我的PendingIntent的引用,并且我在API中看不到任何方法来检索对它的引用.

in that point - I lost reference to the PendingIntent provided to me before, and I don't see any way in the API to retrieve back reference to it.

我也看不到任何方法来永久保存(数据库/sharedPreferences/文件系统)以保存待使用的意图供以后使用

also I don't see any way to save persistently(database/sharedPreferences/file system) saving the pending intent for latter on usage

我的问题是:

  • 是否可以以某种方式持久存储未决意图?

  • is it possible to store pending intent persistently somehow?

是否可以找回"对我之前已经获得的相同未决意图的引用?

is it possible to "get back" reference to the same pending intent I already got before?

如果不是,是否还有其他建议来实现我所描述的方法?

if not, is there any other suggestion to implement such thing as I described?

推荐答案

是否可以以某种方式持久存储未决意图?

is it possible to store pending intent persistently somehow?

否.

是否可以找回"对我之前已经获得的相同未决意图的引用?

is it possible to "get back" reference to the same pending intent I already got before?

不是来自操作系统.如果您有其他自举"通信方法,则可以要求原始应用重新提供PendingIntent.例如,您可以发送广播声明您需要重新注册应用程序.使用您服务的应用程序会收听此类广播并为您提供新鲜的PendingIntent.

Not from the OS. If you have some other "bootstrap" communications method, you could ask the original app to re-supply a PendingIntent. For example, you could send a broadcast stating that you need apps to re-register; apps using your service would listen for such broadcasts and give you a fresh PendingIntent.

或者,完全跳过PendingIntent并使用其他功能.例如,应用程序可以导出BroadcastReceiver.他们将在您当前的计划中注册PendingIntent的地方,他们只是为您提供BroadcastReceiverComponentName.该信息(程序包名称和类名称)可以保留,然后您可以根据需要向该特定的ComponentName发送广播.

Or, skip the PendingIntent entirely and use something else. For example, apps could export a BroadcastReceiver. Where they would register a PendingIntent in your current plan, they would simply provide you the ComponentName of the BroadcastReceiver. That information (package name and class name) could be persisted, and you could then send a broadcast to that specific ComponentName as needed.

请注意,对于任何涉及持久性的策略,您都需要处理客户端应用程序已升级且旧存储的详细信息现在不正确的情况(例如,它们重构了代码,而旧的ComponentName是现在无效).

Note that with any strategy that involves persistence, you will need to deal with the cases where the client app has been upgraded and the old stored details are now incorrect (e.g., they refactored their code, and the old ComponentName is now invalid).

这篇关于如何持久保存另一个应用程序提供的PendingIntent的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆