Listen for app installed / upgraded broadcast message in Android
Asked Answered
P

5

13

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.

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

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.

Peewee answered 24/4, 2012 at 11:42 Comment(6)
why is ACTION_PACKAGE_ADDED wrong?Markley
Because "Note that the newly installed package does not receive this broadcast";Peewee
You don't get the message yourself if you was just installed. That's not what you want I guess.Markley
zapi is correct -- you cannot use ACTION_PACKAGE_ADDED to find out about your own app being installed.Mathilda
AFAIK, This is quite difficult to get broadcast at the time of installation. But I have some trick may be this will help you. If you set some other broadcast receiver which can frequently calls in the application like Battery status, Phone state, wifi-state changed and some more so that there is possibility that you can get at least one of the broadcast receive so you can work at the time.Dignify
I sincerely hope that the newly installed app will never receive ACTION_PACKAGE_ADDED broadcasts, since it can be used to startup an app upon installation. Imagine the ways this could be exploited by malware.Yellows
K
14

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();

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.

Kneedeep answered 3/5, 2012 at 9:51 Comment(2)
post if you have any specific queries or something about what do you want to achieve.Kneedeep
From what I see in Lookout, they also have a FileSystemMonitorService to keep track of new installed apps. Maybe it's similar what's in your answer. In fact I think in Android, there is no broadcast message to keep track of new installed app. But using your way, it's possible.Peewee
S
8

You can try this receiver and permission. (But this seem only work in /system/app)^^"

<receiver
    android:name="com.your.receiver"
    android:enabled="true"
    android:exported="true" >
            <intent-filter>
                <action android:name="android.intent.action.PACKAGE_ADDED" />
                <action android:name="android.intent.action.PACKAGE_REMOVED" />
                    <data android:scheme="package"/> 
                </intent-filter>
 </receiver>
 <uses-permission android:name="android.permission.BROADCAST_PACKAGE_REMOVED" />
Shirlyshiroma answered 20/2, 2014 at 4:59 Comment(2)
Sending this intent is explicitly for System. But receiving it isn't.Hirundine
The permission BROADCAST_PACKAGE_REMOVED only granted for System Apps.Sherburne
R
2

Android won't send your a broadcast that you're being installed, but Google Play will. This won't help if your app is loaded through Amazon or through the debugger, but it does allow you to run code if your app is installed through Google Play: https://developers.google.com/android/reference/com/google/android/gms/tagmanager/InstallReferrerReceiver

Rondon answered 10/11, 2015 at 20:39 Comment(0)
B
0

You need to have two applications, one of them monitors the other app's installation and upgrades.

Berserker answered 1/5, 2012 at 7:42 Comment(0)
C
0

insipred by @Akhil I got a better idea

 val pm = packageManager
 val pkgInfo = pm.getPackageInfo(packageName, 0)
 VLog.e("vvv", "firstInstallTime=${pkgInfo.firstInstallTime}")
 VLog.e("vvv", "lastUpdateTime=${pkgInfo.lastUpdateTime}")
Crouch answered 26/8, 2022 at 8:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.