2

After a few hours of trying I still cannot figure out how to incorporate an asynctask into the below code.

I have tried threading which didn't work either. All I want to do is to run the scan in the background and show a progressbar.

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
}

private void populateView() {

    List<PackageInfo> adPackages = getAdPackages();
    PackageManager pm = getPackageManager();

    List<Map<String, String>> data = new ArrayList<Map<String, String>>(adPackages.size());
    for(PackageInfo pkg : adPackages) {
        Map<String, String> attrs = new HashMap<String, String>();
        attrs.put("App Name", pm.getApplicationLabel(pkg.applicationInfo).toString());
        attrs.put("Package Name", pkg.packageName);
        data.add(attrs);
    }

    String[] from = new String[] {
        "App Name",
        "Package Name"
    };
    int[] to = new int[] {
        android.R.id.text1,
        android.R.id.text2
    };
    SimpleAdapter adapter = new SimpleAdapter(
            this, data, android.R.layout.two_line_list_item, from, to);

    setListAdapter(adapter);
    mPackages = adPackages;
}

.

private List<PackageInfo> getAdPackages() {
    Set<PackageInfo> adPackages = new HashSet<PackageInfo>();

//[...]
    List<ApplicationInfo> appInfos = pm.getInstalledApplications(0);

    for(ApplicationInfo appInfo : appInfos) {

        try {

            //[Heavy Stuff]

    return new ArrayList<PackageInfo>(adPackages);

   }
}
ahsteele
  • 26,243
  • 28
  • 134
  • 248
xAnGz
  • 127
  • 4
  • 11
  • which part you want to run in Async, i suppose getting package loop `in onCreate` – havexz Dec 03 '11 at 07:14
  • I'm trying to run it at `private List getAdPackages() ` as long it will not cause ANR and it will allow to update UI thread to show the progress – xAnGz Dec 03 '11 at 07:19

1 Answers1

2

Yes this can be done.

You have to move your getPackages logic to doInBackground of AsyncTask. And you have to call publishProgress from doInBackground when you want to update progress bar.

Once doInBackground is done, then onPostExecute is called. Put all the logic for data for adapter and adapter itself in it. Set the adapter also in the function.

Below are few reference docs you can refer:

Async Task Worker Threads

Here is some sample:

 private class GetPackageTask extends AsyncTask<Void, Integer, List<PackageInfo>> {
 protected List<PackageInfo> doInBackground(URL... urls) {

     // Put your code of getPackages in here

     // You can call publish like it is done below
     //for (int i = 0; i < count; i++) {
     //    totalSize += Downloader.downloadFile(urls[i]);
     //    publishProgress((int) ((i / (float) count) * 100));
     //}

     // adPackages is what you returning from your getPackages function
     return adPackages;
 }

 protected void onProgressUpdate(Integer... progress) {
     setProgressPercent(progress[0]);
 }

 protected void onPostExecute(List<PackageInfo> result) {
     // Here you will have all the setAdapter related code
 }
}

onCreate will contain

new DownloadFilesTask().execute();

havexz
  • 9,550
  • 2
  • 33
  • 29
  • Sound very complicated, I will try, do you mind help me? I will pay you – xAnGz Dec 03 '11 at 07:31
  • Hope now it will be less complicated...well I reply on stackoverflow just for fun...you have to write `setProgressPercent(progress[0])` function which will update the `ProgressBar` – havexz Dec 03 '11 at 07:46
  • Thanks for your help , this is what i have done http://pastebin.com/nvXD6YHv but still there is a single line error at `protected List doInBackground(Void... params) {` did i miss anything? – xAnGz Dec 03 '11 at 08:31
  • 1
    Updated the paste at : http://pastebin.com/Gg4n95uD ...removed `private List getAdPackages() {` and changed `List adPackages = getAdPackages();` to `List adPackages = result;`...this should work now. – havexz Dec 03 '11 at 09:08