First attempt:
Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
this.startActivityForResult(pickIntent, MoreIconsConstants.REQUEST_PICK_APPLICATION)
Average time needed-->8-10 second
Second attempt I tried getting the list of apps in a list programmatically and showing the list my self in a dialog.-----> about 4 seconds...Faster but still slow.
Third attempt: I store the list in my preferences file so that in the future I load that list instantly...meanwhile getting the current list in the background and if there are differences update the list being shown to the user----Also about 4 seconds.
This is where it gets weird. Using Log statements i measured th exact time each method needs. If I load the list from the prefferences first and then load it by querying the package manager I takes again 4 seconds for the preferences method and 0.5 second for the querying method
If i load the list querying the package manager first and then load it from preferences it takes about 4 seconds for the querying method and 0.5 second for the preference method
So no matter what I do the first method takes a lot of time and the second is executed right away.
Is there an explanation for this or some other way to load this list faster?
I quote my code for both methods
loading list quering the package manager
private class AppAsyncCaller extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() { super.onPreExecute(); }
@Override
protected Void doInBackground(Void... params) {
ArrayList<AppItem> allAppsInDevice2 = new ArrayList<AppItem>();
long timeStart=System.currentTimeMillis();
Intent mainIntent = new Intent(Intent.ACTION_MAIN, null).addCategory(Intent.CATEGORY_LAUNCHER);
List<ResolveInfo> packages=pm.queryIntentActivities(mainIntent, 0);
for(int i=0;i<packages.size();i++){
try{
String packageName=packages.get(i).activityInfo.packageName;
AppItem item=getAppItem(packageName,false);
if(item!=null){allAppsInDevice2.add(item);}
}catch(Exception e){}
}
Log.w(Long.toString(System.currentTimeMillis()-timeStart),"duration using async caller");
return null;
}
@Override
protected void onPostExecute(Void result) { super.onPostExecute(result); }
}
loading list from preferences method:
private class AppPrefsAsyncCaller extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() { super.onPreExecute(); }
@Override
protected Void doInBackground(Void... params) {
long timeStart=System.currentTimeMillis();
String allAppsListString = prefs.getString("allAppsListString", "");
String[] tab=allAppsListString.split("_APPAPPAPP_");
allAppsInDevice.clear();
boolean updateAllApps=false;
for(String s:tab){
if(!s.equals("") && !s.equals(" ")){
AppItem item=getAppItem(s,false);
if(item!=null){ allAppsInDevice.add(item); }
}
}
Log.w(Long.toString(System.currentTimeMillis()-timeStart),"duration apo pref");
return null;
}
@Override
protected void onPostExecute(Void result) { super.onPostExecute(result); }
}
public AppItem getAppItem(String packageName,boolean getIcon){
AppItem item=new AppItem();
item.packageName=packageName;
ApplicationInfo ai=null;
try { ai = pm.getApplicationInfo( packageName, 0); }
catch (final NameNotFoundException e) {return null; }
final String applicationName = (String) (ai != null ? pm.getApplicationLabel(ai) : "(unknown)");
item.appName=applicationName;
Intent intent = pm.getLaunchIntentForPackage(packageName);
if(getIcon){
Drawable icon=null;
if (intent != null) { try { icon = pm.getActivityIcon(intent); } catch (NameNotFoundException e) {} }
item.icon=icon;
}
return item;
}
public class AppItem{
String packageName;
String appName;
Drawable icon;
}