Faheem
Faheem

Reputation: 509

Using ApplicationInfo.FLAG_SYSTEM to filter out System Apps

I am using following code to differentiate between User Installed App and System App:

final PackageManager pm = getPackageManager();
List<ApplicationInfo> packages = pm.getInstalledApplications(PackageManager.GET_META_DATA);
for (ApplicationInfo packageInfo : packages)
{
    if ((packageInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0)
        Log.d(TAG, "Installed package (System) :" + packageInfo.packageName);
    else
        Log.d(TAG, "Installed package (User) :" + packageInfo.packageName);
}

How ever this gives some apps as User Installed Apps which are actually not. Following is the LogCat in my emulator:

Installed package (User) :com.android.smoketest
Installed package (User) :com.android.widgetpreview
Installed package (User) :com.example.android.livecubes
Installed package (User) :com.example.android.apis
Installed package (User) :com.android.gesture.builder
Installed package (User) :com.example.android.softkeyboard
Installed package (User) :com.android.smoketest.tests
Installed package (User) :faheem.start

Ideally, only last application should be output in User Installed Apps.

Upvotes: 3

Views: 7210

Answers (1)

user330844
user330844

Reputation: 880

Uddhav Gautam

I think your answer is wrong in multiple places.

  1. the code you site in the link specifically states:

    /** * Flags associated with the application. Any combination of

which means your third example (following the OR) is completely wrong since the value 0x1001 has the 1 bit set but is definitely not equal to 1

  1. Since the result of an and operation against the mask 0x1 is always either 0x0 or 0x1, there is no difference between != 0 and == 1

that said I do agree that the correct way to test for any bit being set is:

if ((packageInfo.flags & ApplicationInfo.FLAG_SYSTEM) == ApplicationInfo.FLAG_SYSTEM)

Upvotes: 1

Related Questions