getPackageManager ().getInstalledPackages (PackageManager.GET_ACTIVITIES) returns null - android

If I call
PackageManager pm = getPackageManager () ;
List<PackageInfo> pis = pm.getInstalledPackages (PackageManager.GET_PROVIDERS) ;
I get a list of installed packages, including any provivders they declare (i.e, with pis[i].providers possibly being non-null).
However, if I include PackageManager.GET_ACITIVITIES among the flags, as in
PackageManager pm = getPackageManager () ;
List<PackageInfo> pis = pm.getInstalledPackages (PackageManager.GET_ACTIVITIES | PackageManager.GET_PROVIDERS) ;
I expect to get the "same" list of installed packages, but with pis[i].activities being non-null as well. But I don't, I get an empty list.
Is there something special about including PackageManager.GET_ACTIVITES among the flags that isn't mention in the documentation?
My current work around is to leave PackageManager.GET_ACTIVITIES out of the flags, then loop through the returned list as follows:
for (PackageInfo pi : pis) {
try {
PackageInfo tmp = pm.getPackageInfo (pi.packageName, PackageManager.GET_ACTIVITIES) ;
pi.activities = tmp.activities ;
}
catch (NameNotFoundException e) {
Log.e (TAG, e.getMessage ()) ;
}
But that seems like a real kludge.
The only mention I could find of getInstalledPackages (PackageManager.GET_ACTIVITIES) returning an empty list is here, but the problem in that case seems to have been something about calling getInstalledPackages() outside the main application thread and that is not the situation in my case.
p.s. this is the .602 VZW build of Gingerbread, in case that matters

I came across the same issue and found out a better workaround:
public void listAllActivities() throws NameNotFoundException
{
List<PackageInfo> packages = getPackageManager().getInstalledPackages(0);
for(PackageInfo pack : packages)
{
ActivityInfo[] activityInfo = getPackageManager().getPackageInfo(pack.packageName, PackageManager.GET_ACTIVITIES).activities;
Log.i("Pranay", pack.packageName + " has total " + ((activityInfo==null)?0:activityInfo.length) + " activities");
if(activityInfo!=null)
{
for(int i=0; i<activityInfo.length; i++)
{
Log.i("PC", pack.packageName + " ::: " + activityInfo[i].name);
}
}
}
}
Notice that I need to query PackageManager twice. Once using getPackageManager().getInstalledPackages(...) and again using getPackageManager().getPackageInfo(...)
I hope it helps.

Related

PackageManager getChangedPackages always return NULL

In my app I need to monitorize recently added or updated packages, but since Oreo this is a hard task.
To do it I have a service that runs every X time to detect the new installed/updated apps.
The main core of this service is to call the getChangedPackages function from the PackageManager, but this function always returns null, even if I install or update any app from or not from the Play Store in the interval between two consequtive calls to getChangedPackages.
https://developer.android.com/reference/android/content/pm/PackageManager.html#getChangedPackages(int)
I need to request any permission to call this function? Is the getChangedPackages buggy?
private void _doProcess()
{
try
{
PackageManager package_manager = getPackageManager();
int sequence_number = ApplicationPreferences.getInteger(this, GET_CHANGED_PACKAGES_SEQUENCE_NUMBER_KEY, 0);
ChangedPackages changed_packages = package_manager.getChangedPackages(sequence_number);
LogUtilities.show(this, String.format("Retrieve recently apps installs/updates using sequence number %d returns %s", sequence_number, changed_packages == null ? "null" : "a not null object"));
if (changed_packages == null) changed_packages = package_manager.getChangedPackages(0);
LogUtilities.show(this, String.format("Retrieve recently apps installs/updates using sequence number %d returns %s", sequence_number, changed_packages == null ? "null" : "a not null object"));
if (changed_packages != null)
{
List<String> packages_names = changed_packages.getPackageNames();
LogUtilities.show(this, String.format("%d recently installed/updated apps", packages_names == null ? 0 : packages_names.size()));
if (packages_names != null) for (String package_name : packages_names) PackagesUpdatedReceiver.doProcessPackageUpdate(this, new Intent(isNewInstall(package_manager, package_name) ? Intent.ACTION_PACKAGE_ADDED : Intent.ACTION_PACKAGE_REPLACED).setData(Uri.parse(String.format("package:%s", package_name))));
LogUtilities.show(this, String.format("Storing %s is the sequence number for next iteration", changed_packages.getSequenceNumber()));
ApplicationPreferences.putInteger(this, GET_CHANGED_PACKAGES_SEQUENCE_NUMBER_KEY, changed_packages.getSequenceNumber());
}
else
{
LogUtilities.show(this, String.format("Storing %s is the sequence number for next iteration", sequence_number + 1));
ApplicationPreferences.putInteger(this, GET_CHANGED_PACKAGES_SEQUENCE_NUMBER_KEY, sequence_number + 1);
}
}
catch (Exception e)
{
LogUtilities.show(this, e);
}
}
My experimental results so far have shown that this PackageManager API method getChangedPackages() is not reliable: quite often the returned ChangedPackages value contains many unchanged packages. So I’ve decided to implement a similar feature in a class called PackageUtils, as shown below. The idea is to poll for all the installed packages, as shown in method getInstalledPackageNames() below, and compare the string list with a previously saved one. This comparison boils down to comparing 2 string lists, as shown in method operate2StringLists() below. To get a set of removed packages, use GET_1_MINUS_2_OR_REMOVED as operation. To get a set of added packages, use GET_2_MINUS_1_OR_ADDED as operation.
public class PackageUtils {
public static final int GET_1_MINUS_2_OR_REMOVED = 0;
public static final int GET_2_MINUS_1_OR_ADDED = 1;
// Get all the installed package names
public static List<String> getInstalledPackageNames(Context context) {
List<String> installedPackageNames = new ArrayList<>();
try {
PackageManager packageManager = context.getPackageManager();
List<ApplicationInfo> appInfoList = packageManager.getInstalledApplications(PackageManager.GET_META_DATA);
for (ApplicationInfo appInfo : appInfoList) {
installedPackageNames.add(appInfo.packageName);
}
} catch (Exception e) {
e.printStackTrace();
}
return installedPackageNames;
}
// Compare 2 string lists and return differences.
public static Set<String> operate2StringLists(List<String> pkgList1, List<String> pkgList2, int operation) {
Set<String> result = null;
Set<String> pkgSet1 = new HashSet<String>(pkgList1);
Set<String> pkgSet2 = new HashSet<String>(pkgList2);
switch (operation) {
case GET_1_MINUS_2_OR_REMOVED:
pkgSet1.removeAll(pkgSet2);
result = pkgSet1;
break;
case GET_2_MINUS_1_OR_ADDED:
pkgSet2.removeAll(pkgSet1);
result = pkgSet2;
break;
default:
break;
}
return result;
}
}
The code has been tested on an Android Oreo device. It can reliably detect all added and removed packages between 2 time instances. However, it can’t detect updated packages in-between.
Finally got it. You have to create a variable called sequenceNumber, and update it every time you query changed packages.
private static int sequenceNumber = 0;
...
PackageManager pm = getContext().getPackageManager();
ChangedPackages changedPackages = pm.getChangedPackages(sequenceNumber);
if(changedPackages != null)
sequenceNumber = changedPackages.getSequenceNumber();

Correct name of installed applications

I am getting the names for my installed applications with below code and use them to see if any updates for this application is available.
But sometimes an incorrect name (MX Speler instead of MX Player) is being provided, as a result no updates are found.
Is there any better code i can/should use?
{
final PackageInfo pi = installedInfo != null ? installedInfo : downloadedInfo;
final PackageManager pm = getApplicationContext().getPackageManager();
ApplicationInfo ai;
try {
ai = pm.getApplicationInfo(pi ??, 0); //How to set the name of the installed application?
} catch (final NameNotFoundException e) {
ai = null;
}
final String applicationName = (String) (ai != null ? pm.getApplicationLabel(ai) : "(unknown)");
System.out.println("Application name : "+ applicationName);
}
You should not use labels as it may be different for every language or changed on each update. You should only rely on application's id (packageId) as this id stays unchanged for the whole life of the application.

How can I obtain and show the version number of an android apk?

In Xcode I do this:
NSString *temp;
temp = [#"© 2011 Robert Schoenburg "stringByAppendingString:#"Version "];
temp = [temp stringByAppendingString:[[[NSBundle mainBundle] infoDictionary] objectForKey:#"CFBundleVersion"]];
myLabel.text = temp;
How can I do the same thing in an android app?
Use the getPackageInfo method from PackageManager (which can be obtained via getPackageManager method of any Context). In the resulting PackageInfo, there are versionCode and versionName fields to read from. They correspond to values set in the AndroidManifest.xml in <manifest> tag.
EDIT: Added example for obtaining version code/name of own .apk:
PackageManager pm = context.getPackageManager();
try {
PackageInfo pi = pm.getPackageInfo(context.getPackageName(), 0);
Log.d("Version " + pi.versionName + "(" + pi.versionCode + ")");
} catch (NameNotFoundException e) {
// impossible here as we refer to our own package
}
PackageManager m = getPackageManager();
PackageInfo info = m.getPackageInfo("com.mypackage.name", 0);
Log.i("Version Code",info.versionCode+"");

Android: Populate ListPreference with installed applications?

I am somewhat new to android development, this is my first time trying to provide a list of installed applications in a preference screen. The selected value from this list will later be used to launch an app of the user's choosing.
I have created the following class:
public class AppSelectorPreference extends ListPreference {
public AppSelectorPreference(Context context, AttributeSet attrs) {
super(context,attrs);
PackageManager pm = context.getPackageManager();
List<PackageInfo> appListInfo = pm.getInstalledPackages(0);
CharSequence[] entries = new CharSequence[appListInfo.size()];
CharSequence[] entryValues = new CharSequence[appListInfo.size()];
try {
int i = 0;
for (PackageInfo p : appListInfo) {
if (p.applicationInfo.uid > 10000) {
entries[i] = p.applicationInfo.loadLabel(pm).toString();
entryValues[i] = p.applicationInfo.packageName.toString();
Log.d(BT,"Label: " + entries[i]);
Log.d(BT,"PName: " + entryValues[i]);
i++;
}
}
} catch (Exception e) {
Log.e(BT,"ER> Put descriptive error message here");
e.printStackTrace();
}
setEntries(entries);
setEntryValues(entryValues);
}
}
This is added to my PreferenceScreen with the following XML:
<PreferenceCategory android:title="Launch Application">
<CheckBoxPreference
android:title="Launch Application"
android:summary="Launch an application"
android:defaultValue="false"
android:key="pref_connect_launch_enable"
android:dependency="pref_connect_enable"/>
<com.nirsoftware.AppSelectorPreference
android:title="Select Application"
android:summary="Select application to launch"
android:key="pref_connect_package_name"
android:dependency="pref_connect_launch_enable"/>
</PreferenceCategory>
It appears everything is working correctly, until clicking on the AppSelectorPreference, which throws an NPE in android.preference.ListPreference.findIndexOfValue(ListPreference.java:169). Any suggestions?
I have tried your code, and it works fine for me, both adding the prefence programatically like this:
AppSelectorPreference pref2 = new AppSelectorPreference(this, null);
getPreferenceScreen().addPreference(pref2);
and using xml like you wrote. Which one is the line 169 where the error is appearing?
Also, did you look through logcat to see if any of the app names or labels is giving something like null? The only thing I can think is that your android has different apps from mine.
Edit: Sorry, I tested again and now I got the same error as you. Not sure what I did different, other than select a value
However, I fixed it by overriding the findIndexOfValue method. I just did this to test:
#Override
public int findIndexOfValue(String value) {
return 0;
//return super.findIndexOfValue(value);
}
but of course you will need to implement findind the index for that value. Might be an android bug for very large entries arrays?
i would suggest you to remove the if condition, cause it is what causes a null pointer error since the condition will be false unable to execute the commands null entries a value
try {
int i = 0;
for (PackageInfo p : appListInfo) {
entries[i] = p.applicationInfo.loadLabel(pm).toString();
entryValues[i] = p.applicationInfo.packageName.toString();
Log.d(BT,"Label: " + entries[i]);
Log.d(BT,"PName: " + entryValues[i]);
i++;
}
} catch (Exception e) {
Log.e(BT,"ER> Put descriptive error message here");
e.printStackTrace();
}

Determine list of permissions used by an installed application in Android

I have to determine list of permission used each by the installed application on my device.
I have got the list of applications installed and there package name using the following code:
PackageManager pm = this.getPackageManager();
Intent intent = new Intent(Intent.ACTION_MAIN, null);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
List<ResolveInfo> list = m.queryIntentActivities(intent,PackageManager.PERMISSION_GRANTED);
for (ResolveInfo rInfo : list) {
Log.d("Installed Applications", rInfo.activityInfo.applicationInfo
.loadLabel(pm).toString());
Log.d("packegename",rInfo.activityInfo.applicationInfo.packageName.
toString());
}
How do I get permission used by each application?
So i coded it.i needed not just the permissions but also the recievers and services.pls see the following code,hope its useful for others.
PackageManager p = this.getPackageManager();
final List <PackageInfo> appinstall=p.getInstalledPackages(PackageManager.GET_PERMISSIONS|PackageManager.GET_RECEIVERS|
PackageManager.GET_SERVICES|PackageManager.GET_PROVIDERS);
for(PackageInfo pInfo:appinstall){
//PermissionInfo[] permission=pInfo.permissions;
String[] reqPermission=pInfo.requestedPermissions;
ServiceInfo[] services=pInfo.services;
ProviderInfo[] providers=pInfo.providers;
int versionCode=pInfo.versionCode;
Log.d("versionCode-package ",Integer.toString(versionCode));
Log.d("Installed Applications", pInfo.applicationInfo
.loadLabel(pm).toString());
Log.d("packegename",pInfo.packageName.
toString());
if(reqPermission!=null)
for(int i=0;i<reqPermission.length;i++)
Log.d("permission list",reqPermission[i]);
}
NOTICE-setting flags is important otherwise it causes problem n u cnt get services ,provider
NOTE- NULL CHECK IS IMP OR IT GIVES NPE
also the previous code i wrote ws using activityInfo this one uses packageInfo .it better now i guess :)
happy coding ppl :)
Here how to retrieve the list of the apps installed on an Android device, and the permissions used by every app.
private static final String TAG = "MyActivity";
...
final PackageManager pm = getPackageManager();
final List<ApplicationInfo> installedApps = pm.getInstalledApplications(PackageManager.GET_META_DATA);
for ( ApplicationInfo app : installedApps ) {
//Details:
Log.d(TAG, "Package: " + app.packageName);
Log.d(TAG, "UID: " + app.uid);
Log.d(TAG, "Directory: " + app.sourceDir);
//Permissions:
StringBuffer permissions = new StringBuffer();
try {
PackageInfo packageInfo = pm.getPackageInfo(app.packageName, PackageManager.GET_PERMISSIONS);
String[] requestedPermissions = packageInfo.requestedPermissions;
if ( requestedPermissions != null ) {
for (int i = 0; i < requestedPermissions.length; i++) {
permissions.append(requestedPermissions[i] + "\n");
}
Log.d(TAG, "Permissions: " + permissions);
}
}
catch ( PackageManager.NameNotFoundException e ) {
e.printStackTrace();
}
}
Check out freetaskmanager
// Loop through all installed packaged to get a list of used permissions and PackageInfos
for (PackageInfo pi : appList) {
// Do not add System Packages
if ((pi.requestedPermissions == null || pi.packageName.equals("android")) ||
(pi.applicationInfo != null && (pi.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0))
continue;
for (String permission : pi.requestedPermissions) {
Map<String, String> curChildMap = new HashMap<String, String>();
try {
PermissionInfo pinfo = mPm.getPermissionInfo(permission, PackageManager.GET_META_DATA);
CharSequence label = pinfo.loadLabel(mPm);
CharSequence desc = pinfo.loadDescription(mPm);
} catch (NameNotFoundException e) {
Log.i(TAG, "Ignoring unknown permission " + permission);
continue;
}
}
}
This is the only thing I found, though I've not tested it out.
Let me know if it works for any one:
pm.getPackageInfo(rInfo.activityInfo.applicationInfo.packageName, packageManager.GET_PERMISSIONS);

Categories

Resources