xAnGz
xAnGz

Reputation: 127

How to apply async task into this

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);

   }
}

Upvotes: 2

Views: 415

Answers (1)

havexz
havexz

Reputation: 9590

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();

Upvotes: 2

Related Questions