3

As I've asken on another question HERE it seems that the PackageManager.getInstalledPackages() doesn't play nice with Threading. As CommonsWare stated HERE:

Perhaps the PackageManager needs to be invoked on the main application thread, for some reason

So having it on a thread gets undesired behavior, entering and exiting a few times in my Activity makes the list of displayed apps sometimes with items, sometimes empty. Having everything in the UI Thread works like a dream, loads fine everytime. The thing is, the user expects some sort of feedback and I need to provide one. As I start the activity, the screen remains black for 3-4-5-6 seconds (depending on the device and apps installed). How can I provide some sort of feedback ? I am thinking of a ProgressDialog but I don't know how can I start it. Thank you.

Community
  • 1
  • 1
Alin
  • 14,809
  • 40
  • 129
  • 218
  • I doubt it isn't the case, but are you sure its the call to PackageManager.getInstalledPackages() that takes time, and not the loop through the returned data that makes it pause? You could get the data on the main thread and then loop through it in your AsyncTask, but I doubt this is the issue. – FunkTheMonk Nov 14 '11 at 12:27
  • Actually you are right, getting the intalled Packages() in the UI thread and making the loop in a AsyncTask does the job right. Write it a answer so I could accept it. Thanks – Alin Nov 14 '11 at 13:06

3 Answers3

1

Use Async to do background work and show indicator while loading data.

in you onCreate(). call new AsyncCommonDataFetcher(this).execute();

public class AsyncCommonDataFetcher extends AsyncTask<Void, Void, Void> {

    Context mContext = null;
    private ProgressDialog mProgressIndicator = null;

    public AsyncCommonDataFetcher(Context ctx) {
        mContext = ctx;
    }

protected void onPreExecute() {
        mProgressIndicator = ProgressDialog.show(((Activity) mContext), null,
                    "Please wait", true);

    }
    @Override
    protected Void doInBackground(Void... voids) {
        // Do what ever work you like to do. It will do this in backgound and show user a indicator to wait.
    }

    @Override
    protected void onPostExecute(Void voidInstance) {


            try {
                if (mProgressIndicator != null) {
                    mProgressIndicator.hide();
                    mProgressIndicator.dismiss();
                }
            } catch (Exception e2) {
                e2.printStackTrace();
            }
        }

    }
}
Arslan Anwar
  • 18,746
  • 19
  • 76
  • 105
  • Do your task in OnPostexecute not in doInbackround...... cause it will cause run time Exection as mention below http://stackoverflow.com/questions/7474379/asynctask-only-the-original-thread-that-created-a-view-hierarchy-can-touch-its – Buggy IdioT Sep 22 '14 at 05:27
1

As discovered, the loop to work through the applications takes awhile (which can be done in a separate thread), compared to the call to PackageManager.getInstalledPackages() (which has to be done on the UI thread).

FunkTheMonk
  • 10,908
  • 1
  • 31
  • 37
  • Does it really have to be done on the UI thread? See my answer here: http://stackoverflow.com/questions/3455781/packagemanager-getinstalledpackages-returns-empty-list/26172008#26172008 – darken Oct 03 '14 at 01:26
0

Try the following for ProgressDialog in the onCreate() of your activity

this.requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
ProgressDialog LoadingDialog = ProgressDialog.show(this, "", "Loading..", true);

And then dismiss it when the process causing the delay is over

LoadingDialog.dismiss();
devanshu_kaushik
  • 929
  • 11
  • 30