문제

Background

I'm working on my app that is an alternative to the app manager (link here), and wish to optimize it a bit.

As it turns out, one of the slowest things on the app is its bootup, and the main reason for this is getting the app name . I intend on caching it, but I also wish to optimize the way it's being queried, if possible.

The problem

Android has two ways to get the app name: PackageManager.getApplicationLabel and ApplicationInfo.loadLabel .

both have about the same description, but I'm not sure which one should be used.

Not only that, but looking at the code of "ApplicationInfo.loadLabel" , it looks something like this:

public CharSequence loadLabel(PackageManager pm) {
    if (nonLocalizedLabel != null) {
        return nonLocalizedLabel;
    }
    if (labelRes != 0) {
        CharSequence label = pm.getText(packageName, labelRes, getApplicationInfo());
        if (label != null) {
            return label.toString().trim();
        }
    }
    if (name != null) {
        return name;
    }
    return packageName;
}

I can't find the code of "PackageManager.getApplicationLabel", as it's abstract.

The question

Is there any difference between the two?

If there is no difference, why do we have 2 very similar methods to get the same app name? I mean, I can use either of them only if I have both applicationInfo object and the PackageManager object, but that's enough to use any of the methods...

If there is difference, which of them is better in terms of speed?

도움이 되었습니까?

해결책

The source of 'PackageManager.getApplicationLabel' is available in 'ApplicationPackageManager.java'. It is as follows;

@Override
public CharSequence getApplicationLabel(ApplicationInfo info) {
    return info.loadLabel(this);
}

ApplicationPackageManager.java

I see in AppUtils.java the same wrapping is done as follows;

/** Returns the label for a given package. */
public static CharSequence getApplicationLabel(
        PackageManager packageManager, String packageName) {
    try {
        final ApplicationInfo appInfo =
                packageManager.getApplicationInfo(
                        packageName,
                        PackageManager.MATCH_DISABLED_COMPONENTS
                                | PackageManager.MATCH_ANY_USER);
        return appInfo.loadLabel(packageManager);
    } catch (PackageManager.NameNotFoundException e) {
        Log.w(TAG, "Unable to find info for package: " + packageName);
    }
    return null;
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top