Search code examples
androidandroid-preferencessharedpreferenceslistpreferencepreferencescreen

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?


Solution

  • 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?