从服务通知安装完成
我有这段代码
private void initiateInstallation() {
Intent intent = new Intent(Intent.ACTION_VIEW);
Uri uri = Uri.fromFile(new File("/sdcard/example.apk"));
intent.setDataAndType(uri, "application/vnd.android.package-archive");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
,从我的服务中安装一个名为 example.apk 的应用程序,
我希望在安装完成后运行一个通知用户有关安装的活动。我这样做了,除了该活动在安装完成之前出现之外。 问题是在服务中我无法使用 startActivityForResult。因此,我需要一种解决此问题的方法,以便仅在安装完成后才能开始我的通知活动(或者为了示例,只需在服务中使用 Toast 打印一些内容)。 我已经尝试过其他问题的一些答案,例如“服务中 startActivityforResult 的替代方案”,但我仍然无法弄清楚这一点。 我还放置了代码,以便也许可以在其中完成一些操作。
预先感谢...欢迎任何建议。
I have this piece of code
private void initiateInstallation() {
Intent intent = new Intent(Intent.ACTION_VIEW);
Uri uri = Uri.fromFile(new File("/sdcard/example.apk"));
intent.setDataAndType(uri, "application/vnd.android.package-archive");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
that from within my service installs an application named example.apk
I want after the installation is finished to run an activity which notifies the user about the installation.I did that except the activity appears before the installation finishes.
The problem is that within a service I cannot use startActivityForResult. So, I need a way around this so that I can start my notification activity(or for the sake of example just print something out with Toast within the service) only AFTER the installation is complete.
I already tried some answers from other questions like "alternative to startActivityforResult in services" but still I couldn't figure this out.
I also put the code so that maybe there may be something done in there.
Thanks in advance ... any suggestions are welcome.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以收听 PACKAGE_ADDED 广播意图:http://developer.android。 com/reference/android/content/Intent.html#ACTION_PACKAGE_ADDED
据我所知,这些是在安装完成后发送的,您可以从 服务。
请注意,如果应用程序已安装,您将获得 ACTION_PACKAGE_CHANGED (据我所知)。
此外,您还必须知道包名称,而不仅仅是 apk 名称,因为意图将包含包名称。
You could listen to the PACKAGE_ADDED broadcast intent: http://developer.android.com/reference/android/content/Intent.html#ACTION_PACKAGE_ADDED
As far as I know, these are sent after the installation is done, and you can listen to those from the service.
Just note that if the application was already installed, you will get ACTION_PACKAGE_CHANGED (as far as I know).
Also you must know the package name as well, not just the apk name, since the intent will contain the package name.
@Pal Szasz 给出的答案在技术上是正确的(据我所知;-))。
但是,根据您问题中提供的信息,我假设您只想显示通知(不执行进一步的编程操作)。如果我的假设正确,我会恭敬地建议您不要显示此类通知。这就是原因:
Android 系统已经有了向用户传递通知的标准方法。在这种情况下,状态栏已向您显示一条消息,表明新应用程序已成功安装(或在出现错误时未安装)。如果您实施另一个通知渠道,您很可能会因偏离标准的预期行为而使用户感到困惑或恼怒。
如果这超越了理智的界限,人们也可能会争论这样一个事实:在某种意义上,你也会对 Android 的碎片化做出贡献(虽然规模很小,但无论如何)。
The answer given by @Pal Szasz is technically correct (as far as I know ;-) ).
However, based on the information given in your question, I assume you only wish to show a notification (no further programmatically actions are to be performed). If my assumptions are correct I would respectfully advise you NOT to show such a notification. And this is why:
The Android system already has a standard means of passing notifications to the user. The status bar will in this case already show you a message saying that the new app is successfully installed (or not installed in case of an error). If you implement yet another notification channel you will most likely confuse or irritate your users by diverging from the standard, expected behaviour.
Taking this beyond the borders of sanity one could also argue for the fact that you in some sense also would contribute to the fragmentation of Android (in a very small scale, but nevertheless).