A faster or cleaner way to find out if a package is installed on Android

I know that I can either catch a NameNotFoundException from the call to PackageManager.getPackageInfo , or skip the PackageInfo list returned by PackageManager.getInstalledPackages to find out if a particular package is installed, but both of them seem long perverted or ugly. I have more than 300 packages installed on my personal phone, so I would not want to do this operation every time I need to check. And catching an exception as a means of executing applied logic just makes me feel wrong with everything. Am I missing the isPackageInstalled method somewhere, or do I just need to implement it myself using one of the above methods? And if the latter, which will be considered a faster and less resource-intensive option?

+4
source share
4 answers

Since PackageManager.getInstalledPackages() returns a List , you do not need to PackageManager.getInstalledPackages() over it manually. You can use List.contains() or List.containsAll() to complete the task in a single line of code. Of course, this does not change the efficiency, since both methods probably contain a loop.

+2
source

If you use the API, you are really mistaken, then you can look at the hacks involving the following

Bash shell expression that receives a PM list Java Runtime expression Java packets and buffers and Java streams NIO Java grep

So the bash expression would be:

pm package list -f | sed 's / ^ package.//' | awk -F "=" '{print $ 2 "" $ 1}' | sort

and a list of links to handle stdout from the "pm list" so as to speed things up ...

PipedBuffers

NIO / grep

Runtime / streams

+1
source

Handling the name NotFoundExcepetion should not make you feel "wrong in everything" IMHO. According to the documentation, this exception will be thrown if the package does not exist from api level 1. Using the try / catch statement is very similar to using if / then to check for a null value.

In this case, this should not be considered a workaround or hack, since you are using the documented and expected return value of the exception to determine if the package exists.

I would suggest that this method would be faster than repeating through the List returned by getInstalledPackages (). However, I do not know what steps the android takes before returning NameNotFoundExcepetion. This would make an interesting test test.

I do not know any other practical method for testing an installed package.

0
source

I wrote several tests and experienced exception catching, as well as several different ways to extract installed packages and scroll through them. Here is my result

  • Calling PackageManager.getPackageInfo and catching a NameNotFoundException took 1 to 4 ms in all cases whether the requested package was installed or not, and I also included cases where it was the first call to PackageManager for a specific application launch and as a subsequent call just in case the structure caches this information to run the application.

  • The call to PackageManger.getPackageInfo took 1 to 1.5 seconds in all cases.

Calling getPackageInfo and removing an exception to determine if a package is installed is certainly a faster way to check.

0
source

All Articles