如何知道我的 Android 应用程序已升级以重置闹钟?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2133986/
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
How to know my Android application has been upgraded in order to reset an alarm?
提问by tbruyelle
I noticed that an alarm is disabled when the application which sets this alarm has been upgraded. Is that true ?
我注意到当设置此警报的应用程序升级时,警报被禁用。真的吗 ?
Until now, I used the SharedPreferences
with a FIRST_RUN
key in order to know if it's the first run of my application. If I don't find this key, I enable the alarm and set FIRST_RUN
to false, else I do nothing.
到现在为止,我使用SharedPreferences
带FIRST_RUN
密钥的 是为了知道它是否是我的应用程序的第一次运行。如果我找不到这个键,我就启用警报并设置FIRST_RUN
为 false,否则我什么都不做。
But I noticed also that these preferences remain intact between app upgrade !
但我也注意到这些偏好在应用程序升级之间保持不变!
So after an upgrade, the FIRST_RUN
key is already false, so I do nothing while my alarm need to be enabled.
所以升级后,FIRST_RUN
密钥已经是假的,所以我什么都不做,而我的警报需要启用。
How to handle such case ?
遇到这种情况怎么办?
Thanks in advance
提前致谢
采纳答案by Dan Lew
I've never tried this myself, but what about creating a BroadcastReceiver
that listens to the ACTION_PACKAGE_REPLACED
Intent?
我自己从来没有尝试过这个,但是创建一个BroadcastReceiver
听ACTION_PACKAGE_REPLACED
意图的东西呢?
I've thought about trying this before, but I'm not sure if there's a chicken-and-egg problem with it or not (e.g., does the Intent get sent before the new upgraded application can receive it?). Worth a try, though.
我以前曾考虑过尝试这个,但我不确定它是否存在先有鸡还是先有蛋的问题(例如,是否在新升级的应用程序可以接收到它之前发送了 Intent?)。不过值得一试。
回答by tbruyelle
Solution by Daniel Lew :
Daniel Lew 的解决方案:
Need a receiver with the following lines in manifest :
需要清单中包含以下行的接收器:
<receiver android:name=".OnUpgradeReceiver">
<intent-filter>
<action android:name="android.intent.action.PACKAGE_REPLACED" />
<data android:scheme="package" android:path="your.app.package" />
</intent-filter>
</receiver>
android:path
is used in order to prevent OnUpgradeReceiver
to be triggered by any upgrade of any application.
android:path
用于防止OnUpgradeReceiver
被任何应用程序的任何升级触发。
回答by Sami Eltamawy
Simply, listen to the android.intent.action.MY_PACKAGE_REPLACED
... This INTENT
will notify you if a new version of your application has been installed over an existing one
简单地,听听android.intent.action.MY_PACKAGE_REPLACED
...INTENT
如果您的应用程序的新版本已安装在现有应用程序上,这将通知您
Note: This intent can is available starting from API 12
注意:这个意图可以从 API 12 开始可用
回答by armansimonyan13
For the Android OS v12 and above you need to register BroadcastReceiver with action ACTION_MY_PACKAGE_REPLACED
对于 Android OS v12 及更高版本,您需要使用操作ACTION_MY_PACKAGE_REPLACED注册 BroadcastReceiver
<receiver android:name=".MyBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.MY_PACKAGE_REPLACED" />
</intent-filter>
</receiver>