在启动之前加载活动

时间:2013-07-17 16:12:24

标签: android asynchronous progressdialog

我有一个包含按钮的活动,当您按下按钮时,它会加载第二个活动。第二个活动有一个listView和一堆其他的东西,在大多数设备上加载需要几秒钟,所以我想显示progressDialog让用户知道活动实际上正在加载。我有以下内容:

编辑:

public void rewardFirstNext(View view){

    AsyncTask<Void, Void, Void> progress = new AsyncTask<Void, Void, Void>() {
        private ProgressDialog progressDialog;

        protected void onPreExecute(){
            progressDialog = new ProgressDialog(getApplicationContext());
            progressDialog.setTitle("Loading Installed Applications");
            progressDialog.setMessage("Please wait.");
            progressDialog.setCancelable(false);
            progressDialog.setIndeterminate(true);
            progressDialog.show();
        }

        protected Void doInBackground(Void... voids) {
            PackageManager mPackageManager = getPackageManager();
            List<Applications> packageInfoListApps = new ArrayList<Applications>();
            List<PackageInfo> packageInfoList = mPackageManager.getInstalledPackages(PackageManager.GET_PERMISSIONS);
            List<PackageInfo> installedApps = new ArrayList<PackageInfo>();

            for (PackageInfo apps : packageInfoList) {
                if (!isSystemPackage(apps)) {
                    installedApps.add(apps);
                }

            }
            Collections.sort(installedApps, new Comparator<PackageInfo>() {
                public int compare(PackageInfo o1, PackageInfo o2) {
                    return o1.applicationInfo.loadLabel(getPackageManager()).toString().compareToIgnoreCase(o2.applicationInfo.loadLabel(getPackageManager()).toString());
                }
            });
            for (PackageInfo apps : installedApps) {
                packageInfoListApps.add(new Applications(apps, false, false));
            }

            //what else happens here?
            return null;
        }

        protected void onPostExecute(Void result){
            progressDialog.dismiss();
        }
    };

    progress.execute((Void[]) null);
}

1 个答案:

答案 0 :(得分:1)

你应该做的就是把所有这些

mPackageManager = getPackageManager();
List<Applications> packageInfoListApps = new ArrayList<Applications>();
List<PackageInfo> packageInfoList = mPackageManager.getInstalledPackages(PackageManager.GET_PERMISSIONS);
List<PackageInfo> installedApps = new ArrayList<PackageInfo>();

for (PackageInfo apps : packageInfoList) {
    if (!isSystemPackage(apps)) {
        installedApps.add(apps);
    }

}
Collections.sort(installedApps, new Comparator<PackageInfo>() {
    public int compare(PackageInfo o1, PackageInfo o2) {
        return o1.applicationInfo.loadLabel(getPackageManager()).toString().compareToIgnoreCase(o2.applicationInfo.loadLabel(getPackageManager()).toString());
    }
});
for (PackageInfo apps : installedApps) {
    packageInfoListApps.add(new Applications(apps, false, false));
}

在asynctask内部。

获取此new ApplicationAdapter(this, packageInfoListApps, mPackageManager)并在

中保留对它的引用
ApplicationAdapter adater = new ApplicationAdapter(this, packageInfoListApps, mPackageManager)

然后按照您的操作和onPostCreate调用

开始进度对话框
adapter.notifyDataSetChanged();

然后填充listview,你也应该停止对话