在Android中收听应用程序已安装/已升级的广播消息 [英] Listen for app installed / upgraded broadcast message in Android

查看:522
本文介绍了在Android中收听应用程序已安装/已升级的广播消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Lookout应用程序(https://play.google.com/store/apps/details?id=com.lookout),每次安装或升级应用程序时都会看到,它将自动扫描该应用程序以确保不是恶意的.

Using Lookout app (https://play.google.com/store/apps/details?id=com.lookout), I see every time I install or upgrade app, it'll automatically scan this app to ensure it's not malicious.

跟随监视,我编写了一个简单的应用程序,每当安装或升级每个应用程序时,它都会收听广播消息. AFAIK,有某种类型的IntentFilter用于广播消息,它是:

Follow Lookout, I write a simple app which listen broadcast message whenever each app is installed or upgraded. AFAIK, there are some type of IntentFilter for broadcast message, it is:

  • Intent.ACTION_PACKAGE_ADDED
  • Intent.ACTION_PACKAGE_CHANGED
  • Intent.ACTION_PACKAGE_INSTALL

我希望Intent.ACTION_PACKAGE_ADDED是答案,但这是错误的(ACTION_PACKAGE_ADDED:设备上已安装了新的应用程序包.数据包含该包的名称.请注意,新安装的包不会收到此广播.)而不推荐使用ACTION_PACKAGE_INSTALL.

I hope Intent.ACTION_PACKAGE_ADDED is the answer but it's wrong (ACTION_PACKAGE_ADDED: A new application package has been installed on the device. The data contains the name of the package. Note that the newly installed package does not receive this broadcast.) while ACTION_PACKAGE_INSTALL is deprecated.

有人可以告诉我更好的方法吗?欢迎任何帮助.

Can anyone tell me a better way? Any help is welcome.

推荐答案

如果要安装应用程序A,则设备上的所有其他应用程序都会得到Intent,即应用程序A是新安装的应用程序,而不是A本身,例如它似乎没有任何用处.现在,如果以后安装或更改其他应用,A将获得广播.

如果要了解何时安装应用程序或其他应用程序的上次安装或更新时间,则可以始终使用PackageManager:

If you are installing an application A, all other applications on the device will get the Intent that application A is the newly installed application but not A itself as it doesn't seem of any use. Now A will get broadcasts if other apps are later installed or changed.

If you want to find out at when your app was installed or some other app's the last install or update time, you can always use PackageManager:

PackageManager pm = context.getPackageManager();
ApplicationInfo appInfo = pm.getApplicationInfo("app.package.name", 0);
String appFile = appInfo.sourceDir; 
long installed = new File(appFile).lastModified();

此处app.package.name是要查找安装时间的应用程序的软件包名称.如果要在应用程序中使用它,请输入应用程序的程序包名称.

here app.package.name is the package name of the app you want to find out the install time. If you want to use it for your app, pass in your app's package name.

这篇关于在Android中收听应用程序已安装/已升级的广播消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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