I am building android app that get Installed apps using PackageManager and files (Not all file just the files ends with(txt,docx,MP4,3gp,wmv,mp3,apk) from the device when it starts (i do that inside onStart() function),
actually in the first version take about 12-15 sec But i do some process using AsyncTask in Background and that help me a little, But still not enough for me >> I need to run the app without delay !!! or if there some tricks to do what i want ??
the Code :
public class GetApps {
private PackageManager packageManager = null;
private List<ApplicationInfo> applist = null;
MainActivity activity;
public GetApps( MainActivity act,PackageManager pm) {
this.activity=act;
this.packageManager = pm;
new LoadApplications().execute();
}
private List<ApplicationInfo> checkForLaunchIntent(List<ApplicationInfo> list) {
ArrayList<ApplicationInfo> appList = new ArrayList<ApplicationInfo>();
for (int i = 0; i < list.size(); i++) {
ApplicationInfo info = list.get(i);
try {
if (packageManager.getLaunchIntentForPackage(info.packageName) != null) {
appList.add(info);
}
} catch (Exception e) {
e.printStackTrace();
}
}
return appList;
}
private class LoadApplications extends AsyncTask<Void, Void, List<ApplicationInfo>> {
private ProgressDialog progress = null;
@Override
protected List<ApplicationInfo> doInBackground(Void... params) {
return packageManager.getInstalledApplications(PackageManager.GET_META_DATA);
}
@Override
protected void onPostExecute(List<ApplicationInfo> list) {
super.onPostExecute(list);
MainActivity.progressDialog.dismiss();
System.out.println(">>>>8 onPostExecute ...");
activity.callBackDataFromAsynctask(list);// To MainActivit Clas
}
}
and in the main activity i fill three arrays (name and package name and icons)
the Code:
public void callBackDataFromAsynctask(final List<ApplicationInfo> list) {
System.out.println(">>>>9 callBackDataFromAsynctask ...");
values = new String[list.size()+namesOfFiles.size()];
values1 = new String[list.size()+namesOfFiles.size()];
icons1 = new Drawable[list.size()+namesOfFiles.size()];
for (int i = 0; i < list.size(); i++) {
ApplicationInfo Info = list.get(i);
try {
values[i] = Info.loadLabel(packageManager).toString();
values1[i] = Info.packageName;
try {
icons1[i] = packageManager.getApplicationIcon(values1[i]);
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
insertNamesOfFiles(list.size());
System.out.println(">>>>10 Complete ...");
}
and this code the get files names
public void insertNamesOfFiles(int iii)
{
int j = 0;
for ( int i =iii; i < values.length; i++) {
values[i] = namesOfFiles.get(j);
values1[i]="gogo";
if (values[i].endsWith(".txt"))
icons1[i] = txt;
else if (values[i].endsWith(".docx"))
icons1[i] = doc;
else if (values[i].endsWith(".MP4") || values[i].endsWith(".3gp"))
icons1[i] = vid;
else if (values[i].endsWith(".wmv") || values[i].endsWith(".mp3"))
icons1[i] = adu;
else if (values[i].endsWith(".apk"))
icons1[i] = apk;
j++;
}
}
Thx in Advance