user3541639
user3541639

Reputation:

How to get the application name of the recently installed application

I am detecting if an application is installed in an android device using the broadcast receiver.

During that time that I detected an application was installed, I want to get the application name of that application.

I tried to search the web but I can only find how to get the all the applications installed, can anyone help me?

Upvotes: 3

Views: 704

Answers (2)

user3541639
user3541639

Reputation:

I added a simple logic that will get the recently installed application(appName and packageName).

        PackageManager pm = context.getPackageManager();
        List<ApplicationInfo> packages = pm.getInstalledApplications(PackageManager.GET_META_DATA);

        long prev_modified = 0;
        packageName = null;
        appName = null;
        for (ApplicationInfo packageInfo : packages) {
            String appFile = packageInfo.sourceDir;
            long lastModified = new File(appFile).lastModified();

            //Use this to get first time install time
            //long installed = context.getPackageManager().getPackageInfo(packageName, 0).firstInstallTime;
            if (lastModified > prev_modified) {
                prev_modified = lastModified;
                packageName = packageInfo.packageName;
                appName = packageInfo.loadLabel(context.getPackageManager()).toString();

            }
        }

        Toast.makeText(context, packageName + "\n" + appName, Toast.LENGTH_SHORT).show();

Upvotes: 1

Niko Adrianus Yuwono
Niko Adrianus Yuwono

Reputation: 11112

With this code you can get the list of installed application and recent date of install/update of the package.

final PackageManager pm = getPackageManager();
List<ApplicationInfo> packages = pm.getInstalledApplications(PackageManager.GET_META_DATA);

for (ApplicationInfo packageInfo : packages) {
    String packageName = packageInfo.packageName;
    String appFile = packageInfo.sourceDir;
    long lastModified = new File(appFile).lastModified();
    //Use this to get first time install time
    //long installed = context.getPackageManager().getPackageInfo(packageName, 0).firstInstallTime;
    Log.d(TAG, "Installed package :" + packageName);
    Log.d(TAG, "Source dir : " + appFile);
    Log.d(TAG, "Last Modified Time :" + lastModified); 
}

Upvotes: 1

Related Questions